1 | const { VkBotSdk } = require('../index');
|
2 |
|
3 | class CustomBotError extends Error { }
|
4 |
|
5 | const customErrorsHandler = (err, ctx, next) => {
|
6 | if(err instanceof CustomBotError) ctx.reply(`Error: ${err.message}`);
|
7 | else next();
|
8 | };
|
9 |
|
10 | const unknownErrorsHandler = (err, ctx, next) => {
|
11 | ctx.reply('Unknown internal error, try to send this command later');
|
12 | };
|
13 |
|
14 | const main = async () => {
|
15 | const sdk = new VkBotSdk({
|
16 | debug: true,
|
17 | group_id: 0,
|
18 | access_token: ''
|
19 | });
|
20 |
|
21 | const bot = sdk.getCallback();
|
22 |
|
23 | bot.onError(customErrorsHandler);
|
24 | bot.onError(unknownErrorsHandler);
|
25 |
|
26 | bot.defaultReply((ctx, params) => {
|
27 | if(!('nonExistParameter' in ctx)) {
|
28 | throw new CustomBotError('Invalid ctx');
|
29 | }
|
30 |
|
31 | ctx.replyKeyboard(`Default reply, which will never sent`);
|
32 | });
|
33 |
|
34 | bot.initLongPoll();
|
35 | };
|
36 |
|
37 |
|
38 | main().then(() => {
|
39 | console.log('Initialized');
|
40 | });
|
41 |
|