# Telegram Bot

Let's consider an example of a bot that demonstrates working with a Telegram bot:

* receiving messages from the bot;
* replying to a message;
* saving the sent file in the project folder.

**Note:** *You can view the creation of a Telegram bot in the video instructions for the block "Bot. Create Connection".*

**Important!** *Connection data for Telegram can be found at: c:\Users\User\AppData\Roaming\Sherpa RPA Data\Telegram\\*

Video instructions for creating a bot in Telegram: <http://sherparpa.ru/videos/ru/telegrambot.mp4>

**The main project diagram** looks like this (for convenience, the blocks of the diagram are numbered):

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcrtMI_AbXcmjfypr9h3DEkgqri42Ac_wkH3uEjxyEBRQe2HX0r74fBZlQtXsuE3vTrm5SqJvgJKS1rPwKuhiEqKnimQ48VdG8I4pSoIoP2LfA9s0c_Eco989r_tacSvNPNsrx7Iw?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

1. **Block "Start"** (any diagram starts from this block).
2. **Block "Bot. Create Connection"** allows you to create connections for the Telegram bot. The following properties are specified for this block:

* Bot token (the bot token that can be obtained from BotFather in the Telegram client);
* Timeout (the timeout for a response from the Telegram server in seconds).

The output is a "Connection" – a variable with the current connection object to Telegram. In other blocks, this variable must be specified.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcfO_b95eQxdP5FC7N-w7MOMPuLFZirldO1epYLQMLsDIkKxLy9zK37q4sk2nAasMfF_QaQxHgMW2Py4LRmOD6x5ZTOE85656ZgoIu8GpQ8MaFqm5S-jvz4TA25DDmlaKxi85C0Qg?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

3. **Block "Bot. Get Message"** allows you to receive a message or callback from the queue. If there are no messages in the queue, an error is generated. The property "Connection" is specified for this block (the connection object to the bot. The variable name in this property must match the variable name in the "Connection" property of the "Bot. Create Connection" block from the Telegram group that was previously used to connect to the Telegram bot).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdpg9y0oRd4iaFqVd9YvOWexDnP4KGuINrM-DWMAQc9kxUORPOANKz33USwPnRWCKqAfmCvaigsdamHd7ieBIcqNIBGBNSyZqP68LXQiw8icyYekVbjzGDJg4RUipylIiXX9qqzgw?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

4. **Block "Condition"** checks the specified condition for truth, after which the scenario execution continues towards the "Yes" exit (if the condition is met) or towards the "No" exit (if the condition is not met).

The condition is written in the format: “variable” equals (==)/ greater than (>)/ less than (<) “value”.

For example: $a == “Hello”, that is, if the value of the variable $a is equal to “Hello”, then the exit is “Yes”, otherwise – the exit is “No”.

$Result > 5, that is, if the value of the variable $Result is less than 5, then the exit is “Yes”, otherwise – the exit is “No”.

In this case, the condition is set as: $TMessage.Text=="stop". That is, the user wrote "stop" to the Telegram bot.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfG89LvD9netT91TdgCzoijpUe2qrHzJN4T45KMY9jGY16x7yxTA7ecaVHRZCqRBVFJDkgWrkBge8H5dtfrzaXv4MlbPkWDidFG9dTUL0zt4McO0vXUo7ejKlQclrWeDWtnK_X0HQ?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

5. **Block "Bot. Close Connection"** allows you to close the connection to the Telegram bot. The property "Connection" is specified for this block (the connection object to the bot. The variable name in this property must match the variable name in the "Connection" property of the "Bot. Create Connection" block from the Telegram group that was previously used to connect to the Telegram bot).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcSbe-LNHt0JJrgvu3Llfz1rOJ77tJxOPnwtE7TQXxVycGxLeXcrN720tIQnWRUbyzSPeAsgDEZUgBsbWQ7YW6dfG3JqEr0JqkTWtj9PCBRUtpJiD917_6enV2Xpi6DGkFAmFLrcA?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

6. **Block "Pause"** suspends the execution of the scenario for a specified time. The following properties are specified for this block:

* Unit of measurement (the unit of measurement in which the duration of the pause is specified);
* Duration (the value of the time interval in the specified units of measurement);
* Start immediately (the set flag enables the application of the delay immediately on the first pass of this block from the moment the robot scenario is launched).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdt4ggzf2NyZ45dBTSIeFskhtQsielfmlRGlIvpeme07d7g__9BPmSIL3ipbczNIAF1SCcDoAZ1nFIFwURUIwRkaT8Bl5O6SU025luiWICGzYPXrXEN4a3vW9XS7qzs5xCyMD-NfQ?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

7. **Block "Condition"** checks the specified condition for truth, after which the scenario execution continues towards the "Yes" exit (if the condition is met) or towards the "No" exit (if the condition is not met).

