Build and test DLLs

Build DLLs that call a REST API to return the latest price of bitcoin in US dollars and Euros.

Prerequisites

  • Basic understanding and experience with programming
  • Access to an Integrated Development Environment (IDE) to develop code examples as a DLL that supports C#.

    An example IDE: Microsoft Visual Studio Community Edition

  • Access to the Automation Anywhere Enterprise Client
  • Bot Creator permission
  • A decent understanding of the Automation Anywhere RPA platform, including basic understanding of Task Bots, MetaBots, and what they do.
    Tip: Review this topic to understand how to build a bot: Build a basic bot using the Enterprise Client.

There are four programs that are part of the broader solution MyApp4Lib. Input and build this sample code.

Two DLLs are created:
  • MyApp4Lib.dll
  • RestSharp.dll

Procedure

  1. Compile the code that is used to test the methods.
    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. Compile the code that orchestrates the REST call and retrieves the bitcoin price.
    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. Compile the methods to make REST calls.
    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. Compile the code that processes the JSON response and returns the bitcoin price.
    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; }
        }
    }
    

Next steps

Add the DLLs created in this task to a MetaBot in Create an advanced MetaBot.