diff --git a/.travis.yml b/.travis.yml index f3caf71..4154af3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ cache: - node_modules node_js: - - 10 - 12 - 14 + - 15 after_success: npm run coverage diff --git a/README.md b/README.md index a0093ff..ebd1e61 100644 --- a/README.md +++ b/README.md @@ -30,9 +30,8 @@ $ npm install telegraf-session-local -S ## 👀 Quick-start example ```js -const - Telegraf = require('telegraf'), - LocalSession = require('telegraf-session-local') +const {Telegraf} = require('telegraf') +const LocalSession = require('telegraf-session-local') const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here @@ -55,15 +54,14 @@ bot.command('/remove', (ctx) => { ctx.session = null }) -bot.startPolling() +bot.launch() ``` ## 💡 Full example ```js -const - Telegraf = require('telegraf'), - LocalSession = require('telegraf-session-local') +const {Telegraf} = require('telegraf') +const LocalSession = require('telegraf-session-local') const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here @@ -119,12 +117,12 @@ bot.command('/remove', (ctx) => { ctx[property] = null }) -bot.startPolling() +bot.launch() ``` #### Another examples located in `/examples` folder (PRs welcome) Also, you may read comments in [/lib/session.js](https://github.com/RealSpeaker/telegraf-session-local/blob/master/lib/session.js) -# +# Tema Smirnov and contributors / / [![Telegram](https://img.shields.io/badge/%F0%9F%92%AC%20Telegram-%40TemaSM-blue.svg)](https://goo.gl/YeV4gk) diff --git a/examples/extra.js b/examples/extra.js index fe24c26..a7d3ba5 100644 --- a/examples/extra.js +++ b/examples/extra.js @@ -1,6 +1,5 @@ -const - Telegraf = require('telegraf'), - LocalSession = require('../lib/session') // require('telegraf-session-local') +const {Telegraf} = require('telegraf') +const LocalSession = require('../lib/session') // require('telegraf-session-local') const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here @@ -56,4 +55,4 @@ bot.command('/remove', (ctx) => { ctx[property] = null }) -bot.startPolling() +bot.launch() diff --git a/examples/simple.js b/examples/simple.js index f36b6b3..5b65d1f 100644 --- a/examples/simple.js +++ b/examples/simple.js @@ -1,6 +1,5 @@ -const - Telegraf = require('telegraf'), - LocalSession = require('../lib/session') // require('telegraf-session-local') +const {Telegraf} = require('telegraf') +const LocalSession = require('../lib/session') // require('telegraf-session-local') const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here @@ -23,4 +22,4 @@ bot.command('/remove', (ctx) => { ctx.session = null }) -bot.startPolling() +bot.launch() diff --git a/lib/session.d.ts b/lib/session.d.ts index 05448ad..32b3898 100644 --- a/lib/session.d.ts +++ b/lib/session.d.ts @@ -1,7 +1,8 @@ declare module 'telegraf-session-local' { import { AdapterSync, AdapterAsync, BaseAdapter } from 'lowdb' import { Context } from 'telegraf' - import { MiddlewareFn } from 'telegraf/typings/composer' + + type MiddlewareFn = (ctx: C, next: () => Promise) => Promise export interface LocalSessionOptions { storage?: AdapterSync | AdapterAsync diff --git a/package.json b/package.json index a646f8d..5b66b31 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ }, "peerDependencies": { "debug": "^2.0.0 || ^3.0.0 || ^4.0.0", - "telegraf": "^3.38.0" + "telegraf": "^3.38.0 || ^4.0.0" }, "devDependencies": { "coveralls": "^3.1.0", @@ -67,7 +67,7 @@ "np": "*", "nyc": "^15.1.0", "should": "^13.2.3", - "telegraf": "^3.38.0" + "telegraf": "^4.0.2" }, "np": { "yarn": false diff --git a/tests/general.js b/tests/general.js index 5cf027c..a94e0a5 100644 --- a/tests/general.js +++ b/tests/general.js @@ -1,6 +1,6 @@ /* eslint object-curly-spacing: ["error", "always"] */ const - Telegraf = require('telegraf'), + {Telegraf} = require('telegraf'), LocalSession = require('../lib/session'), should = require('should'), debug = require('debug')('telegraf:session-local:test'), @@ -12,6 +12,7 @@ describe('Telegraf Session local : General', () => { it('Should works without specifying any options for LocalSession', (done) => { bot = new Telegraf() + bot.botInfo = {}; const session = new LocalSession() bot.on('text', session.middleware(), (ctx) => { should.exist(ctx.session) @@ -25,6 +26,7 @@ describe('Telegraf Session local : General', () => { it('Should use custom `format.serialize` and `format.deserialize` functions', (done) => { bot = new Telegraf() + bot.botInfo = {}; const session = new LocalSession({ database: 'test_sync_db.json', storage: LocalSession.storageFileSync, @@ -49,6 +51,7 @@ describe('Telegraf Session local : General', () => { it('Should have access to lowdb instance via ctx.sessionDB', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('lowdb instance via `ctx.sessionDB` %o', ctx.sessionDB) should.exist(ctx.sessionDB) @@ -59,6 +62,7 @@ describe('Telegraf Session local : General', () => { it('Should override default `session` property to `data` at middleware() call', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware('data'), (ctx) => { debug('Overrided session property %o', ctx.data) should.exist(ctx.data) @@ -69,6 +73,7 @@ describe('Telegraf Session local : General', () => { it('Should have access to lowdb instance via overrided property in ctx', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware('data'), (ctx) => { debug('lowdb instance via `ctx.dataDB` %o', ctx.dataDB) should.exist(ctx.dataDB) @@ -79,6 +84,7 @@ describe('Telegraf Session local : General', () => { it('Should return `undefined` when context has no `from` field', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Telegraf context `from` field: %o', ctx.from) should.not.exists(localSession.getSessionKey(ctx)) @@ -89,6 +95,7 @@ describe('Telegraf Session local : General', () => { it('Should return `undefined` when no key provided for session to be saved', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), async (ctx) => { const sessionKey = localSession.getSessionKey(ctx) debug('Real session key calculated by LocalSession: %s', sessionKey) diff --git a/tests/sessionKeyUpdateTypes.js b/tests/sessionKeyUpdateTypes.js index be2ad51..ee770d0 100644 --- a/tests/sessionKeyUpdateTypes.js +++ b/tests/sessionKeyUpdateTypes.js @@ -1,6 +1,6 @@ /* eslint object-curly-spacing: ["error", "always"] */ const - Telegraf = require('telegraf'), + {Telegraf} = require('telegraf'), LocalSession = require('../lib/session'), should = require('should'), debug = require('debug')('telegraf:session-local:test'), @@ -12,6 +12,7 @@ describe('Telegraf Session local : Session Key Update Types', () => { it('Should handle message', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', (ctx) => { const sessionKey = localSession.getSessionKey(ctx) debug('Session key', sessionKey) @@ -23,6 +24,7 @@ describe('Telegraf Session local : Session Key Update Types', () => { it('Should handle inline_query', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('inline_query', (ctx) => { const sessionKey = localSession.getSessionKey(ctx) debug('Session key', sessionKey) @@ -35,6 +37,7 @@ describe('Telegraf Session local : Session Key Update Types', () => { it('Should handle callback_query from chat', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.action('c:b', (ctx) => { const sessionKey = localSession.getSessionKey(ctx) debug('Session key', sessionKey) @@ -47,6 +50,7 @@ describe('Telegraf Session local : Session Key Update Types', () => { it('Should handle callback_query from inline_query message', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.action('c:b', (ctx) => { const sessionKey = localSession.getSessionKey(ctx) debug('Session key', sessionKey) diff --git a/tests/storageCustom.js b/tests/storageCustom.js index 2705675..1ff52e2 100644 --- a/tests/storageCustom.js +++ b/tests/storageCustom.js @@ -1,6 +1,6 @@ /* eslint object-curly-spacing: ["error", "always"] */ const - Telegraf = require('telegraf'), + {Telegraf} = require('telegraf'), LocalSession = require('../lib/session'), should = require('should'), debug = require('debug')('telegraf:session-local:test') @@ -38,6 +38,7 @@ describe('Telegraf Session local : storageCustom', () => { it('storageCustom: Should has session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session %O', ctx.session) should.exist(ctx.session) @@ -50,6 +51,7 @@ describe('Telegraf Session local : storageCustom', () => { it('storageCustom: Should handle existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -62,6 +64,7 @@ describe('Telegraf Session local : storageCustom', () => { it('storageCustom: Should handle not existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Not Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -73,6 +76,7 @@ describe('Telegraf Session local : storageCustom', () => { it('storageCustom: Should handle session reset', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session reset - before %O', ctx.session) ctx.session = null diff --git a/tests/storageFileAsync.js b/tests/storageFileAsync.js index baa0fb0..807e422 100644 --- a/tests/storageFileAsync.js +++ b/tests/storageFileAsync.js @@ -1,6 +1,6 @@ /* eslint object-curly-spacing: ["error", "always"] */ const - Telegraf = require('telegraf'), + {Telegraf} = require('telegraf'), LocalSession = require('../lib/session'), should = require('should'), debug = require('debug')('telegraf:session-local:test'), @@ -30,6 +30,7 @@ describe('Telegraf Session local : storageFileAsync', () => { it('storageFileAsync: Should has session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session %O', ctx.session) should.exist(ctx.session) @@ -42,6 +43,7 @@ describe('Telegraf Session local : storageFileAsync', () => { it('storageFileAsync: Should handle existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -54,6 +56,7 @@ describe('Telegraf Session local : storageFileAsync', () => { it('storageFileAsync: Should handle not existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Not Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -65,6 +68,7 @@ describe('Telegraf Session local : storageFileAsync', () => { it('storageFileAsync: Should handle session reset', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session reset - before %O', ctx.session) ctx.session = null diff --git a/tests/storageFileSync.js b/tests/storageFileSync.js index e668f6a..c8ed9c0 100644 --- a/tests/storageFileSync.js +++ b/tests/storageFileSync.js @@ -1,6 +1,6 @@ /* eslint object-curly-spacing: ["error", "always"] */ const - Telegraf = require('telegraf'), + {Telegraf} = require('telegraf'), LocalSession = require('../lib/session'), should = require('should'), debug = require('debug')('telegraf:session-local:test'), @@ -27,6 +27,7 @@ describe('Telegraf Session local : storageFileSync', () => { it('storageFileSync: Should has session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session %O', ctx.session) should.exist(ctx.session) @@ -39,6 +40,7 @@ describe('Telegraf Session local : storageFileSync', () => { it('storageFileSync: Should handle existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -51,6 +53,7 @@ describe('Telegraf Session local : storageFileSync', () => { it('storageFileSync: Should handle not existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Not Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -62,6 +65,7 @@ describe('Telegraf Session local : storageFileSync', () => { it('storageFileSync: Should handle session reset', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session reset - before %O', ctx.session) ctx.session = null diff --git a/tests/storageMemory.js b/tests/storageMemory.js index 47557ce..4abb3dc 100644 --- a/tests/storageMemory.js +++ b/tests/storageMemory.js @@ -1,6 +1,6 @@ /* eslint object-curly-spacing: ["error", "always"] */ const - Telegraf = require('telegraf'), + {Telegraf} = require('telegraf'), LocalSession = require('../lib/session'), should = require('should'), debug = require('debug')('telegraf:session-local:test'), @@ -26,6 +26,7 @@ describe('Telegraf Session local : storageMemory', () => { it('storageMemory: Should has session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session %O', ctx.session) should.exist(ctx.session) @@ -38,6 +39,7 @@ describe('Telegraf Session local : storageMemory', () => { it('storageMemory: Should handle existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -50,6 +52,7 @@ describe('Telegraf Session local : storageMemory', () => { it('storageMemory: Should handle not existing session', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Not Existing Middleware session %O', ctx.session) should.exist(ctx.session) @@ -61,6 +64,7 @@ describe('Telegraf Session local : storageMemory', () => { it('storageMemory: Should handle session reset', (done) => { bot = new Telegraf() + bot.botInfo = {}; bot.on('text', localSession.middleware(), (ctx) => { debug('Middleware session reset - before %O', ctx.session) ctx.session = null