Functions for sending and editing messages

How to send messages using a Telegram Business account

tg_send_message(platform_id, text,client_message_id, reply_markup, parse_mode, disable_web_page_preview, protect_content, disable_notification, message_thread_id, entities)

Parameters:

Parameter
Description

! platform_id

Telegram user ID to which a message should be sent *

! text

Message text

client_message_id

Message ID to be quoted

reply_markup

Button settings **

parse_mode

Bold and italic text formatting in the description ***. It can have values such as html, markdown, markdownV2.

disable_web_page_preview

Show link preview. To disable it, pass 1; otherwise, pass 0 or leave it empty "".

protect_content

Content protection flag. Pass any value except 0, False, or '' to enable.

disable_notification

Sound notification flag (default: 0). Pass 1 to disable notification, 0 to enable it.

message_thread_id

Topic ID (available for supergroups with forum functionality enabled)

entities

This allows you to copy pre-formatted text along with all its styles and simply specify the character range where a particular font should be applied. An example can be found in the tg_request under the corresponding field. The parameter must be a dictionary. See the example tab for reference.

Detailed example

Let’s consider a simple example with a set of required parameters:

The platform_id specifies the identifier of a specific client.

See the same example, but using variables:

In this example, the variable soob will contain the server’s response after sending a message.

If you save the message_id from the received response, it will allow you to later work with this message (edit, delete, forward, comment).

Difficulties often arise when using all the parameters. Let’s consider the following example:

  • First, declare all the parameters used in the function. Remember, parameters can be passed not only as values but also as variables, which is often more convenient and clearer. Variables such as platform_id and client_message_id can be obtained from the client’s profile card. platform_id — Telegram client ID to which the message should be sent * >We will reply in the same chat where the client writes

    text - message text. >We use text formatting - for example, bold highlighting.

    client_message_id - Message ID to be quoted >In chats, this variable is assigned its value automatically. reply_markup — button settings **. >Let’s assign it to the variable opts. parse_mode — Bold and italic text formatting in the description ***. It can have values such as html, markdown, markdownV2. The characters used for formatting message text are described here. >Let's use markdown. disable_web_page_preview - Show link preview. To disable it, pass 1; otherwise, pass 0 or leave it empty "". >We can pass any value since the message text does not contain a link. protect_content — Content protection flag. Pass any value except 0, False, or '' to enable. >We don’t need content protection, so we’ll pass an empty string ''. disable_notification — Sound notification flag (default: 0). Pass 1 to disable notification, 0 to enable it. >A notification is a pop-up window displaying the message text. Let’s enable it.

  • Next, we assemble the function. Remember to assign the function to a variable — this will allow you to track the message sending status.

The "Calculator" field

Here’s what we got: After the client sends us the keyword test, we reply by quoting his message.

In ok we see the sending status; next comes information about the message itself — its ID, sender data, and content.

Example with the entities parameter You can store the original string in a variable, as shown below:

text = "qwert asdfg zxcvb poiuy lkjhg 12345" You should write the parameter as a dictionary with the data and specify the desired formatting by indicating the fonts:

entities = [{"offset":0,"length":5,"type":"bold"},{"offset":6,"length":4,"type":"text_link","url":" https://mavibot.ai"},{"offset":11,"length":9,"type":"strikethrough"},{"offset":21,"length":6,"type":"spoiler"},{"offset":29,"length":12,"type":"code"}]

Pass the parameter last in the function you use. The parameter can be passed to both the tg_send_message and tg_send_message_1 functions:

x = tg_send_message(platform_id, text, None, None, None, False, False, False, None, entities)

To assign text with line breaks to a variable, specify the value as follows:

text = "First line of text" + "\n" + "Second line of text" + "\n" + "Third line"

How to send a message specifying a particular Telegram bot

tg_send_message_1(token, platform_id, text, client_message_id, reply_markup, parse_mode, disable_web_page_preview, protect_content, disable_notification, message_thread_id, entities, business_connection_id)

Parameter
Description

! token

Telegram bot token obtained from BotFather

! platform_id

Telegram user ID to which a message should be sent *

! text

Message text

client_message_id

Message ID to be quoted

reply_markup

Button settings **

parse_mode

