Automation Anywhere

Automation Anywhere

關閉內容

內容

開啟內容

建置和測試 DLL

  • 已更新:5/10/2019
    • 11.3.x
    • 建立
    • Enterprise

建置和測試 DLL

建置多個 DLL 來進行 REST API 呼叫,以美元和歐元為幣別傳回比特幣的最新價格。MetaBot

先決條件

  • 對程式設計的基本了解和經驗
  • 存取整合式開發環境 (IDE),以將程式碼範例開發為可支援 C# 的 DLL。IDE 範例是 Microsoft Visual Studio Community Edition
  • 存取 Automation Anywhere Enterprise 用戶端
  • 機器人建立器權限
  • Automation Anywhere RPA 平台的深入了解,包括對 TaskBotMetaBot 及其功能的基本了解。
    提示: 請查看使用Enterprise 用戶端建立基本機器人以取得機器人的基本了解。

有四個程式屬於更廣泛的解決方案 (MyApp4Lib)。輸入並建置此範例程式碼。

建立兩個 DLL:
  • MyApp4Lib.dll
  • RestSharp.dll

程序

  1. 編譯用於測試方法的程式碼。
    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MyApp4Lib; namespace MyApp4Test { class Program // Test program for the classes { static void Main(string[] args) { VisibleBotMethods vbm = new VisibleBotMethods(); String UsdPrice = vbm.GetPriceOfBitcoin("usd"); Console.WriteLine("\nPrice of Bitcoin in USD: " + UsdPrice); Console.ReadKey(); } } } 
  2. 編譯會協調 REST 呼叫並擷取比特幣價格的程式碼。
    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyApp4Lib { public class VisibleBotMethods { public String GetPriceOfBitcoin(String Currency) { // Make GET call -- the Currency is the variable: // usd for $Dollars, eur for Euros String URL = "https://api.cryptonator.com/api/ticker/btc-" + Currency; // Instantiate objects RestUtils ru = new RestUtils(); JsonUtils ju = new JsonUtils(); // Make the REST call and return the JSON response String JsonResp = ru.CallRestGETNoAuth(URL); // Retrieve just the price from the JSON as a String String BCPrice = ju.GetDataFromJsonResponse(JsonResp); return BCPrice; } } } 
  3. 編譯用來進行 REST 呼叫的方法。
    using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace MyApp4Lib { public class RestUtils { public String CallRestGETNoAuth(String URL) { // Setting up a web request on the URL that we pass as a parameter, // specifying a GET request for a JSON response System.Net.HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(URL); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "GET"; // Submitting the request, getting the response, turning it into a string, // and returning the response try { var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); return result; } } catch (System.Net.WebException e) // Catch error if URL is not valid { return "Error:" + e.Message; } } } } 
  4. 編譯會處理 Json 回應並傳回比特幣價格的程式碼。
    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyApp4Lib { class JsonUtils // Includes methods to process JSON { public String GetDataFromJsonResponse(String JsonResp) { RestSharp.RestResponse response = new RestSharp.RestResponse(); response.Content = JsonResp; // Turning JSON structure into object / class structure JsonDeserializer deserial = new JsonDeserializer(); StdJsonResponseForCurrCheck x = deserial.Deserialize<StdJsonResponseForCurrCheck>(response); String Resp = ""; // Identifying price within the class structure, and then return it Resp = x.ticker.price; return Resp; } } // Taking JSON response and turning it into classes public class Ticker { public string @base { get; set; } public string target { get; set; } public string price { get; set; } public string volume { get; set; } public string change { get; set; } } public class StdJsonResponseForCurrCheck { public Ticker ticker { get; set; } public int timestamp { get; set; } public bool success { get; set; } public string error { get; set; } } } 

後續步驟

將此任務中建立的 DLL 新增到 MetaBot,如將多個 DLL 新增到 MetaBot任務中所述。
傳送意見反饋