-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathsimple-keyboard-bot.js
More file actions
119 lines (97 loc) · 2.85 KB
/
simple-keyboard-bot.js
File metadata and controls
119 lines (97 loc) · 2.85 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const { VK, Keyboard } = require('vk-io');
const { HearManager } = require('@vk-io/hear');
const vk = new VK({
token: process.env.TOKEN,
});
const hearManager = new HearManager();
vk.updates.on('message_new', (context, next) => {
const { messagePayload } = context;
context.state.command = messagePayload?.command || null;
return next();
});
vk.updates.on('message_new', hearManager.middleware);
// Simple wrapper for commands
const hearCommand = (name, conditions, handle) => {
if (typeof handle !== 'function') {
handle = conditions;
conditions = [`/${name}`];
}
if (!Array.isArray(conditions)) {
conditions = [conditions];
}
hearManager.hear([(_text, { state }) => state.command === name, ...conditions], handle);
};
// Handle start button
hearCommand('start', (context, next) => {
context.state.command = 'help';
return Promise.all([
context.send('Hello!'),
next(),
]);
});
hearCommand('help', async context => {
await context.send({
message: `
My commands list
/help - The help
/time - The current date
/cat - Cat photo
/purr - Cat purring
`,
keyboard: Keyboard.builder()
.textButton({
label: 'The help',
payload: {
command: 'help',
},
})
.row()
.textButton({
label: 'The current date',
payload: {
command: 'time',
},
})
.row()
.textButton({
label: 'Cat photo',
payload: {
command: 'cat',
},
color: Keyboard.PRIMARY_COLOR,
})
.textButton({
label: 'Cat purring',
payload: {
command: 'purr',
},
color: Keyboard.PRIMARY_COLOR,
}),
});
});
hearCommand('cat', async context => {
await Promise.all([
context.send('Wait for the uploads awesome 😻'),
context.sendPhotos({
value: 'https://loremflickr.com/400/300/',
}),
]);
});
hearCommand('time', ['/time', '/date'], async context => {
await context.send(String(new Date()));
});
const catsPurring = [
'http://ronsen.org/purrfectsounds/purrs/trip.mp3',
'http://ronsen.org/purrfectsounds/purrs/maja.mp3',
'http://ronsen.org/purrfectsounds/purrs/chicken.mp3',
];
hearCommand('purr', async context => {
const link = catsPurring[Math.floor(Math.random() * catsPurring.length)];
await Promise.all([
context.send('Wait for the uploads purring 😻'),
context.sendAudioMessage({
value: link,
}),
]);
});
vk.updates.start().catch(console.error);