Any Telegram Event
Handling any Telegram events that occurred in a chat with the bot. Tracking any Telegram event using the events feature
Creating a Telegram event handler
Information
Before creating an event handler, first create a new scenario that will be launched by the event.
To set up tracking of any Telegram event, go to the "Messenger and app events" section and open the Telegram tab.
.png)
Telegram tab in the "Messenger and app events" section
Click "Create" and select "Any Telegram event".
.png)
Event selection
In the event creation form, fill in the name and select a scenario.
Information
The selected scenario will be launched if the script runs successfully.
If the script runs with an error, execution will continue from where the contact was before the event triggered.
.png)
Creating an event handler
Add JavaScript code in the code input field. In the code, you can handle Telegram events; for details on available events, see the Telegram documentation.
The request received from Telegram is available in the response object:
var rawBody = response.body; // Raw request body
var data = response.data; // Object of all request variables, including query string, form, body, etc.
Information
To view the request contents during debugging, you can send it in a message to the bot chat — see the code example Sending request contents in a message to the bot chat
Information
When the bot is added to a group as an administrator, all group events will be available, but all JS code methods will only work for bot contacts. For example, if the event initiator is a contact subscribed to the bot, the bot can send them a message in the chat using the sendMessage() method.
More details about available variables and functions.
Important
The following methods are not available for handling events in JavaScript code:
- goToBlock(),
- disableContinue(),
- setContactByTelegramId(id),
- setContactById(id)
Examples
Sending request contents in a message to the bot chat
For debugging event handling, you can send the request contents to the bot chat in a message. When sending a message to the chat, Telegram may interpret special characters (for example, underscores, asterisks) as formatting elements. To display request parameters as valid JSON, use the following code:
var formatted = JSON.stringify(response.data, null, 2);
var message = "<pre><code>" + formatted + "</code></pre>";
sendMessage(message);
Message edited event
var data = response.data;
sendMessage("You edited a message. New contents:" + data.edited_message.text);
In this code:
- if the data.edited_message.text parameter exists, the edited_message event occurred. After sending the message, the scenario specified in the event settings will launch
- if the parameter does not exist, the script will run with an error and execution will return to where the contact was before editing the message. You can also throw an exception explicitly:
if (!data.edited_message) {
throw new Error("Expected message edit event");
}
Example response when this event triggers (edited_message)
{
"update_id": 123456789,
// Event: user edited a message
"edited_message": {
// Information about the chat where editing occurred
"chat": {
"first_name": "First", // User's first name
"id": 111111111, // Chat ID (matches user ID in private chats)
"last_name": "Last", // User's last name
"type": "private", // Chat type: private, group, supergroup, channel
"username": "exampleuser" // Username
},
"date": 1710000000, // Original message send time (UNIX time)
"edit_date": 1710000300, // Message edit time (UNIX time)
// Sender information
"from": {
"first_name": "First",
"id": 111111111,
"is_bot": false,
"language_code": "en", // User's Telegram interface language
"last_name": "Last",
"username": "exampleuser"
},
"message_id": 112, // Message ID
"text": "Updated message text" // Updated message text
}
}
Accepting Telegram Stars payments in a chat bot — creating a digital goods sales system
Step-by-step creation of a Telegram star currency payment acceptance system with automatic digital goods delivery.
Learn how to create event handlers via JavaScript, generate payment links via Telegram Bot API (bot application programming interface), handle successful transactions, and build a sales system without integrating external payment systems.
Template for one-click installation of the "Telegram Stars for chat bot payments | Telegram stars" project:
Script for getting the event response body:
var result = response.body;
sendMessage(result);
Script for saving payment ID and product type to variables:
var payID = response.data.pre_checkout_query.id
var type = response.data.pre_checkout_query.invoice_payload
setContactVariable("payID", payID);
setContactVariable("type", type);
GPT integration for handling all message types
Create a smart Telegram bot: processing text messages, voice, and images via OpenAI. Telegram event automation.
Ready template for one-click deployment of the "How to create an AI assistant that HEARS, SEES, and SELLS — in 1 lesson (GPT + Telegram)" project:
Script for the user message received event and determining its type:
var d = (response && response.data) ? response.data : (response || {});
var m = (d.message) || (d.result && d.result[0] && d.result[0].message) || {};
setContactVariable("debugData", JSON.stringify(m));
var t = (m && m.text) || "";
// Text first
if (t) {
setContactVariable("messageType", "text");
setContactVariable("textMessage1", t);
}
// Then voice
else if (m.voice && m.voice.file_id) {
setContactVariable("messageType", "voice");
setContactVariable("voiceFileId", m.voice.file_id);
}
// If photo
else if (m.photo && m.photo.length > 0) {
var ph = m.photo[m.photo.length - 1]; // take the largest photo
setContactVariable("messageType", "photo");
setContactVariable("photoFileId1", ph.file_id);
// Save caption if present
if (typeof m.caption === "string" && m.caption) {
setContactVariable("captionText", m.caption);
}
}
// Everything else
else {
setContactVariable("messageType", "other");
}
The LEADTEX dashboard already offers 250+ lessons on chat bot and mini-app development, plus the opportunity to join the largest developer community with 6,000+ members — Go to dashboard.