Skip to content

Commit

Permalink
refactor: adapt for telegraf v4.0 and v3.38 (#107)
Browse files Browse the repository at this point in the history
* refactor: adapt for telegraf v4.0 and v3.38

this is not breaking compatibility with 3.38

* chore(npm): require at least telegraf 3.38 of the v3

telegraf typings arnt considered breaking in v3 and 3.38 works for sure

* docs: readme example != example folder

* refactor: redefine MiddlewareFn to prevent breaking

redefining something from a lib is bad but as the import changed it
would be breaking otherwise

* test: adapt to telegraf v4

* 💚 [travisci] Removed node 10 / Added node 15

Co-authored-by: Tema Smirnov <git.tema@smirnov.one>
  • Loading branch information
EdJoPaTo and TemaSM authored Jan 30, 2021
1 parent 0de45f6 commit aeb2d62
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ cache:
- node_modules

node_js:
- 10
- 12
- 14
- 15
after_success: npm run coverage
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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 / <github.tema@smirnov.one> / [![Telegram](https://img.shields.io/badge/%F0%9F%92%AC%20Telegram-%40TemaSM-blue.svg)](https://goo.gl/YeV4gk)
7 changes: 3 additions & 4 deletions examples/extra.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -56,4 +55,4 @@ bot.command('/remove', (ctx) => {
ctx[property] = null
})

bot.startPolling()
bot.launch()
7 changes: 3 additions & 4 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -23,4 +22,4 @@ bot.command('/remove', (ctx) => {
ctx.session = null
})

bot.startPolling()
bot.launch()
3 changes: 2 additions & 1 deletion lib/session.d.ts
Original file line number Diff line number Diff line change
@@ -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<C extends Context> = (ctx: C, next: () => Promise<void>) => Promise<void>

export interface LocalSessionOptions<TSession> {
storage?: AdapterSync | AdapterAsync
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -67,7 +67,7 @@
"np": "*",
"nyc": "^15.1.0",
"should": "^13.2.3",
"telegraf": "^3.38.0"
"telegraf": "^4.0.2"
},
"np": {
"yarn": false
Expand Down
9 changes: 8 additions & 1 deletion tests/general.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion tests/sessionKeyUpdateTypes.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion tests/storageCustom.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/storageFileAsync.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/storageFileSync.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion tests/storageMemory.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit aeb2d62

Please sign in to comment.