# Telegram client

Let's consider an example of a bot that demonstrates working with Telegram as a client.

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

The robot project consists of one diagram and a file in .mp3 format. To describe step by step, this robot works as follows:

1. Establishes a connection to Telegram.
2. Retrieves a list of contacts.
3. Exports a list of all chats.
4. Exports messages from a group chat.
5. Exports messages from a chat with a user.
6. Sends a message, sends a file.
7. Receives messages. (If the message contains a file, it saves it; if it only contains text, it displays a dialog box with that text).

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

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

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

1. **Block "Start"** (any diagram starts from this block).
2. **Block "Client. Create Connection"** allows you to create a connection to Telegram as a client. The following properties are specified for this block:

* Session name (session name);
* Timeout (timeout for a response from the Telegram server in seconds).

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

**Important!** *To maintain authorization, you must specify a unique session name and subsequently use it for connection. Upon the first launch, an authorization form will open. To authorize, you will need to enter your phone number, then the service will send a code that will arrive in the already connected Telegram client (for example, on your phone). Then enter this code and click "Authorize".*

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfEBZsUP-SV1SWUbHcDE-H-1eS6DrFiQB1AbVzKF-F9xrokYEjsoW__6v8JjZautTeswHc0OBcIN4RLz_LtW-GtNH3SnIpKJd7520_jxrpahSh0hNPNbJtuwMZYqTdXb6L9lDqJ?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

3. **Block "Client. List of Contacts"** returns a list of contacts. For this block, one property "Connection" is specified (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client). The output is the property "Contacts" (a data table containing the list of contacts. The table contains the following columns: Id, Username, FirstName, LastName, Phone)

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcFmpytElc8Xy4c6aZA8WYcTNhUVQuXkY40AMS9LGhq2MPBgNlaIlHIK4WmWtbupps65C3AURVM_uFHjtF_AG9A3iLBYcpbmDPGe-UXmabGcQZYqewA4lg7Fo1lBgTaWerAA5w9?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

4. **Block "Log"** allows you to output arbitrary messages and/or variable values to the log during the robot's scenario execution. For this block, the property "Value" is specified. A text constant is indicated in quotes, and the variable name starts with the symbol $. That is, it outputs the variable $Contacts to the log.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdTm9JP5hlelaZkmrQ7vnBEMwFWkyIVyosu8c3FYRFzJvGsKKNfwmszkFm4NZM6dxra4zgB3Qf4djuodwU8qQNHgbxBbEkJjGi5E_4pU_BRPEzcP-WYvw3nHANaqLmdsweLVU3rWg?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

5. **Block "Client. Export Messages"** allows you to export messages from a chat.

**Note:** *messages are exported from the last one, i.e., from bottom to top.*

For this block, the following properties are specified:

* Connection (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client);
* Destination (the destination object. This string can contain the following values: Chat Id, chat name, user Id, combination "FirstName LastName" of the user, user's Username value, user's phone number);
* Offset (the message identifier from which the export starts. The default value is set to 0, which means loading messages from the very last one);
* Count (limit on the number of messages exported at one time).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXe9-CNn93huf-1vZ7rLAFMHMO2lC649PY2vTt3_6x9nBzVM1JgQevcKnIEWM4mLOFjG6d0NEoyLpL-3ik_2oHn3GGo63WJI1Za8ouJxDAVfWr9tup96rYfyAyVUe4cNgXwy7iNr?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

6. **Block "Log"** allows you to output arbitrary messages and/or variable values to the log during the robot's scenario execution. For this block, the property "Value" is specified. A text constant is indicated in quotes, and the variable name starts with the symbol $. That is, it outputs the variable $Chats to the log.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfQQo3GkoKwgmDd9S5hKt8lAiMlLmcp_RzFLrNaYRdnOjtuYp0t7cA3561AtCZzHnQ54blT1YuY3C6QoGJuAiel0of3IZfRCOMljyf6SgTAZ6A9-gDQiMrWDeneN8CYwvVlPRBeYA?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

7. **Block "Client. Export Messages"** allows you to export messages from a chat. For this block, the following properties are specified:

