Skip to content

Commit

Permalink
in groups, only react to mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Apr 1, 2018
1 parent 2f8e257 commit 60d9084
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path')
const Bot = require('telegraf')
const url = require('url')

const inGroupsOnlyMentions = require('./lib/in-groups-only-mentions')
const textWithoutMention = require('./lib/text-without-mention')
const logging = require('./lib/logging')
const session = require('./lib/session')
Expand Down Expand Up @@ -31,6 +32,8 @@ if (!TOKEN) {
const pathToDb = path.join(__dirname, 'vbb-telegram.ldb')

const bot = new Bot(TOKEN)

bot.use(inGroupsOnlyMentions)
bot.use(logging)
bot.use(textWithoutMention)
bot.use(session(pathToDb))
Expand Down
23 changes: 23 additions & 0 deletions lib/in-groups-only-mentions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const HANDLE = process.env.HANDLE
if (!HANDLE) {
console.error('Missing HANDLE env var.')
process.exit(1)
}

const mention = ('@' + HANDLE).toLowerCase()

const inGroupsOnlyMentions = (ctx, next) => {
if (!ctx.chat || !ctx.chat.type || !ctx.message) return null
if (ctx.chat.type === 'group') {
const text = ctx.message.text
if (text) {
const i = text.toLowerCase().indexOf(mention)
if (i < 0) return null // no mention
}
}
next()
}

module.exports = inGroupsOnlyMentions

0 comments on commit 60d9084

Please sign in to comment.