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:
- Establishes a connection to Telegram.
- Retrieves a list of contacts.
- Exports a list of all chats.
- Exports messages from a group chat.
- Exports messages from a chat with a user.
- Sends a message, sends a file.
- 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):
- Block "Start" (any diagram starts from this block).
- 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".
- 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)
- 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.
- 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).
- 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.
- 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).
- 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.
- 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).
- 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).
- 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.
- 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).
- 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.
- 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).
- 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.
- 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).
- 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).
- 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).
- 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).
- 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).
- Block "End" (this block concludes the execution of the scenario or returns the subprocess diagram to the main process).