Powered by ioredis
Based on vk-io-redis-storage
yarn add puregram-redis-storage
npm i puregram-redis-storage
const { Telegram } = require('puregram');
const { SessionManager } = require('@puregram/session');
const { RedisStorage } = require('puregram-redis-storage');
const telegram = Telegram.fromToken(process.env.TOKEN);
function startBot({ updates }) {
const storage = new RedisStorage({
// redis: ioRedisClient,
redis: {
host: '127.0.0.1',
port: 6379
},
keyPrefix: 'puregram:session:'
// ttl: 12 * 3600
});
const sessionManager = new SessionManager({
storage,
getStorageKey: (context) => `${context.chat.id}:${context.from?.id ?? 0}`
});
updates.on('message', sessionManager.middleware);
updates.on('message', (context, next) => {
if (context.text !== '/counter') {
return next();
}
if (context.isOutbox) return;
const { session } = context;
session.counter = (session.counter || 0) + 1;
context.send(`You turned to the bot (${session.counter}) times`);
});
updates.startPolling().catch(console.error);
}
// ...
startBot(telegram);