* Connection (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client);
* Destination (the destination object. This string can contain the following values: Chat Id, chat name, user Id, combination "FirstName LastName" of the user, user's Username value, user's phone number);
* Offset (the message identifier from which the export starts. The default value is set to 0, which means loading messages from the very last one);
* Count (limit on the number of messages exported at one time).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcQj6Lv06tw9iLK88FHkpy5LOeRb-L8Uj8Od1eTZwarIX0BIbuBNENnPPx-4n4pd1sAQ7HZUuq6mQblRdjNH-zXmatKJ7u8rhRd5HDuwIU7iFiTV0iLBunWAMGREPdL-at1_m1A?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

8. **Block "Log"** allows you to output arbitrary messages and/or variable values to the log during the robot's scenario execution. For this block, the property "Value" is specified. A text constant is indicated in quotes, and the variable name starts with the symbol $. That is, it outputs the variable $Messages to the log.

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

9. **Block "Client. Send Message"** allows you to send a text message. For this block, the following properties are specified:

* Connection (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client);
* Destination (the destination object. This string can contain the following values: Chat Id, chat name, user Id, combination "FirstName LastName" of the user, user's Username value, user's phone number);
* Allow import (the set flag allows an attempt to import the user into contacts if they are not found. This feature is available if the "Destination" is set to the phone number registered in Telegram. Only after this will it be possible to send messages to the user via their phone);
* Text (the message text).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeOoFYEfytgJ4BR1BQroum0nIktd_BYAQiKW55b3l2ZCTvMh5qqQzlusdIMG8b6AOe7Bo4frQL9J77j1W0JfsFE0v1-tXfecj5sshRC8Ljd0_3KGuCsrG7ZimTe8ti3VCDayA8q?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

10. **Block "Client. Send File"** allows you to send a file, in this case "test.mp3". For this block, the following properties are specified:

* Connection (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client);
* Destination (the destination object. This string can contain the following values: Chat Id, chat name, user Id, combination "FirstName LastName" of the user, user's Username value, user's phone number);
* File name (the name and path of the file that will be sent).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcDeF2FmKsYLyOQ7bjwAOFqB7QhxPtID-qJFmRoWVlDcs4ofGK9qHnXJyywvkj8zcm1Q9nBmymD_uYtZB2DRmwgqYhuU_YKbt_ETaA5D5Oiq49XzZN6HUOmGM5LQ-RYrrtTH3GD_A?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

11. **Block "Client. Get Message"** allows you to get a message from the queue. For this block, the property "Connection" is specified (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client). The output is "Message" (returns the message object. Available properties: Id - message id; Date - date; From - who the message is from; To - who the message is to; FromId - Id of the user the message is from; Title - chat name; Text - message text; Type - text designation of the message type; IsFile - indicates whether there is a file in the message (here you can more precisely determine the type using the properties IsDocument, IsPhoto). For example: $TMessage.IsPhoto - if equal to $true, then the message contains a photo)

**Note:** *if there are no messages in the queue, an error is generated.*

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfsjRn2XJ3nNtydpuBaJvRNQIOSpiwwpz86-KoATce7IVgRq667HzS-Mo-aOhBsLlMN5ioYrurcCEIvFoynBZvxQawS7Xxi9uPXeON17IYvD9WfN_b-jzVtVKN3sFbT4WpuzgqHBw?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

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

* 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_4nXd9_866ERuDG15RzCLhFGvwAurbzQAI0lzi9APfQxHC5g66TuWRboUb4vLrbDTjM8ZjH2KNMUGWZbhripKqrBoFxXdrV-YsF1gBW-lxGc9GPEIfcx9ORIzoGdTOKV8PlG1x_u6eaQ?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

