Events
All update types and their handling
Events
bot.on(event, handler) subscribes a handler to a Yurba update type.
Registering handlers
bot.on('message', (ctx) => {
console.log('Повідомлення:', ctx.message.Text);
});
bot.on('inline', (ctx) => {
if (!ctx.query) return;
return ctx.answer([{ id: '1', title: 'Echo', text: ctx.query }]);
});
bot.on('stopped', (update) => {
console.log('Користувач заблокував бота:', update.User?.ID);
});Event types and contexts
| Event | What the handler receives | Description |
|---|---|---|
message | MessageContext | Regular messages (not commands) |
inline | InlineContext | Inline queries (@bot query) |
callback | — | Handled via bot.callback() |
| Others | Raw update | See the table below |
Other events
For all other types, Yurst passes the raw update object:
| Event | When it occurs |
|---|---|
inline_sent | The user selected an inline result |
stopped | The user blocked the bot |
added | The bot was added to a chat |
removed | The bot was removed from a chat |
joined | Someone joined the chat |
left | Someone left the chat |
edit | A message was edited |
delete | A message was deleted |
test | A test request from the bot settings |
Examples
// Користувач заблокував бота
bot.on('stopped', (update) => {
console.log(`User ${update.User?.ID} blocked the bot`);
});
// Бота додали до чату
bot.on('added', (update) => {
console.log('Added to chat:', update);
});
// Повідомлення відредаговано
bot.on('edit', (update) => {
const msg = update.Message;
console.log(`Message ${msg?.ID} edited:`, msg?.Text);
});
// Повідомлення видалено
bot.on('delete', (update) => {
console.log('Message deleted:', update);
});Multiple handlers
You can register several handlers for one event. They run sequentially:
bot.on('message', async (ctx) => {
console.log('Перший обробник');
// Виконується першим
});
bot.on('message', async (ctx) => {
console.log('Другий обробник');
// Виконується другим
});Error handling
If a handler throws an error:
- Subsequent handlers for this event are not called
- The error is logged
stats.errorsis incremented- The process keeps running
bot.on('message', async (ctx) => {
throw new Error('Помилка!');
// Цей код не виконається
});
bot.on('message', async (ctx) => {
// Цей обробник НЕ буде викликаний
// бо попередній впав з помилкою
});Do not forget await
If you call asynchronous methods, be sure to await the promise or return it. Otherwise the error may be lost.
Concurrency
Different updates can be processed simultaneously:
let counter = 0;
bot.on('message', async (ctx) => {
const current = counter++;
await new Promise(r => setTimeout(r, 1000));
console.log(current, counter);
// Може вивести: 0 5
// Бо інші оновлення оброблялися паралельно
});If handlers share state (a Map, counters, and so on), do not assume sequential execution.
The message event
bot.on('message') receives only non-command messages:
bot.command('ping', 'Ping', (ctx) => ctx.reply('pong'));
bot.on('message', (ctx) => {
// Сюди НЕ потрапить /ping
// Сюди потрапить будь-яке інше повідомлення
console.log('Text:', ctx.message.Text);
});Unknown events
If Yurba adds a new event type, Yurst passes it as a raw object:
bot.on('new_event_type', (update) => {
console.log('New event:', update);
});Example: Logging all events
import { L } from 'yurst.js';
// Логуємо всі події
const EVENT_TYPES = [
'stopped', 'added', 'removed', 'joined', 'left', 'edit', 'delete',
];
for (const type of EVENT_TYPES) {
bot.on(type, (update) => {
L.info('event', type, update);
});
}Next
Lifecycle
Startup, graceful shutdown, and error handling