-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.ts
93 lines (67 loc) · 2.12 KB
/
bot.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import Telegraf, { ContextMessageUpdate } from 'telegraf'
import Extra from 'telegraf/extra'
import Markup from 'telegraf/markup'
import db from './lib/db'
const token = process.env.TELEGRAM_TOKEN
const bot = new Telegraf(token);
const markup = Extra.markdown()
const keyboard = Markup.keyboard([
['List'],
['Help']
])
.oneTime()
.resize();
const helpMessage = `
What bot can:
- Add todo - just write "*Add* todo text"
- List all todo - write "*List*"
- Complete todo - write "*Done* existing todo text"
`
const helloMessage = `
Hi!
There simple Todo X bot,
Bot fully Open Source, you can find code on [github](https://github.com/LeoVS09/serverless-aws-node-typescript-dynamodb-telegram-bot-bootstrap)
${helpMessage}
`
const replyWithHello = async (ctx: ContextMessageUpdate) => {
await ctx.reply(helloMessage, markup)
await ctx.reply('Menu', Extra.markup(keyboard))
}
bot.start(replyWithHello)
// Will send pong when someone send ping
bot.hears('ping', ctx => ctx.reply('pong'))
bot.hears('hi', replyWithHello)
bot.hears('Hi', replyWithHello)
bot.hears(/Add (.+)/, async ({match, reply, chat}) => {
const text = match[1]
await db.create(chat.id, text)
await reply(`${text} *successfully added!*`, markup)
})
bot.hears('List', async ({reply, chat}) => {
await reply('*TODO*:', markup)
const todoReplies: Array<Promise<any>> = [];
for await (const todo of db.list()) {
if(todo.chatId === chat.id)
todoReplies.push(reply(todo.text))
}
await Promise.all(todoReplies)
})
bot.hears(/Done (.+)/, async ({match, reply, chat}) => {
const text = match[1]
for await (const todo of db.list()) {
if(todo.chatId !== chat.id || todo.text !== text)
continue
await db.delete(todo.id)
await reply(`${todo.text} *completed!*`, markup)
return
}
reply(`*Cannot find todo:* ${text}`, markup)
})
const replyWithHelp = async (ctx: ContextMessageUpdate) => {
await ctx.reply(helpMessage, markup)
await ctx.reply('Menu', Extra.markup(keyboard))
}
bot.help(replyWithHelp)
bot.hears('help', replyWithHelp)
bot.hears('Help', replyWithHelp)
export default (body) => bot.handleUpdate(body)