The condition is written in the format: “variable” equals (==)/ greater than (>)/ less than (<) “value”.

For example: $a == “Hello”, that is, if the value of the variable $a is equal to “Hello”, then the exit is “Yes”, otherwise – the exit is “No”.

$Result > 5, that is, if the value of the variable $Result is less than 5, then the exit is “Yes”, otherwise – the exit is “No”.

In this case, the condition is set as: $TMessage.IsFile. That is, it checks if there is an attached file in the message.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXf55H5o9boIuS6tlc5bq_hbKELOggDBdCMGfdgPNNQ5LhHyghnaHumeWXG591dIgnzuGW1CqDf5p4tT2gRjQ6XlPMeN6ccFvx8QxQkrmHktcWn1-iA9B-hbYbObIMa4DyhY4kAb?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

8. **Block "Create File"** allows you to create a new text file. The property "Path" is specified for this block (the path to the folder where the required file is located. If this property is left empty, the folder where the project is located will be used. If a folder is specified, it will be concatenated with the file name).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdP5CmVRgbZaGxjj52i9X_rqq7xTkvywKd3bH1WnhqUODVZ16Ea5bzUlQyjBIncvO-rXYjLcT_YfXPW57hcZFGuKZdCLyhaOTASMDhg2zbQeGKRmrJ6Xroh86_fRCNCQztMC1Ogjw?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

9. **Block "Bot. Download File"** allows you to download a file from a message. The following properties are specified for this block:

* Connection (the connection object to the bot. The variable name in this property must match the variable name in the "Connection" property of the "Bot. Create Connection" block from the Telegram group that was previously used to connect to the Telegram bot);
* Message (the message object. The variable name in this property must match the variable name in the "Message" property of the "Bot. Get Message" block from the Telegram group);
* File name (the name and path of the file where the file from the message will be saved. Voice messages must be downloaded in .ogg format).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXd2HeJ40DAzTnyL-BhnbpGjM4v64Lh95hw_HofY7X0vmMedcIOiqaUDbyzPag9IXwP6kGndooUPTDNOTjJuc4eOorMCZRyei_wFIxYuFN6zKsOWipAZIRSvPEILCjABazrbS_Z99g?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

10. **Block "Text Input Window"** allows you to show a modal dialog window designed to receive information from the user. The following properties are specified for this block:

* Title (the text that will be displayed in the title of the dialog window. In this case: “Telegram”);
* Message (the text that will be displayed inside the dialog window. In this case, it is set to the variable $TMessage.Text).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcfdOU-RXo77_qIaIW8V8L3mzsis2Nx3kH-weV3yf1uJV4utpNsyHrJXJCYf_vZKDMRRRTUJ5-rfs4F-hLA5cGjBWyGnIwZ7e_1M0vSkMbtB2tVZ1Nqia84OC69ULzBR5hWj5Hg9w?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

11. **Block "Bot. Send Message"** allows you to send a text message to the chat. The following properties are specified for this block:

* Connection (the connection object to the bot. The variable name in this property must match the variable name in the "Connection" property of the "Bot. Create Connection" block from the Telegram group that was previously used to connect to the Telegram bot);
* Chat ID (Chat ID. This value is obtained from the "Bot. Get Message" block in the "Message" property);
* Message ID (the ID of the message to which a reply is required. This value can be obtained from the "Bot. Get Message" block in the "Message" property);
* Text (the text of the message).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdkQp9_WTCRrDV8fQuTSGBka9BfnNY2EdfCzU9NQGAQAvqXruZTyye-TEGp1w8NQr-aq-1f0QW0YWkD5wH9D9Sj9Tc9pg4drCAMIZ8YwExtxWLVLz5DxMAnY8FF5sapG1x1XpLD?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

12. **Block "Pause"** suspends the execution of the scenario for a specified time. The following properties are specified for this block:

* Unit of measurement (the unit of measurement in which the duration of the pause is specified);
* Duration (the value of the time interval in the specified units of measurement);
* Start immediately (the set flag enables the application of the delay immediately on the first pass of this block from the moment the robot scenario is launched).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdDVVa1MB9t37-KMFfEUcRUDXKIycu-MA5v8N0YJEmJ1xqBhGVTAIPFenesGSSpTS1cS1f0RrzAVM2WkZ0fHvLwotVpz4C5NWTlKveKHu6hIaMyGPtLlee9Ofna7_bEpbN9b1AlNw?key=30LNZh35UC4eIWihhlpMcBJd" alt=""><figcaption></figcaption></figure>

13. **Block "End"** (this block concludes the execution of the scenario or returns the subprocess diagram to the main process).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sherparpa.ru/en/sherpa-rpa/sherpa-designer/primery-robotov/telegram-bot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
