-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
92 lines (73 loc) · 2.81 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
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
const { Telegraf, Scenes, session, Markup} = require('telegraf')
const { match } = require('telegraf-i18n')
const { comment, createPostHere, profile, profileSettings, randomPost, viewProfile, search } = require('./src/controllers')
const User = require('./src/models/user')
const config = require('config');
const db = require('./src/lib/db')
const TelegrafI18n = require("telegraf-i18n");
const path = require("path");
db.createDB().then(() => console.log('DB initialized'))
const bot = new Telegraf(config.get('bot.token'))
const i18n = new TelegrafI18n({
defaultLanguage: config.get('language'),
allowMissing: false,
directory: path.resolve(__dirname, 'locales')
})
const stage = new Scenes.Stage([comment, createPostHere, profile, profileSettings, randomPost, viewProfile, search])
bot.use(Telegraf.log());
bot.use(session());
bot.use(stage.middleware());
bot.use(i18n.middleware())
async function getCreatePostKeyboard(ctx, link) {
return Markup.keyboard([
[Markup.button.webApp(ctx.i18n.t('createPost.createWebAppBtn'), link), ctx.i18n.t('createPost.createHereBtn')]
]).oneTime().resize()
}
bot.hears(match('createPost.createHereBtn'), (ctx) => ctx.scene.enter('createPostHere'))
bot.start((ctx) => {
(async function() {
await ctx.replyWithHTML(ctx.i18n.t('start.greeting'))
console.log('\n\n\n\n\n\n' + ctx.from.id + '\n\n\n\n\n\n')
const reply = await db.createUser(new User(ctx.from.id, 0))
const message = ctx.i18n.t('start.profileSummary', {
username: ctx.from.username,
carma: reply['user'].carma,
})
await ctx.replyWithHTML(message)
})()
})
bot.command('add', (ctx) => {
(async function() {
await ctx.replyWithHTML(ctx.i18n.t('action.promptAction'), await getCreatePostKeyboard(ctx, 'https://mootfrost.ru/'))
})()
})
bot.command('random', (ctx) => ctx.scene.enter('randomPost'))
bot.command('profile', (ctx) => ctx.scene.enter('profile'))
bot.command('viewprofile', (ctx) => {
(async function() {
let args = ctx.update.message.text.split(' ')
if (args.length > 1) {
await ctx.scene.enter('viewProfile', {userId: args[1]})
}
else {
await ctx.replyWithHTML(ctx.i18n.t('viewProfile.noUserIdProvided'))
}
})()
})
bot.command('search', (ctx) => ctx.scene.enter('search'))
bot.help((ctx) => {
(async function() {
let commands = ''
for (let command of await bot.telegram.getMyCommands()) {
commands += (command['command'] + ' - ' + command['description'] + '\n')
}
console.log('replied')
await ctx.replyWithHTML(await ctx.i18n.t('help.commands', {
commands: commands
}))
})()
})
bot.launch().then(() => console.log(`${config.get('bot.name')} started`))
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))
module.exports = bot