Bold and italic text formatting in the description ***. It can have values such as html, markdown, markdownV2.

disable_web_page_preview

Show link preview. To disable it, pass 1; otherwise, pass 0 or leave it empty "".

protect_content

Content protection flag. Pass any value except 0, False, or '' to enable.

disable_notification

Sound notification flag (default: 0). Pass 1 to disable notification, 0 to enable it.

message_thread_id

Topic ID (available for supergroups with forum functionality enabled)

entities

This allows you to copy pre-formatted text along with all its styles and simply specify the character range where a particular font should be applied. An example can be found in the tg_request under the corresponding field. The parameter must be a dictionary.

business_connection_id

The Business ID value is used when connecting the bot and displayed in channels. It should be provided if a bot token is included in the parameters and the message needs to be sent through a user account linked to the bot.

Example

Example of passing the parameter: entities = [{"offset":0,"length":5,"type":"bold"},{"offset":6,"length":4,"type":"text_link","url":"https://mavibot.ai"},{"offset":11,"length":9,"type":"strikethrough"},{"offset":21,"length":6,"type":"spoiler"},{"offset":29,"length":12,"type":"code"}]

The example shows only the dictionary, while the message text itself is assigned to a separate variable.

How to edit text in Telegram message

tg_edit_message_text(platform_id, message_id, text, reply_markup, parse_mode, disable_web_page_preview, entities)

Параметр
Описание

! platform_id

Telegram user ID to which a message should be sent *

! message_id

Message ID to be edited. This ID must be saved beforehand when sending the message.

! text

Message text

reply_markup

Button settings **

parse_mode

Bold and italic text formatting in the description ***. It can have values such as html, markdown, markdownV2.

disable_web_page_preview

Show link preview. To disable it, pass 1; otherwise, pass 0 or leave it empty "".

entities

This allows you to copy pre-formatted text along with all its styles and simply specify the character range where a particular font should be applied. An example can be found in the tg_request under the corresponding field. The parameter must be a dictionary.

How to send a reaction to a message

tg_set_reaction(platform_id, message_id, reaction)

Parameter
Description

! platform_id

Telegram chat ID

! message_id

Message ID

! reaction

The required reaction is passed as a string.

Example

Code example for copying:

react = tg_set_reaction(platform_id, 1556, '👌')

Example in the calculator:

How to edit an attachment description

tg_edit_message_caption(platform_id, message_id, caption, reply_markup, parse_mode, entities, show_caption_above_media)

Parameter
Descritption

! platform_id

Telegram user ID to which a message should be sent *

! message_id

Message ID to be edited

! caption

Description text

reply_markup

Button settings **

parse_mode

Bold and italic text formatting in the description ***. It can have values such as html, markdown, markdownV2.

entities

This allows you to copy pre-formatted text along with all its styles and simply specify the character range where a particular font should be applied. An example can be found in the tg_request under the corresponding field. The parameter must be a dictionary. Example of passing the parameter: entities = [{"offset":0,"length":5,"type":"bold"},{"offset":6,"length":4,"type":"text_link","url":"https://,mavibot.ai"},{"offset":11,"length":9,"type":"strikethrough"},{"offset":21,"length":6,"type":"spoiler"},{"offset":29,"length":12,"type":"code"}]

The example shows only the dictionary, while the message text itself is assigned to a separate variable.

show_caption_above_media

Takes the value True; if this parameter is specified, the message text will be displayed above the attachment.

How to edit media attachments in a message

Parameter
Description

! platform_id

Telegram user ID to which a message should be sent *

! message_id

Message ID to be edited. This ID must be saved beforehand when sending the message.

! media

A dictionary describing the media file: Example of a JSON dictionary to replace a previously sent photo: media = '{"type": "photo", "media": "<file_to_send>"}'

where <file_to_send> is recommended to be the file_id obtained via the full Telegram webhook.

For more details, the parameters for the dictionary are described in the official Telegram documentation.

reply_markup

Button settings **

How to edit an inline keyboard in a message

Parameter
Description

! platform_id

Telegram user ID to which a message should be sent *

! message_id

Message ID to be edited. This ID must be saved beforehand when sending the message.

! reply_markup

Button settings **

Example: message editing with Telegram API

