Telegram Bot Integration With UiPath (Remote Captcha solving)

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.

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 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.

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”

Our  Intial Workflow will look like this

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.

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””}]]}”

Send a screenshot with Captcha.

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

Then send it to telegram chat via HTTP Request activity:

In the chat it will look like- 

 

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.

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:

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

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

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:

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

Then we need to get the result text of validation:

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:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Exit mobile version