Telegram

All about connecting the bot to Telegram

Maximum length of the text message is 4096 symbols, maximum length for a message with a picture (1080x1080px) is 1024 symbols

How to connect the bot to Telegram

The bot in Telegram is created with the help of a different bot by the name of BotFather. You text it the command /newbot, choose the name which will be shown in the contacts list, and the logs of your bot (it must end with bot or _bot)

If the name isn’t taken and is written correctly, BotFather sends a reply with a token - a kind of “key” for accessing the created bot. It needs to be saved and not be shown to anybody.

After that you need to find the section “Messengers and chats” in Salebot, choose Telegram and enter the token you got from BotFather:

Then you press "Done" and you've successfully connected your bot to Telegram!

For now you can only send files with gif-, pdf- and zip- extension

How to work with buttons

pageButtons

How to mesh two types of buttons together

You need to create a block with a reply-button(s), which you need to visualize. For example, [{"type": "reply", "text": "BUTTON NAME", "line": 0, "index_in_line": 0}]

Then you create a block below (timer is set for 0 seconds) with inline buttons which need to be placed without deleting reply-button(s). For example, [{"type": "inline", "text": "BUTTON NAME", "line": 0, "index_in_line": 0}]

The result:

The names of the buttons have to correlate with the “Condition” tab in the message to which the button leads

You have to keep the same button order: first the reply buttons then inline. Otherwise the reply buttons will be lost with the next block

Video tutorial

How to make the bot work in Telegram groups

To make the bot work in Telegram chats or channels you need to:

Add the bot as an administrator; Add this function in BotFather with the /setjoingroups command as shown below:

Which variables are needed for chats

Variables with the information about the message’s author

message_from - identification of the author message_from_name - the author’s name message_from_username - the author’s username in Telegram

Variables with the information about the forwarded message

reply_message_id - the number of the message that’s forwarded reply_from - who is the author of the forwarded message reply_from_name - the name of the person whose message is forwarded reply_from_username - the username of the person whose message is forwarded reply_from_chat_id - the number of the dialogue from which the message was forwarded

Adding and deleting from groups

In this case the callbacks new_chat_member and left_chat_member are activated

chat_member_name - the user’s name chat_member_username - the user’s username chat_member_id - the user’s number

SCREENSHOT

Which Callbacks there are in Telegram channels

new_chat_member - a new user has been added left_chat_member - a user has left the channel kicked_chat_member - a user has been deleted by an admin chat_join_request - you received a new join request edited_channel_post - a channel’s post has been edited (edited_channel_post + the date and the time of the edit)

SCREENSHOT

When you get any of the mentioned callbacks these variables are updated: chat_member_name - the user’s name chat_member_username - the user’s username chat_member_id - the user’s number

How to work with requests for channels/chats

To approve the request to join and to add the user to the channel/chat this function is used:

tg_approve_chat_join_request(platform_id, user_id)

To decline the request to join the channel/chat this function is used instead:

tg_decline_chat_join_request(platform_id, user_id)

How to make a Markdown in Telegram

To create a Markdown in a block you need to open its setting and it the tab “Advanced settings” switch on the “Markdown on Telegram”

If there’s any symbol from the following: '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!' - they need to be shielded by the backslash symbol \

Otherwise the message won’t be sent at all. And it really doesn’t matter which part of the text you're marking

Example: Hi. Great to see you!

Shielding is possible manually as well as in the calculator with the function

txt = tg_escape(s)

Then you enter s - a line with the original text

At the end txt gets a line with now put in slashes where they’re needed

Example:

Then you mark the needed text:

To make the text bold you need to put two asterisks by the sides of it: *hello*

To make it cursive use the underscore: _hello_

An underlined text is made with two underscores by each side: __hello__

A crossed text is made with tildas: ~hello~

A link in the text: [the text in square brackets] (a link in round brackets), e.g.

