From 60d9084713942c719c7577ff644ae3397ab08b59 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 1 Apr 2018 21:51:24 +0200 Subject: [PATCH] in groups, only react to mentions --- index.js | 3 +++ lib/in-groups-only-mentions.js | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 lib/in-groups-only-mentions.js diff --git a/index.js b/index.js index 9207712..a81c9d4 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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)) diff --git a/lib/in-groups-only-mentions.js b/lib/in-groups-only-mentions.js new file mode 100644 index 0000000..3da78d0 --- /dev/null +++ b/lib/in-groups-only-mentions.js @@ -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