-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegram.js
73 lines (64 loc) · 1.74 KB
/
telegram.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const { Telegraf } = require('telegraf')
const HttpsProxyAgent = require('https-proxy-agent')
function setupProxy() {
if (!process.env.http_proxy) {
return {}
}
const proxy = process.env.http_proxy
const agent = new HttpsProxyAgent(proxy)
console.log(proxy)
return { telegram: { agent } }
}
const tgbot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN, setupProxy())
function botForCtx(ctx) {
return {
formatCommand(cmd) {
return `/${cmd}`
},
sender: ctx.from.username,
async sendMessage(msg) {
// await ctx.telegram.sendMessage(ctx.message.chat.id, msg);
ctx.reply(msg)
},
async sendHtmlMessage(msg) {
// await ctx.telegram.sendMessage(ctx.message.chat.id, msg, {parse_mode: 'HTML'});
ctx.reply(msg, { parse_mode: 'HTML' })
},
}
}
exports.start = function (ghost) {
for (const cmd in ghost) {
tgbot.command(cmd, (ctx) => {
const bot = botForCtx(ctx)
const args = ctx.message.text
.split(' ')
.map((s) => s.trim())
.slice(1)
ghost[cmd](bot, args)
})
}
tgbot.on('text', (ctx) => {
const cmds = ctx.message.text.split(' ').map((s) => s.trim())
console.log('chat type', ctx.chat.type)
if (ctx.chat.type == 'group' || ctx.chat.type == 'supergroup') {
if (cmds.length < 2) return
const mention = cmds[0]
if (mention != `@${ctx.botInfo.username}`) return
cmds.shift()
} else {
if (!cmds) return
}
if (!cmds[0].startsWith('/')) return
const cmd = cmds[0].substring(1)
const args = cmds.slice(1)
const bot = botForCtx(ctx)
if (cmd in ghost) {
ghost[cmd](bot, args)
}
})
tgbot.start((ctx) => {
const bot = botForCtx(ctx)
ghost.faucet(bot)
})
tgbot.launch()
}