2.0.0 • Published 2 months ago

puregram-redis-storage v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

puregram-redis-storage

NPM version NPM downloads

✈️ Redis storage for @puregram/session module

Powered by ioredis Based on vk-io-redis-storage

Installation

yarn add puregram-redis-storage
npm i puregram-redis-storage

Example usage

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);