Skip to main content

Update Handling

The Updates module is a powerful tool for processing and responding to Telegram bot updates. It makes it easy to register handlers for different types of updates and contexts, as well as send text messages and other actions in response to received updates.

Registering Handlers

To register handlers, you need to use the on() method, which has many overloads based on different types of updates and corresponding contexts. For example, to register a message update handler:

client.updates.on("message", (message) => {
// your message update handling code
});

Handlers are added to the handlers object of an Updates instance. Multiple handlers can be registered for each type of update.

note

In the example, client is an instance of the Evogram class.

Context Objects

A context is an object that contains information about the received update and its sender. Depending on the type of update, the appropriate context is used. A context is an instance of a class, the subclass of which is the context for a specific type of update.

For example, to handle a message update, you need to use MessageContext. It contains information about the message text, sender and recipient, as well as methods for responding to the message.

client.updates.on('message', (message) => {
message.reply('Hello, I am a bot!');
});

Starting Update Processing

To start the process of updating handling, you need to call the start() method on the transport object (Polling or Webhook). The polling.start() method allows you to start the process of regular polling of the Telegram bot's servers via an API, and the webhook.start() method sets up a webhook.

client.updates.polling.start();
note

Note that the start() method must be called after registering all update handlers.

Usage Example

import { Evogram } from 'evogram';
const client = new Evogram("YOUR_BOT_TOKEN");

client.updates.on("message", (message) => {
message.reply("Hello!")
})

client.updates.polling.start();

The code above is an example that allows your Telegram bot to respond to all received messages with the text Hello!.