-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (55 loc) · 1.77 KB
/
index.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
import TelegramBot from 'node-telegram-bot-api';
import { TOKEN } from './config.js';
import { IBotCommandInfo } from './types/IBotCommandInfo.js';
import { EBotCommands } from './types/EBotCommands.js';
import { animeInfoSearch } from './api/animeInfoSearch.js';
import { sendStartMessage } from './utils/sendStartMessage.js';
import { throwError } from './utils/throwError.js';
import { sendResultOfSearching } from './utils/sendResultOfSearcing.js';
const bot = new TelegramBot(TOKEN, { polling: true });
bot.on('polling_error', (err) => {
throwError(err.message);
});
// Commands of bot
const Commands: IBotCommandInfo[] = [
{
command: EBotCommands.start,
description: 'Start the bot.',
},
{
command: EBotCommands.search,
description: 'Search anime and info about it.',
},
];
// Set commands
bot.setMyCommands(Commands);
const start = async (msg: TelegramBot.Message) => {
const chatId = msg.chat.id;
sendStartMessage(bot, chatId);
};
const messageHandler = async (title: string, id: number) => {
const chatId = id;
try {
animeInfoSearch(title)
.then((res) => {
if (res === undefined) {
bot.sendMessage(chatId, 'Write title on english language!');
return;
}
sendResultOfSearching(bot, chatId, res);
})
.catch((err) => {
throwError(err);
});
} catch (err) {
throwError(err);
}
};
bot.onText(new RegExp(EBotCommands.search), (msg) => {
const chatId = msg.chat.id;
// Take all in message expect "/search"
messageHandler(msg.text?.split('/search ')[1] ? msg.text?.split('/search ')[1] : 'naruto', chatId);
});
bot.on('message', async (msg) => {
if (msg.text === EBotCommands.start) await start(msg);
});