@vbots/session-storage
v1.1.0
Published
SessionStorage with lowdb Add-on for Session vk-io library
Downloads
11
Readme
SessinStorage Add-on for Session vk-io library
Powered by Lowdb
Usage
NPM
npm i @vbots/session-storage
Yarn
yarn add @vbots/session-storage
Example
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);