-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.ts
135 lines (108 loc) · 3.47 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { load } from 'https://deno.land/std@0.211.0/dotenv/mod.ts'
import {
Bot,
GrammyError,
HttpError,
} from 'https://deno.land/x/grammy@v1.21.1/mod.ts'
import { createConversation } from 'https://deno.land/x/grammy_conversations@v1.2.0/conversation.ts'
import { conversations } from 'https://deno.land/x/grammy_conversations@v1.2.0/mod.ts'
import {
askApiKey,
forwardPostLabels,
saveBunchUrls,
setDefaultLabel,
updateToken,
} from './src/conversations.ts'
import { cancelMenu } from './src/menus.ts'
import { OmnivoreApi } from './src/omnivore/api.ts'
import { MyContext, sessionHandler } from './src/sessionsHandler.ts'
import { inlineQuery } from "./src/inlineQuery.ts";
import { fileListener } from "./src/files.ts";
import { slashCommandsListener } from './src/slashCommands.ts'
import { cancelMenuAndResetLabel } from "./src/menus.ts";
import { getUrlAndLabels } from "./src/utils/getUrlAndLabels.ts";
import { includeSourceChoiceMenu } from "./src/menus.ts";
await load({ export: true })
const bot = new Bot<MyContext>(Deno.env.get('BOT_TOKEN') || '')
// sessions
bot.use(sessionHandler())
// conversations
bot.use(conversations())
bot.use(createConversation(askApiKey))
bot.use(createConversation(saveBunchUrls))
bot.use(createConversation(updateToken))
bot.use(createConversation(setDefaultLabel))
bot.use(createConversation(forwardPostLabels))
// Menu
bot.use(cancelMenu)
bot.use(cancelMenuAndResetLabel)
bot.use(includeSourceChoiceMenu)
// inline query
bot.use(inlineQuery)
// file handling
bot.use(fileListener)
// slash commands handler
bot.use(slashCommandsListener)
// handlers
bot.on('message:entities:url', async ctx => {
// retrieve stuff from session
const token = ctx.session.apiToken
const api = new OmnivoreApi(token)
const {url, labels} = getUrlAndLabels(ctx)
await api.saveUrl(url, labels)
const feedback = api.addedEntriesCount === 1
? 'Successfully added link to Omnivore! 😸👍'
: 'Failed to add the link. 😿'
await ctx.reply(feedback)
})
bot.command('start', async ctx => {
await ctx.reply(
"Hey there! I'm your ultimate bot for all Omnivore things. 🌟 \n\n<b>👾 Save a bunch</b> - allows you to save a bunch of urls at once \n\nTo unlock these features, I'll need an API token. \nDon't worry, your information is handled securely.",
{
parse_mode: 'HTML',
}
)
await ctx.conversation.enter('askApiKey')
})
bot.hears('👾 Save a bunch of urls', async ctx => {
await ctx.reply(
`Send me urls in following format:
url1
url2
url3
each on separate line`,
{
reply_markup: cancelMenu,
}
)
await ctx.conversation.enter('saveBunchUrls')
})
bot.hears('✨ Set new token', async ctx => {
await ctx.reply(
`Okay, just send me the new one
You can get new by following this guide [Getting an API token](https://docs.omnivore.app/integrations/api.html#getting-an-api-token)`,
{
parse_mode: 'MarkdownV2',
reply_markup: cancelMenu,
}
)
await ctx.conversation.enter('updateToken')
})
bot.on('message:text', async ctx => {
await ctx.conversation.enter('forwardPostLabels')
})
bot.start()
bot.catch(err => {
const ctx = err.ctx
console.error(
`Error while handling update ${ctx.update.update_id}:`
)
const e = err.error
if (e instanceof GrammyError) {
console.error('Error in request:', e.description)
} else if (e instanceof HttpError) {
console.error('Could not contact Telegram:', e)
} else {
console.error('Unknown error:', e)
}
})