-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
74 lines (67 loc) · 2.37 KB
/
Copy pathindex.js
File metadata and controls
74 lines (67 loc) · 2.37 KB
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
import {
Bot,
InlineQueryResultBuilder,
webhookCallback,
} from 'grammy';
import { createServer } from 'node:http';
import 'dotenv/config';
const bot = new Bot(process.env.TOKEN);
const botUpdateOpts = {
allowed_updates: ['message', 'inline_query'],
};
let profs = [];
async function update() {
const resp = await fetch('https://classifikators.ru/assets/downloads/okved/okved.csv');
if (!resp.ok || !resp.body) {
console.error('KTHXBYE');
return;
}
const newBody = resp.body
.pipeThrough(new TextDecoderStream('windows-1251'))
.pipeThrough(new TextEncoderStream());
const data = await new Response(newBody).text();
profs = data
.split('\r\n')
.filter(x => x != '')
.map(x => x
.split(';')[2]
.replace(/^"(.+)"$/, '$1'));
}
await update();
const escMd = txt => txt.replace(/([_\*\[\]\(\)~\``>#\+\-=|\{\}\.!])/g, '\\$1');
function getProf() {
if (profs.length == 0) {
update();
return 'Список профессий не был загружен…';
}
return profs[Math.floor(Math.random() * profs.length)];
}
bot.command('start', ctx => ctx.reply(
'Привет! Этот бот поможет выбрать профессию по случайному выбору из ОКВЭД.' +
`\nДля того, чтобы начать, введите /get_prof или используйте инлайн запросы, набрав @${bot.me.username} <пробел>, должна появиться кнопка.`
));
bot.command('get_prof', ctx => ctx.reply(getProf()));
bot.on('inline_query', async ctx => {
const result = InlineQueryResultBuilder
.article('prof', 'Познай свою профессию')
.text(`_${escMd('В следующей жизни у меня будет профессия…')}_\n${escMd(getProf())}`,
{ parse_mode: 'MarkdownV2' });
await ctx.answerInlineQuery([result], { cache_time: 0, is_personal: true });
});
console.log('let\'s go');
switch (process.env.MODE) {
case 'debug':
await bot.api.deleteWebhook();
bot.start(botUpdateOpts);
break;
case 'prod': {
await bot.api.setWebhook(process.env.WEBHOOK_URL, botUpdateOpts);
const server = createServer(webhookCallback(bot, 'http'));
server.listen(
parseInt(process.env.SRV_PORT) || 8300,
process.env.SRV_HOST || 'localhost',
() => console.log('webhook is ready')
);
break;
}
}