13. **Block "Condition"** checks the specified condition for truthfulness, after which the execution of the scenario continues towards the "Yes" output (if the condition is met) or towards the "No" output (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 output is "Yes", otherwise – the output is "No".

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

In this case, the condition is set as: $TMessage.Text=="stop". That is, whether the word "stop" is in the message text.

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfetvGdQh1RBAn6_QxmnDu-YVINpLpUETdL-eow5ssPae1kTPCXSeBF8tPVaAJprEsdJbMr_cae656kWQeOcmhS0_Ny6cBJzxBUYRxwYM4tuLyUifBt59Hzi6FYc_AgYyFBVOCFaQ?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

14. **Block "Client. Close Connection"** allows you to close the client connection. For this block, one property is specified – "Connection" (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXdDa91e0EquIwRY1ZntFjdRf5dC-91hn3LUqjacjJ_L3kgQbXERDi2zFdxSW3AViUySJIkMjF6Q-SXQdBuwq7-C6RXfCMz1yfhZBheOSsvXrQK7rZIwM1z0xYAMx7mQrdiCmk0p7A?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

15. **Block "Condition"** checks the specified condition for truthfulness, after which the execution of the scenario continues towards the "Yes" output (if the condition is met) or towards the "No" output (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 output is "Yes", otherwise – the output is "No".

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

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

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXcBPBjszGJWlbzTtIY2DzwI2DmLC1pbJaUf89xAsqfdGKq2hXY59IpTo1eKNVZa8XK32A-i8TocRCGtlbknVBzYevfEIc35L7MXo6UB3Lam3orqoGFp8Qmz5IlW2yOKZ_r9myG5wQ?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

16. **Block "Create File"** allows you to create a new text file. For this block, the property "Path" is specified (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 taken. If a folder is specified, it will be concatenated with the file name).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXebWNvbfIE32GYSqXxsRrKU4a1Hdjw3V9Rd5TLyLTeMmupf_W5BduuupvEabnxUsOt2UF1UxCriF0W6nrGmT6W9W-3byWRxctuI9qkIEpyGdyBZD3jzljB7gezFvjS3oHCoM-xRVQ?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

17. **Block "Client. Download File"** allows you to download a file from a message. For this block, the following properties are specified:

* Connection (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client);
* Message (the message object. The variable name in this property must match the variable name in the "Message" property of the "Client. Get Message" block or from the array property "Messages" of the "Client. Export Messages" block from the Telegram group);
* File name (the name and path of the file where the file from the message will be saved).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXfPN92M1HrbsqIEV40vw68T092xf361Q3FkF0xT6r7SlUXNMldo_Cm_RTcrLcwmQk8jOhLn1LuWaV5UYpCKddQzuAbjwGDRiDLbl3Bs_NUSS5HF_DM7BRc0rvyUi4h_Nc-ycmrqNA?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

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

* 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 by the variable $TMessage.Text).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXeTNB8BIkiHjnSlsNozPvyQsnuO52Eu1o7Guv-p2NE8WhK2sGA7qjIaVklMBSTS-4kKheOJzRsqLu6uLkccUb28dCZmASx_G21mBUY-LSjGuQOWB_XoRfNUqYd7ixldGDxLmtUHhg?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

19. **Block "Client. Send Message"** allows you to send a text message. For this block, the following properties are specified:

* Connection (the client connection object. The variable name in this property must match the variable name in the "Connection" property of the "Client. Create Connection" block from the Telegram group that was previously used to connect the Telegram client);
* Destination (the destination object. This string can contain the following values: Chat Id, chat name, user Id, combination "FirstName LastName" of the user, user's Username value, user's phone number);
* Allow import (the set flag allows an attempt to import the user into contacts if they are not found. This feature is available if the "Destination" is set to the phone number registered in Telegram. Only after this will it be possible to send messages to the user via their phone);
* Text (the message text).

<figure><img src="https://lh7-rt.googleusercontent.com/docsz/AD_4nXendbYfPyL-NgLO_bfP99aCrSG6wmZ9QK2wYIcEPTqjGwTfQYJ_uEyLS57dFbYvBktFRPTzBXMOF7GT6sc7z5T84A6oc_LmDikt1iV6_gbNFugs0x7fe5sediX6Clck_mPlE0gB?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

20. **Block "Pause"** pauses the execution of the scenario for a specified time. For this block, the following properties are specified:

* 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_4nXftEIQowakugkMdWOm3fC9RPFqtGaG0i1iYH80Wu2ox43IyRNGydqS-iPCcRM7JshtzEehYlGvQo15C--5kSMoAXBPdJEOqeWJIbcCtTS0hRHLLnNAr8ULwklEbda40dsm8MlpJUg?key=eIuXXe1wcakZ5HVC8JFqa2bC" alt=""><figcaption></figcaption></figure>

21. **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-client.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.
