-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
52 lines (38 loc) · 1.16 KB
/
index.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
const Telegraf = require('telegraf');
const EventSource = require('eventsource');
const { telegrafBotToken } = require('./env');
const {
log,
commandHelpers: { extractCommandSyntax, findCommand },
} = require('./modules');
const commands = require('./commands');
const bot = new Telegraf(telegrafBotToken);
bot.use(Telegraf.log())
const triggerReply = (ctx, output) => {
const { text, markup, isMarkdown } = output;
const reply = isMarkdown
? ctx.replyWithMarkdown
: ctx.reply;
if (markup) {
reply(text, markup);
} else {
reply(text);
}
};
bot.on('text', (ctx, next) => {
const msg = ctx.message.text;
const commandSyntax = extractCommandSyntax(msg);
const command = findCommand(commands, commandSyntax);
if (command) {
const localTriggerReply = output => triggerReply(ctx, output);
const output = command.invoke(commandSyntax, ctx, localTriggerReply);
if (output) {
localTriggerReply(output);
}
}
next();
});
bot.startPolling();
log('app: started');
const publicDoorES = new EventSource('http://door.bar/sse');
publicDoorES.onmessage = ({ data }) => bot.telegram.sendMessage(-1001320604180, JSON.parse(data));