This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
bot.js
47 lines (46 loc) · 1.68 KB
/
bot.js
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
import { botList } from './utils/tgbot_command/list'
import { botSub } from './utils/tgbot_command/sub'
import { botUnSub } from './utils/tgbot_command/unsub'
import { botUnSubAll } from './utils/tgbot_command/unsuball'
const { Telegraf } = require('telegraf')
import { config } from './config'
const bot = new Telegraf(config.TG_TOKEN)
bot.command('list', botList)
bot.command('sub', botSub)
bot.command('unsuball', botUnSubAll)
bot.command('unsub', botUnSub)
export function setTgBot(router) {
router.post(`/${config.SECRET_PATH}`, async (req, e) => {
const body = await req.json()
if (body.message != undefined) {
const msg = body.message
let cmd = ''
if (msg.entities != undefined) {
for (let i = 0; i < msg.entities.length; i++) {
if (msg.entities[i].type == 'bot_command') {
cmd = msg.text.substring(
msg.entities[i].offset,
msg.entities[i].offset + msg.entities[i].length
)
break
}
}
}
console.log(cmd)
if (
(cmd === '/unsub' ||
cmd === '/sub' ||
cmd === '/unsuball' ||
cmd === '/list') &&
msg.chat.id == config.TG_USERID
) {
await bot.handleUpdate(body)
return new Response('ok', { status: 200 })
} else {
return new Response('ok', { status: 200 })
}
} else {
return new Response('ok', { status: 200 })
}
})
}