Go to the profile of  Leo
Leo
why do i need a bio? they are painful to write.
4 min read

How to send messages from UIPath workflows to Azure Bot Framework to the user 🚀

How to send messages from UIPath workflows to Azure Bot Framework to the user  🚀

In my previous blog posts, I've shown how to call UIPath workflows in your bot. However, as it was my first attempt with UIPath the basic use case was to end the process when the Azure Bot calls UIPath. This was only unidirection and more development time poured into this project. I wanted to make it more exciting by adding a way for the two bots to communicate with each other.

This sample code will only work with local machine. It will be a different code and approach once you upload and deploy to production. I will share my findings as I explore in my next blog posts

Platform Versions Used

  1. Azure Bot Framework SDK v4 (C#)
  2. Visual Studio 2017
  3. UIPath Studio 2019.2.0

In order to achieve this, one has to know what are the APIs available between the two platforms. I tried searching for the official documentation, but it looks like it's hard to find how to actually use them.

Azure Bot Framework

As I mentioned earlier, you will have to use DirectLine API for Azure Bot once you deploy it in a different environment such as Azure App Service. The idea is that when a user requests a task that needs to be delegated to the UIRobot, the Azure Bot has to respond like it actually came from the Azure Bot. According to the emulator, it sends the following payload

Bot Message

{
  "channelId": "emulator",
  "conversation": {
    "id": "2e183be0-3ae6-11e9-9c43-a36fcce34cfc|livechat"
  },
  "from": {
    "id": "1",
    "name": "Bot",
    "role": "bot"
  },
  "id": "312b4840-3ae6-11e9-baf6-dd0df9b23664",
  "inputHint": "acceptingInput",
  "localTimestamp": "2019-02-27T18:19:55-05:00",
  "locale": "",
  "recipient": {
    "id": "22556c84-cc2c-41fd-8527-ac8167a7eb50",
    "role": "user"
  },
  "replyToId": "2e4b59d0-3ae6-11e9-baf6-dd0df9b23664",
  "serviceUrl": "https://b39898a3.ngrok.io",
  "showInInspector": true,
  "text": "Hello. Welcome to the Meeting Minutes bot!",
  "timestamp": "2019-02-27T23:19:55.332Z",
  "type": "message"
}

My Message

{
  "channelData": {
    "clientActivityID": "15513096792130.bapi252tsre",
    "state": "sent"
  },
  "channelId": "emulator",
  "conversation": {
    "id": "2e183be0-3ae6-11e9-9c43-a36fcce34cfc|livechat"
  },
  "from": {
    "id": "r_qkn4vtvag6",
    "role": "user"
  },
  "id": "632b9340-3ae6-11e9-baf6-dd0df9b23664",
  "localTimestamp": "2019-02-27T18:21:19-05:00",
  "locale": "",
  "recipient": {
    "id": "1",
    "name": "Bot",
    "role": "bot"
  },
  "serviceUrl": "https://b39898a3.ngrok.io",
  "showInInspector": true,
  "text": "test",
  "textFormat": "plain",
  "timestamp": "2019-02-27T23:21:19.220Z",
  "type": "message"
}

Using the bot's initial payload I removed the properties that I think is unnecessary until the Azure Bot will message back the user that actually came from the UIPath Robot.

{
  "channelId": "",
  "conversation": {
    "id": "2e183be0-3ae6-11e9-9c43-a36fcce34cfc|livechat"
  },
  "from": {
    "id": "1",
    "name": "Postman",
    "role": "Postman"
  },
  "recipient": {
    "id": "22556c84-cc2c-41fd-8527-ac8167a7eb50",
    "role": "user"
  },
  "replyToId": "2e4b59d0-3ae6-11e9-baf6-dd0df9b23664",
  "text": "Hello. This is from Postman!",
  "type": "customMessage"
}

The most important part in order for it to determine how to send the message acting as the user the replyToId, from, conversation, type. If you didn't specify the from the bot framework will return a response that those are required.

UIPath Studio

With UIPath studio. As of this writing, I am still exploring the different functionalities that are available in the platform. Luckily there was an HTTP Activity that can just send a payload to the Azure Bot Framework.

Image Description

From the properties of the HTTP Activity, you can set the body as a JSON string value that consists of the one that I mentioned earlier.

Going back to the Bot.cs file, you would want to hijack for now the type of activity that is being sent. From here, we can handle on the Azure Bot code that and delegate what the UIPath robot wants to send to the user.

public async Task OnTurnAsync(ITurnContext turnContext,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            _logger.LogTrace($"Activity Type: {turnContext.Activity.Type}");
            if(turnContext.Activity.Type == "customMessage")
            {
             await turnContext.SendActivityAsync(turnContext.Activity.Text, cancellationToken: cancellationToken); // Simulate as if the message was from the bot, but instead it's a message from UIPath saying it's done.
            }
        }

This solution was part of my exploratory uses of the UIPath Studio and Azure Bot, I am sure there are better ways to do it but I still have to find out what they are. For now this one works.