Telegram Bot Integration With UiPath (Remote Captcha solving)

Nikolay But
6 Min Read

Telegram Bot needs no introduction today and with the growing usage of chat bot in RPA industry it is obvious choice to integrate your workflow with telegram bot.

In this tutorial, we are going to write a small UiPath process which interacts with Telegram Bot chat: send to user a screenshot with Captcha image (from website https://captcha.com/demos/features/captcha-demo.aspx), wait for message with recognized Captcha text or command to change Captcha or command to speak Captcha out.

You can download the project .xaml from GitHub – You’ll just need to add your Telegram Bot token and ChatId (Id of chat of bot with you)  https://github.com/mflow42/rpabotsworldbot  to to get this example working.

Action plan for this article- 

  1. Add UiPath.Web.Activities package in Manage Packages
  2. Create Telegram Bot
  3. Create initial UiPath process.
  4. Get target ChatId to connect bot with you.
  5. Finalize UiPath with Telegram process.
Add UiPath.Web.Activities package in Manage Packages

 

To work with “HTTP request” and “Deserialize JSON” activity you need to first add UiPath Web Activities Pack as dependencies in your project.

You can enable and install it from the Manage Packages window as shown below.

Telegram Bot Integration With UiPath

Select UiPath Web Activities Pack and click on install to add a required dependency. As soon as they are added you will be able to see web activities from the activities panel as below.

UiPath.Web.Activities package in Manage Packages

Create new Telegram Bot

Open telegram app: mobile, desktop, or web (https://web.telegram.org/). Register if you did not do it before. Then follow instructions:

  1. Search “botfather”
  2. Open proper chat
  3. Create new bot by typing “/newbot”
  4. Give bot name and username
  5. Copy bot token – you’ll need it later.
  6. Open chat with new bot and Ssend any test message – to get ChatId in future steps.
Create new Telegram Bot

Create initial UiPath process.

We will use only the following types of Telegram methods:

  1. getUpdates – to get messages and callbacks from user.
  2. sendPhoto – to send screenshot.
  3. sendMessage – to send message user.

Read More at Telegram Documentation – https://core.telegram.org/bots/api#authorizing-your-bot 

First of all, you need to add the “Use Application\Browser” activity and assign it to the website https://captcha.com/demos/features/captcha-demo.aspx.

Create initial UiPath process.

Then add Flowchart and:

  1. Assign string Token to remembered before telegram bot token.
  2. Assign string UpdateId to “0”.
  3. Assign string Offset to ((CInt(UpdateId)+1).ToString. It needs to get not all but only last update from chat. By default, it is incremented value of UpdateId by 1.
  4. Add HTTP request activity and fill in it:

Assign EndPoint to “https://api.telegram.org/bot”+Token+”/getUpdates”

  • Add Parameters:
  • String offset = Offset
  • String limit = “1”
UiPath process for Web activity
Telegram Bot Integration With UiPath (Remote Captcha solving) 3

Our  Intial Workflow will look like this

Deserialize JSON activity to deserialize

Then add Deserialize JSON activity to deserialize response from the server.

Then add assign to UpdateId = If(ResultJObj(“result”) IsNot Nothing AndAlso ResultJObj(“result”)(0)(“update_id”) IsNot Nothing, ResultJObj(“result”)(0)(“update_id”).ToString, UpdateId)

Then add assign ChatId and make Breakpoint at this activity to get ChatId from the response in Local Panel via Debug mode.

Telegram Bot Integration With UiPath (Remote Captcha solving) 4
Then assign valid ChatId. My example below:

Finalize UiPath with Telegram process.

Now we know all the necessary items: Token and ChatId. So we can move on to the most interesting part of this guide.

Making keyboard layout for telegram bot

More at https://core.telegram.org/bots/api#inlinekeyboardmarkup 

I chose this type of markup and it has following JSON form – there are two buttons “change” and “speak” which contains appropriate “callback_data”.

Assign KeyboardMarkup = “{“”inline_keyboard””: [[{“”text””: “”change””,””callback_data””: “”change””},{“”text””: “”speak””,””callback_data””: “”speak””}]]}”

Telegram Bot Integration With UiPath (Remote Captcha solving) 5

Send a screenshot with Captcha.

Add “Take screenshot” activity, set it to Captcha image and save image file path to variable CaptchaImagePath:

Telegram Bot Integration With UiPath (Remote Captcha solving) 6
Telegram Bot Integration With UiPath (Remote Captcha solving) 7

Then send it to telegram chat via HTTP Request activity:

Telegram Bot Integration With UiPath (Remote Captcha solving) 8
Telegram Bot Integration With UiPath (Remote Captcha solving) 9
Telegram Bot Integration With UiPath (Remote Captcha solving) 10

In the chat it will look like- 

 

Telegram Bot Integration With UiPath (Remote Captcha solving) 11

Listen to answer from the user

  • Add another HTTP Request Get Updates activity.
  • Add another Deserialize JSON activity.
  • Add Flow Decisions activity to check whether the answer is callback or message: get action if it is a callback and Captcha text if it is a message.
Telegram Bot Integration With UiPath (Remote Captcha solving) 12

The answer is Callback?

ResultJObj(“result”).Count > 0 AndAlso ResultJObj(“result”)(0)(“callback_query”) IsNot Nothing

If it is a callback then save Action:

ResultJObj(“result”)(0)(“callback_query”)(“data”).ToString

The answer is a Message?

ResultJObj(“result”).Count > 0 AndAlso

ResultJObj(“result”)(0)(“message”) IsNot Nothing

If it is a message then save AnswerText:

ResultJObj(“result”)(0)(“message”)(“text”).ToString

 

 

 

React on actions

We have 2 actions: change and speak. So every action is a click to appropriate button:

Telegram Bot Integration With UiPath (Remote Captcha solving) 13
Telegram Bot Integration With UiPath (Remote Captcha solving) 14
Telegram Bot Integration With UiPath (Remote Captcha solving) 15

Then we need to send a message to the user to give feedback:

Telegram Bot Integration With UiPath (Remote Captcha solving) 16
Telegram Bot Integration With UiPath (Remote Captcha solving) 17
Telegram Bot Integration With UiPath (Remote Captcha solving) 18

Then update UpdateId and loop to “Take Screenshot ‘Captcha’”. Workflow will look like this:

Telegram Bot Integration With UiPath (Remote Captcha solving) 19

React on message

Then we need to process getting and typing into Captcha text. If answer is message with Captcha text then we need to save AnswerText from ResultJObj:

 

ResultJObj(“result”)(0)(“message”)(“text”).ToString

 

Then add “Type Into” activity to type this answer to Captcha validation input:

Telegram Bot Integration With UiPath (Remote Captcha solving) 20

Then add “Click” activity to button “Validate”:

Telegram Bot Integration With UiPath (Remote Captcha solving) 21

Then we need to get the result text of validation:

Telegram Bot Integration With UiPath (Remote Captcha solving) 22

Then direct to “HTTP Request sendMessage” activity. If answer is not a message then we need to direct flow to “HTTP Request getUpdates” activity.

Workflow will look like this:

Telegram Bot Integration With UiPath (Remote Captcha solving) 23

Please feel free to reach me in case you are still facing any issues with your Telegram integration with UiPath.

Happy Automation!

Share This Article
1 Comment