This example demonstrates how to create a simple VK bot that responds to commands using the HearManager from @vk-io/hear.
What You’ll Learn
- Setting up a basic VK bot
- Using HearManager for command handling
- Responding to text commands and regex patterns
- Sending photos and audio messages
- Working with different hear patterns (strings, arrays, regex)
Prerequisites
Install dependencies
npm install vk-io @vk-io/hear
Get your access token
Create a VK community and get an access token with message permissions.
Set environment variable
export TOKEN=your_vk_access_token
Complete Example
const { VK } = 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', hearManager.middleware);
hearManager.hear('/start', async context => {
await context.send(`
My commands list
/cat - Cat photo
/purr - Cat purring
/time - The current date
/reverse - Reverse text
`);
});
hearManager.hear('/cat', async context => {
await Promise.all([
context.send('Wait for the uploads awesome 😻'),
context.sendPhotos({
value: 'https://loremflickr.com/400/300/',
}),
]);
});
hearManager.hear(['/time', '/date'], async context => {
await context.send(String(new Date()));
});
hearManager.hear(/^\/reverse (.+)/i, async context => {
await context.send(context.$match[1].split('').reverse().join(''));
});
const catsPurring = [
'http://ronsen.org/purrfectsounds/purrs/trip.mp3',
'http://ronsen.org/purrfectsounds/purrs/maja.mp3',
'http://ronsen.org/purrfectsounds/purrs/chicken.mp3',
];
hearManager.hear('/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);
Code Breakdown
Initialize VK and HearManager
const vk = new VK({
token: process.env.TOKEN,
});
const hearManager = new HearManager();
vk.updates.on('message_new', hearManager.middleware);
The HearManager is a powerful middleware that allows you to listen for specific patterns in messages. We attach it to the message_new event to handle incoming messages.
Simple Text Commands
hearManager.hear('/start', async context => {
await context.send(`
My commands list
/cat - Cat photo
/purr - Cat purring
/time - The current date
/reverse - Reverse text
`);
});
The /start command sends a welcome message with available commands.
Multiple Command Aliases
hearManager.hear(['/time', '/date'], async context => {
await context.send(String(new Date()));
});
You can pass an array of patterns to hear() to create command aliases. Both /time and /date will trigger the same handler.
Regex Pattern Matching
hearManager.hear(/^\/reverse (.+)/i, async context => {
await context.send(context.$match[1].split('').reverse().join(''));
});
Regex patterns allow you to capture user input. The captured groups are available in context.$match. For example, /reverse hello would reverse the text “hello”.
Sending Photos
hearManager.hear('/cat', async context => {
await Promise.all([
context.send('Wait for the uploads awesome 😻'),
context.sendPhotos({
value: 'https://loremflickr.com/400/300/',
}),
]);
});
Using Promise.all() allows you to send multiple messages concurrently, improving response time.
Sending Audio Messages
const catsPurring = [
'http://ronsen.org/purrfectsounds/purrs/trip.mp3',
'http://ronsen.org/purrfectsounds/purrs/maja.mp3',
'http://ronsen.org/purrfectsounds/purrs/chicken.mp3',
];
hearManager.hear('/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,
}),
]);
});
This example shows how to send audio messages from URLs. The bot randomly selects one of three cat purring sounds.
Running the Bot
Once running, send messages to your VK community:
/start - See the commands list
/cat - Get a random cat photo
/purr - Get a cat purring audio
/time or /date - Get the current date
/reverse hello world - Reverse any text
Next Steps