UNPKG

955 BJavaScriptView Raw
1const { VkBotSdk } = require('../index');
2
3class CustomBotError extends Error { }
4
5const customErrorsHandler = (err, ctx, next) => {
6 if(err instanceof CustomBotError) ctx.reply(`Error: ${err.message}`);
7 else next();
8};
9
10const unknownErrorsHandler = (err, ctx, next) => {
11 ctx.reply('Unknown internal error, try to send this command later');
12};
13
14const 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
38main().then(() => {
39 console.log('Initialized');
40});
41