-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbot.js
More file actions
49 lines (41 loc) · 1.19 KB
/
bot.js
File metadata and controls
49 lines (41 loc) · 1.19 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
47
48
49
import { Bot } from 'grammy'
import { apiThrottler } from '@grammyjs/transformer-throttler'
import { autoRetry } from '@grammyjs/auto-retry'
let botInstance = null
/**
* Create and configure Telegram bot with given configuration
*/
export function createBot(config) {
if (!config.tg || !config.tg.token) {
throw new Error('Telegram bot token is required')
}
const botConfig = {
// Only add client config if needed
...(process.env.TELEGRAM_API_SERVER ? {
client: {
apiRoot: process.env.TELEGRAM_API_SERVER
}
} : {})
}
const bot = new Bot(config.tg.token, botConfig)
// Configure API throttling and auto-retry
const throttler = apiThrottler()
bot.api.config.use(throttler)
bot.api.config.use(autoRetry())
// Handle channel posts
bot.on('channel_post', (ctx, next) => {
ctx.update.message = ctx.update.channel_post
next()
})
botInstance = bot
return bot
}
/**
* Get bot instance (must call createBot first)
*/
export function getBot() {
if (!botInstance) {
throw new Error('Bot not initialized. Call createBot first.')
}
return botInstance
}