RedisStorage - Simple add-on for Session vk-io library
Powered by ioredis
yarn add vk-io-redis-storage
npm i vk-io-redis-storage
const { VK } = require('vk-io');
const { SessionManager } = require('@vk-io/session');
const { RedisStorage } = require('vk-io-redis-storage');
const vk = new VK({
token: process.env.TOKEN,
});
function startBot({ updates }) {
const storage = new RedisStorage({
// redis: ioRedisClient,
redis: {
host: '127.0.0.1',
},
keyPrefix: 'vk-io:session:',
// ttl: 12 * 3600,
});
const sessionManager = new SessionManager({
storage,
getStorageKey: (ctx) =>
ctx.userId
? `${ctx.userId}:${ctx.userId}`
: `${ctx.peerId}:${ctx.senderId}`,
});
updates.on('message', sessionManager.middleware);
updates.on('message_new', (ctx, next) => {
if (context.text !== '/counter') {
return next();
}
if (ctx.isOutbox) return;
const { session } = ctx;
session.counter = (session.counter || 0) + 1;
ctx.send(`You turned to the bot (${session.counter}) times`);
});
updates.start().catch(console.error);
}
// ...
startBot(vk);