Skip to content

Commit

Permalink
📝 Improved code-examples / Updated: README & LICENSE, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
TemaSM committed Jan 11, 2023
1 parent 7ba769c commit 55b4ac1
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 168 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 Tema Smirnov and contributors
Copyright (c) 2023 Tema Smirnov and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
40 changes: 17 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

[![NPM Version](https://img.shields.io/npm/v/telegraf-session-local.svg?style=flat-square)](https://www.npmjs.com/package/telegraf-session-local)
[![Nodejs](https://img.shields.io/node/v/telegraf-session-local.svg?style=flat-square)](https://www.npmjs.com/package/telegraf-session-local)
[![npm](https://img.shields.io/npm/dm/telegraf-session-local.svg?style=flat-square)](https://npmcharts.com/compare/telegraf-session-local,telegraf-session-redis,telegraf-session-mysql,telegraf-session-mongo,telegraf-session-dynamodb?interval=30)
[![GitHub Actions Status](https://img.shields.io/github/workflow/status/RealSpeaker/telegraf-session-local/CI?style=flat-square)](https://github.com/RealSpeaker/telegraf-session-local/actions)
[![NPM downloads/month](https://img.shields.io/npm/dm/telegraf-session-local.svg?style=flat-square)](https://npmcharts.com/compare/telegraf-session-local,telegraf-session-redis,telegraf-session-dynamodb,telegraf-postgres-session,telegraf-session-mysql,telegraf-session-mongoose,telegraf-session-mongo,telegraf-session-rethinkdb?interval=30)
[![GitHub Actions Status](https://img.shields.io/github/actions/workflow/status/RealSpeaker/telegraf-session-local/ci.yml?style=flat-square)](https://github.com/RealSpeaker/telegraf-session-local/actions)
[![Coveralls](https://img.shields.io/coveralls/github/RealSpeaker/telegraf-session-local/master.svg?style=flat-square)](https://coveralls.io/github/RealSpeaker/telegraf-session-local?branch=master)
[![LGTM Grade](https://img.shields.io/lgtm/grade/javascript/g/RealSpeaker/telegraf-session-local.svg?style=flat-square&?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/RealSpeaker/telegraf-session-local/context:javascript)
[![David](https://img.shields.io/david/RealSpeaker/telegraf-session-local.svg?style=flat-square)](https://david-dm.org/RealSpeaker/telegraf-session-local)
![GitHub last commit](https://img.shields.io/github/last-commit/RealSpeaker/telegraf-session-local?style=flat-square)

> Middleware for locally stored sessions & database
Expand Down Expand Up @@ -43,16 +42,16 @@ bot.use((new LocalSession({ database: 'example_db.json' })).middleware())
bot.on('text', (ctx, next) => {
ctx.session.counter = ctx.session.counter || 0
ctx.session.counter++
ctx.replyWithMarkdown(`Counter updated, new value: \`${ctx.session.counter}\``)
ctx.replyWithMarkdownV2(`Counter updated, new value: \`${ctx.session.counter}\``)
return next()
})

bot.command('/stats', (ctx) => {
ctx.replyWithMarkdown(`Database has \`${ctx.session.counter}\` messages from @${ctx.from.username || ctx.from.id}`)
ctx.replyWithMarkdownV2(`Database has \`${ctx.session.counter}\` messages from @${ctx.from.username || ctx.from.id}`)
})

bot.command('/remove', (ctx) => {
ctx.replyWithMarkdown(`Removing session from database: \`${JSON.stringify(ctx.session)}\``)
ctx.replyWithMarkdownV2(`Removing session from database: \`${JSON.stringify(ctx.session)}\``)
// Setting session to null, undefined or empty object/array will trigger removing it from database
ctx.session = null
})
Expand All @@ -68,9 +67,6 @@ const LocalSession = require('telegraf-session-local')

const bot = new Telegraf(process.env.BOT_TOKEN) // Your Bot token here

// Name of session property object in Telegraf Context (default: 'session')
const property = 'data'

const localSession = new LocalSession({
// Database name/path, where sessions will be located (default: 'sessions.json')
database: 'example_db.json',
Expand All @@ -94,30 +90,28 @@ localSession.DB.then(DB => {
// console.log(DB.get('sessions').getById('1:1').value())
})

// Telegraf will use `telegraf-session-local` configured above middleware with overrided `property` value: `data`, instead of `session`
bot.use(localSession.middleware(property))
// Telegraf will use `telegraf-session-local` configured above middleware
bot.use(localSession.middleware())

bot.on('text', (ctx, next) => {
ctx[property].counter = ctx[property].counter || 0
ctx[property].counter++
ctx.replyWithMarkdown(`Counter updated, new value: \`${ctx[property].counter}\``)
ctx.session.counter = ctx.session.counter || 0
ctx.session.counter++
ctx.replyWithMarkdownV2(`Counter updated, new value: \`${ctx.session.counter}\``)
// Writing message to Array `messages` into database which already has sessions Array
ctx[property + 'DB'].get('messages').push([ctx.message]).write()
// `property`+'DB' is a name of property which contains lowdb instance, default = `sessionDB`, in current example = `dataDB`
// ctx.dataDB.get('messages').push([ctx.message]).write()
ctx.sessionDB.get('messages').push([ctx.message]).write()
// `property`+'DB' is a name of ctx property which contains lowdb instance, default = `sessionDB`

return next()
})

bot.command('/stats', (ctx) => {
let msg = `Using session object from [Telegraf Context](http://telegraf.js.org/context.html) (\`ctx\`), named \`${property}\`\n`
msg += `Database has \`${ctx[property].counter}\` messages from @${ctx.from.username || ctx.from.id}`
ctx.replyWithMarkdown(msg)
ctx.replyWithMarkdownV2(`Session has \`${ctx.session.counter}\` messages from @${ctx.from.username || ctx.from.id}`)
})

bot.command('/remove', (ctx) => {
ctx.replyWithMarkdown(`Removing session from database: \`${JSON.stringify(ctx[property])}\``)
ctx.replyWithMarkdownV2(`Removing session from lowdb database: \`${JSON.stringify(ctx.session)}\``)
// Setting session to null, undefined or empty object/array will trigger removing it from database
ctx[property] = null
ctx.session = null
})

bot.launch()
Expand Down
Loading

0 comments on commit 55b4ac1

Please sign in to comment.