Powered by Lowdb
npm i @vbots/session-storage
yarn add @vbots/session-storage
const { VK } = require('vk-io');
const { SessionManager } = require('@vk-io/session');
const { SessionStorage } = require('@vbots/session-storage');
const vk = new VK({
token: process.env.TOKEN
});
async function startBot({ updates }) {
//
const storage = new SessionStorage({ name: 'anotherBot' });
// Init DB folder & file
await storage.init();
const sessionManager = new SessionManager({
storage,
// For test
getStorageKey: (context) => context.userId ? (`${context.userId}_${context.userId}`) : (`${context.peerId}_${context.senderId}`)
});
// Init user session storage
updates.on('message', sessionManager.middleware);
// ...
updates.hear('/counter', async (context) => {
const { session } = context;
if (!session.counter) {
session.counter = 0;
}
session.counter += 1;
await context.send(`You turned to the bot (${session.counter}) times`);
});
// ...
updates.start().catch(console.error);
}
// ...
startBot(vk);