You can find a detailed example of working with Telegram API functions for editing messages below.

How to copy a message

tg_copy_message(platform_id, from_chat_id, message_id, reply_to_message_id, reply_markup, parse_mode, protect_content, disable_notification, caption, message_thread_id, entities, show_caption_above_media)

Parameter
Description

! platform_id

Telegram user ID TO which a message should be copied *

! from_chat_id

Telegram user ID FROM which a message should be copied *

! message_id

Message ID to be copied

reply_to_message_id

Original message ID if the copied message is a comment

reply_markup

Button settings **

parse_mode

Bold and italic text formatting in the description ***.

It can have values such as html, markdown, markdownV2.

protect_content

Content protection flag. Pass any value except 0, False, or '' to enable.

disable_notification

Sound notification flag (default: 0). Pass 1 to disable notification, 0 to enable it.

caption -

Description up to 1024 characters.

message_thread_id

Topic ID (available for supergroups with forum functionality enabled)

entities

This allows you to copy pre-formatted text along with all its styles and simply specify the character range where a particular font should be applied. An example can be found in the tg_request under the corresponding field. The parameter must be a dictionary. Example of passing the parameter: entities = [{"offset":0,"length":5,"type":"bold"},{"offset":6,"length":4,"type":"text_link","url":"https://mavibot.ai"},{"offset":11,"length":9,"type":"strikethrough"},{"offset":21,"length":6,"type":"spoiler"},{"offset":29,"length":12,"type":"code"}] The example shows only the dictionary, while the message text itself is assigned to a separate variable.

show_caption_above_media

Takes the value True; if this parameter is specified, the message text will be displayed above the attachment.

How to forward a message

tg_forward_message(platform_id, from_chat_id, message_id, protect_content, disable_notification, message_thread_id)

Parameter
Description

! platform_id

Telegram user ID TO which a message should be sent *

! from_chat_id

Telegram user ID FROM which a message should be sent *

! message_id

Forwarding message ID

protect_content

Content protection flag. Pass any value except 0, False, or '' to enable.

disable_notification

Sound notification flag (default: 0). Pass 1 to disable notification, 0 to enable it.

message_thread_id

Topic ID (available for supergroups with forum functionality enabled)

How to delete a message

tg_delete_message(platform_id, message_id)

! Use this method to delete a message, including service messages, with the following restrictions:

  • A message can only be deleted if it was sent less than 48 hours ago.

  • Messages with dice in a private chat can only be deleted if they were sent more than 24 hours ago.

  • Bots can delete outgoing messages in private chats, groups, and supergroups.

  • Bots can delete incoming messages in private chats.

  • Bots with the can_post_messages permission can delete outgoing messages in channels.

  • If a bot is an administrator of a group, it can delete any message there.

  • If a bot has the can_delete_messages permission in a supergroup or channel, it can delete any message there.

Parameter
Description

! platform_id

Telegram user ID *

! message_id

Message ID to be deleted

How to delete multiple messages

tg_delete_messages(platform_id, message_ids)

! Use this method to delete a message, including service messages, with the following restrictions:

  • A message can only be deleted if it was sent less than 48 hours ago.

  • Messages with dice in a private chat can only be deleted if they were sent more than 24 hours ago.

  • Bots can delete outgoing messages in private chats, groups, and supergroups.

  • Bots can delete incoming messages in private chats.

  • Bots with the can_post_messages permission can delete outgoing messages in channels.

  • If a bot is an administrator of a group, it can delete any message there.

  • If a bot has the can_delete_messages permission in a supergroup or channel, it can delete any message there.

Parameter
Description

! platform_id

Telegram user ID

! message_ids

An array of message IDs to be deleted. Maximum of 100 items.

Example: message sending with Telegram API

Code example for copying

Example 1

Example 2

Example: message editing with Telegram API

Example of configuration

So, let’s send ourselves a message with an inline keyboard:

Next, edit the message text:

And edit the buttons:

Let’s try to edit a message with an image. To do this, send a message with an image and save the sent message ID. Read the detailed instructions on how to get the image URL here:

Now, let's edit the image and its description:

Code example for copying

Example: message copying with Telegram API

Example

Let's send a message and save its ID.

And copy the previously sent message.

Code example for copying

Last updated