Try UiPath Free

21 July 2020

UiPath Custom Activities and Twilio Tutorial

21 July 2020

UiPath Custom Activities and Twilio Tutorial

⬅  SEE ALL THE ARTICLES

 

uipath devcon rpa developers virtual event 2020

 

My name is Joe Iaquinto. I’m a .NET developer at Troy Web Consulting and I recently had the opportunity to participate in the UiPath Custom Activity Hackathon. Erik Saulnier, a fellow developer at Troy Web Consulting, and I were able to quickly build an activity package to support using UiPath to communicate with Twilio. Our activity package makes it simple to send and receive SMS (a form of text messaging) messages, faxes, and look up phone number information using Twilio. My team was very excited to be selected as the winners of the hackathon!

 

In this blog, we want to demonstrate how easy it is to get started with developing custom activities that can extend the capabilities of your Robotic Process Automation (RPA) processes.

 

Building Custom Activities is a powerful way to integrate existing .NET code with the UiPath Platform. It allows you to easily connect .NET code or a software development kit (SDK) that you want to use in your UiPath RPA workflow by implementing the activity interfaces that all UiPath activities are built on (under the hood). UiPath has created a custom activity creation tool and project template that will quickly get your project set up to add the implementation of the activity with minimal additional configuration.

 

Getting started

You will need Microsoft Visual Studio Community, Professional, or Enterprise 2019 with the .NET desktop development workload installed and UiPath Studio to get started. Next, install the UiPath Activity Creator Extension to Visual Studio which will add a project template and the custom activity scaffolding tool.

 

Creating the project

In Visual Studio, create a new project and select the UiPath Standard Activity Project template. This template will set up a new environment with all of the projects and requisite files needed to build a custom activity NuGet Package. Three projects are created; one with the name of the project, one appended with .Activities, and one appended with .Activities.Design.

 

Adding activities

Next, navigate to the extensions menu in Visual Studio, and select UiPath > Add Activities. This will open a menu that allows you to add descriptions and define properties that the tool will scaffold into your project. This will create an Activity class in your Activities project and an associated designer in your Design project. It will also set up resources that your activity will use to set the name and description of the activity and its properties so that it can easily be localized to other languages for international users. Now that we have our activities created in our project, let’s take a look at the generated code and begin to implement their functionality.

 

Project structure

The Custom Activities Template automatically creates a few projects to keep activity implementation, design, and supporting code in separate locations. Let’s take a look at each of these to understand their purpose.

 

The Activities project is where the majority of the Activity code resides. It contains a class for each activity you add to your project. Each one of these classes has the activity’s input and output arguments as properties and contains a few essential methods.

  • A Constructor - The class constructor can define structural Constraints for an activity to run. For example, an activity which is required to be the child of a scope activity can add the constraint that it must have a specific parent type (type of the parent scope activity).
  •  
  • CacheMetadata - This function is used to define validations on the arguments so that the activity can run. For example, if an argument is required for the activity to work, you should call metadata.AddValidationError if the argument is equal to null.
  •  
  • Execute - This function is what is called during the actual execution of your activity. You can retrieve the value of Input Arguments and then perform some logic or call a .NET SDK Method. This function returns an Action, which is a function that will be evaluated after this method returns. This action should set the values of Output Arguments.

The Design project is where you can customize how the activity will appear in the UiPath Workspace panel when it is added to the workflow. You can customize which inputs and outputs appear as expression text boxes in the designer XAML file for each activity. This can make it easier for an RPA developer to get started using your activity in UiPath Studio.

 

For example, I customized my Twilio Scope activity to allow users to enter their Twilio credentials within the workspace panel instead of requiring them to use the properties panel, as well as customizing my Send Message activity to make it easier to say who to send the message to and what the message content should be.

 

 

Theming

The Design project also contains a Themes folder with a file named Icons.xaml. This file allows you to add custom icons to your activity using XAML Geometry tags which can be generated from SVG images. I utilized some open source Material design icons to create icons for my activities that give an additional visual context of what they do. Below is an example of the Icons.xaml entry for the Twilio Scope activity. You can view the complete file on GitHub.

 

 

There is also the DesignerMetadata.cs file. This file allows you to define how your activities should be grouped together in the Activities panel of UiPath Studio. They can be categorized by creating CategoryAttributes and assigning those attributes to the associated Designer class. Categories can be nested by adding a period separating the parent category name and the child category name.

 

 

How I used the Activity Creator Extension

My project for the hackathon utilized the Twilio C# SDK. This SDK has all of the objects that Twilio sends and receives with their REST API typed as .NET objects and has methods to make sending the various HTTP requests easily using a client object.

 

My strategy was to identify which parts of the Twilio SDK would be the best candidates for adding to the UiPath ecosystem. I settled on sending and retrieving messages and faxes and looking up phone number information. From the target functionality, I wrote a document with the activity names, descriptions, and inputs and outputs. Then, I went to the Activity Creator extension and entered that information. Planning ahead made this process quick and easy.

 

In my parent scope activity, I created a Twilio client object using the input arguments for the Account ID and Authorization Token that a user can get from the Twilio dashboard and store that client in the object container so it could be used by all of the child activities. The object container is provided in the template in order to store objects from a parent activity so that it can be accessed in the child activities. It essentially is a Dictionary with a key of a type which relates to a value of an instance of that type.

 

 

One thing that I did to make implementing the activities themselves a little easier and leaner was to create static wrapper classes for each SDK Type. These wrapper classes allowed me to know (before generating the activities) which input and output arguments I needed to define in the Activities Wizard and simplified the implementation. The Twilio SDK has many parameters and options I was not planning on passing through and requiring the user to configure, so creating the wrapper class allowed me to encapsulate some of the object creation logic and specifics of using the SDK into simpler methods. This allowed much of my activity code to simply call this method, get the resultant object from the SDK, and save the result to an output argument.

 

 

For example, in my Send SMS Message activity, I did not want to require users to provide the number they are sending the message to as a Twilio PhoneNumber object, as that would be cumbersome to use and a string would be easier for most users. The wrapper function for the Send Message request creates the PhoneNumber object and assigns it to the CreateMessageOptions object that is sent to Twilio. These wrapping functions keep everything clean and help separate the activity code from the specifics of the Twilio SDK. This also will aid in adjusting to changes in the Twilio SDK in the future.

 

 

In conclusion

This ability to extend UiPath Platform is one of the best parts of using custom activities to complement the rapid development of RPA. .NET Developers are able to apply all of their knowledge and skills they already have in .NET development to the growing field of RPA development. When you are able to break apart the heavy lifting logic, processing, and integration points into custom activities, you can enable your RPA solutions to be even more powerful. I highly recommend evaluating if developing a custom activity will enable new capabilities for your UiPath Workflows, increase robot performance, and enhance your development experience.

 

Joseph Iaquinto is a .NET developer at Troy Web Consulting.


by Joseph Iaquinto

TOPICS: RPA Tutorial, Custom Activity

Show sidebar