To mention a Telegram user: [the text in square brackets](a link to the user in round brackets), e.g. [inline mention of a user](tg://user?id=123456789). In the link you can also use this format: #{platform_id}

A text as a code is created by putting backticks (grave accents) by the sides: `inline fixed-width code`.

The example:

How to turn on safe mode for the content

To ensure that the content doesn’t get spread around like a highschool rumor, you can turn on safe mode. To achieve this, you need to find a slider “Protect content” in the advanced settings of the block.

Protected messages cannot be forwarded

How to work in the inline mode

How to work in the inline mode

Inline bots support all types of content available in Telegram. They are capable of sending stickers, videos, music, locations, documents and more.To work in an inline mode, you need to turn on that option in the bot’s settings in BotFather (https://t.me/BotFather). Send the /setinline command to it, then choose the placeholder and the inline settings will be updated.

How to change the placeholder

Later on you can change the placeholder that shows up before the query entry. It’s “Search…” by default.

To change the placeholder you need to use the command /setinline again

How to edit the inference results

After pressing on one of the variants from the list in the inline mode, a message will be sent, that was mentioned in the title of the chosen variant, and you can configure the reaction in the workflow.

To set up data for output in the inline mode, use the inline_bot variable. The search in the inline mode with the values of the variable happens in real time.

The data in variables can be set up three ways. Let’s look into them, starting with the easiest one, to the maximum settings.

An array with text data

For example, the search will be happening in the products array.

The search happens when entering the phrase from the array options. In our case, you type in the letter C, 3 choices come up, then you add an H and the amount goes down to 2.

After pressing the option the client needs, the bot gets the message, that can also be seen in the clients tab on Salebot:

An example of options: ["Milk", "Bread", "Eggs", "Tea", "Chocolate", "Condiments", "Vegetables", "Fruits", "Cheese"]

An array with a button options dictionary

The second variant is more customizable, instead of the text values the array holds dictionaries.

The search for this way happens with the use of the “title” key. Case-insensitive.

Let’s break down a group of parameters for one option (dictionary). We’ll take this one as an example:

{"title": "Payment", "description": "Payment options", "thumb_url": "https://img.icons8.com/dusk/50/000000/card-exchange.png", "message_text": "Payment product 1"}

title - button title description - button description thumb_url - the link to the image message_text - this text gets sent when choosing the option

For one option (dictionary) the only mandatory parameter is the title key with the value that gets sent as a message:

An example of the array:

[{"title": "Basket", "description": "Chosen products", "thumb_url": "https://img.icons8.com/dusk/64/000000/shopping-basket.png"}, {"title": "Delivery", "description": "Delivery options", "thumb_url": "https://img.icons8.com/dusk/64/000000/delivery--v1.png"}, {"title": "Payment", "description": "Payment options", "thumb_url": "https://img.icons8.com/dusk/50/000000/card-exchange.png", "message_text": "Payment product 1"}, {"title": "Settings"}]

A dictionary with the list of all button options

The third variant is fitting to those who want to create menus for set phrases and is convenient with the use of inline-buttons in which the needed phrase is situated.

The search for this variant happens with the full match choice of matching, so the request has to be a full match to the key to which the menu is tied to.

In the example the search will happen with the keys Menu, Posts and Products.

This is the structure:

"key": [button array]

{"search phrase1": [{"title": "Basket", "description": "Chosen products"}, {"title": "Payment", "description": "Payment options", "thumb_url": "#{variable with the url}", "message_text": "Payment options"}, {"title": "Delivery", "description": "Delivery options", "thumb_url": "link to image"}], "search phrase2": [{"title": "First", "description": "Description 1"}, {"title": "Second", "description": "Description 2"}, {"title": "Third", "description": "Description 3"}, {"title": "Fourth", "description": "#{variable}"}], "search phrase3": ["Milk", "Bread", "#{variable}", "Chocolate"]}

The value is the array with dictionaries or the array with lines; you can’t combine them.

Let’s decipher the maximum amount of parameters for one dictionary. We’ll take this one as an example:

{"title": "Payment", "description": "Payment options", "thumb_url": "https://img.icons8.com/dusk/50/000000/card-exchange.png", "message_text": "Payment options"},

title - the button title description - the button description thumb_url - link to image message_text - this text gets sent when choosing this option

As the bare minimum, one dictionary has to contain the key title with its value that gets sent after pressing on that option. Example: {“title”: “Settings”}

The data example:

{"Menu": [{"title": "Basket", "description": "Chosen products", "thumb_url": “https://img.icons8.com/dusk/64/000000/shopping-basket.png"}, {"title": "Payment", "description": "Payment options", "thumb_url": "https://img.icons8.com/dusk/50/000000/card-exchange.png", "message_text": "Payment options"}, {"title": "Delivery", "description": "Delivery options", "thumb_url": "https://img.icons8.com/dusk/64/000000/delivery--v1.png"}], "Posts": [{"title": "First", "description": "Description 1"}, {"title": "Second", "description": "Description 2"}, {"title": "Third", "description": "Description 3"}, {"title": "Fourth", "description": "Description 4"}], "Products": ["Milk", "Bread", "Eggs", "Tea", "Chocolate", "Condiments", "Vegetables", "Fruits", "Cheese"]}

Inline buttons with specified search values

This button works only on Telegram

To specify a search query, you need to add the parameter inline_query to the inline button. It has to hold the value that automatically gets placed into the query.

It’s recommended to use this line of work for inline mode, it lets you avoid errors and it works faster.

For example, let’s add three buttons that correspond to the example from the previous chapter:

[{"line":0,"index_in_line":0,"text":"Show me the menu","type":"inline","inline_query":"Menu"}, {"line":0,"index_in_line":1,"text":"Posts","type":"inline","inline_query":"Posts"}, {"line":2,"index_in_line":0,"text":"List of products","type":"inline","inline_query":"Products"}]

When pressing a button, the list of buttons will open up if you specify the inline_query for this button.

Notes

The variable inline_bot can be set not only in the “Shared valuables” tab in settings, but also in the Calculator tab in the block’s settings, you just need to put in apostrophes.

Here’s an example:

inline_bot = ‘["Milk", "Bread", "Eggs", "Tea", "Chocolate", "Condiments", "Vegetables", "Fruits", "Cheese"]’

You can also set any value as a variable for a more flexible configuration. Example:

How to create a poll in Telegram

A simple poll

To create a simple poll, this function is used:

tg_send_poll(platform_id, question, options, is_anonymous, allows_multiple_answers, reply_markup, disable_notification, protect_content, token)

Mandatory parameters: platform_id - Telegram id to which you need to send the message question - the poll question options - the poll options array

Optional parameters: is_anonymous - 1 - anonymous poll, ‘’ - public allows_multiple_answers - 1 - multiple answers are allowed, ‘’ - only one answer reply_markup - 1 - keyboard, ‘’ - no keyboard disable_notification - 1 - enabled notifications, ‘’ - disabled notifications protect_content - 1 - protect from copying or screenshotting, ‘’ - no protection token - the bot’s token if it’s not transferred, then the current one is used

Example:

options = [“blue”, “red”, “yellow”, “purple”, “green”] r = tg_send_poll(platform_id, ‘Your favorite color?’, options, 1, ‘’, ‘’, 1, ‘’)

Notes

1. The function returns an answer from Telegram that has message_id. It’s better to save it, because with its help you’ll be able to stop the poll with the tg_stop_poll function (described below) and get a result.

2. If the poll was added to a channel, then you’ll get a callback: poll_added - unchanged part, followed with whatever question the poll presents

If the poll was added to the chat, then additionally some numbers will be added - this is the id of the Telegram user that added the poll.

When the poll was created by the bot, you won’t get the callback.

3. You can only create anonymous polls in channels.

4. If the poll was sent to the dialogue of a user and the bot, then when the user answers, a callback will be shown in the Salebot chat with the chosen answer(s).

The numbering starts at 0.

poll_answer 5197454583336534137 [4]

poll_answer - unchanged part 5197454583336534137 - poll id [4] - answer

5. If the public poll was created in a group (it doesn’t matter who by - the function or a user) in which the bot is a participant, then the bot sends to each vote a webhook, and when you get it, the callback from point 4 will be sent to the dialogue with a client. If the client had no contact with the bot prior to it, then it’s not possible to send anything as a reply until the client activates the bot.

Attention! It’s recommended to send only anonymous polls to the groupchats!

6. After creating the poll, save its id in the variable to understand to which poll the callback came to.

Quiz

send_quiz_poll(platform_id, question, options, explanation, correct_option_id, is_anonymous, reply_markup, parse_mode, protect_content, disable_notification, token)

Mandatory parameters: platform_id - Telegram id to which you need to send the message question - the poll question options - the poll options array explanation - the text that gets shown when the user chooses a wrong variant or preses on a lightbulb icon, 0-200 symbols with no more than 2 line feeds correct_option_id - the number of the right option, starts at the number 1

Optional parameters: is_anonymous - 1 - anonymous poll, ‘’ - public reply_markup - 1 - keyboard, ‘’ - no keyboard parse_mode - markdown or html for an explanation, ‘’ - without formatting disable_notification - 1 - enabled notifications, ‘’ - disabled notifications protect_content - 1 - protect from copying or screenshotting, ‘’ - no protection token - the bot’s token if it’s not transferred, then the current one is used

Example:

options = ["white", "red", "blue", "green"] r = tg_send_quiz_poll(platform_id, 'What color is a crocodile?', options, 'Here is the explanation', 4, '', '', '', '', 1)

Notes are the same as in the Poll section.

How to stop the poll

When a poll or a quiz ends with this function, the dictionary with poll results comes as a reply.

To stop the poll or the quiz, you need to call the function tg_stop_poll result = tg_stop_poll(platform_id, message_id)

platform_id - Telegram id where the poll is held (user/channel/group id) message_id - id of the message with the poll/quiz

After the execution of the function, the dictionary gets pasted into result, you can parse it with the function get():

{'update_id': 787499908, 'poll': {'id': '5258177312003719338', 'question': 'The question?', 'options': [{'text': Answer 1', 'voter_count': 1}, {'text': Answer 2', 'voter_count': 1}, {'text': Answer 3', 'voter_count': 0}], 'total_voter_count': 2, 'is_closed': True, 'is_anonymous': True, 'type': 'regular', 'allows_multiple_answers': False}}

How to get the full webhook from Telegram

To get the full webhook, it’s enough to assign any value to the variable save_webhook (the variable can be both the project’s constant and the transaction’s).

Additionally, the Telegram’s request will be written down in the variable tg_request.

How to automate the deletion process of systemic notifications in groups

For automatic deletion it’s enough to assign any value to the variable del_tg_system_messages (the variable can be both the project’s constant and the transaction’s) and allow the bot to delete messages.

These types of systemic messages are deleted:

  • Edited channel name

  • Edited group photo

  • Deleted group photo

  • Pinned message

  • User joined group

  • User left group

API Telegram (functions for using everything Telegram is capable of)

Telegram’s built-in pay system

How to create a menu in Telegram

Last updated