-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (40 loc) · 1.4 KB
/
Copy pathindex.js
File metadata and controls
46 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require('dotenv').config();
const { Telegraf } = require('telegraf');
const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
bot.use((ctx, next) => {
// Любопытно, если не передать next в эту мидлварь, и не вызвать next() явно,
// то управление все равно передет следующему обработчику.
// Как это устроено ???
next();
})
bot.use(async (ctx) => {
// Свойство update это сообщение, которое прислал боту Telegram
await ctx.reply(JSON.stringify(ctx.update, null, 2));
});
bot.launch().then((console.log('Started')));
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));
// Пример ctx.update
// {
// "update_id": 927353825,
// "message": {
// "message_id": 2,
// "from": {
// "id": 400654960,
// "is_bot": false,
// "first_name": "Павел",
// "last_name": "Дегтяренко",
// "username": "pavel_else",
// "language_code": "ru"
// },
// "chat": {
// "id": 400654960,
// "first_name": "Павел",
// "last_name": "Дегтяренко",
// "username": "pavel_else",
// "type": "private"
// },
// "date": 1680305655,
// "text": "ping"
// }
// }