From 70a7b1d0e5519a677a84baa242f4d4497c13131e Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 1 Apr 2018 22:52:07 +0200 Subject: [PATCH] in groups, react to replies as well --- commands/help.js | 7 +++-- index.js | 4 +-- ...=> in-groups-only-mentions-and-replies.js} | 12 +++++--- lib/text-without-mention.js | 30 ++++++++++--------- 4 files changed, 30 insertions(+), 23 deletions(-) rename lib/{in-groups-only-mentions.js => in-groups-only-mentions-and-replies.js} (55%) diff --git a/commands/help.js b/commands/help.js index 7625902..611a4a1 100644 --- a/commands/help.js +++ b/commands/help.js @@ -21,9 +21,10 @@ When specifying time, you can use the following formats: - \`tuesday at 6\` The data behind this bot is from VBB, so departures & routing will be just as (in)accurate as in the BVG & VBB apps. -**When using this bot in a group**, telegram prevents it from listening to all messages. In this case you need to mention the bot in every message. For example: -1. \`/a@${HANDLE} U Friedrichstr.\` -2. \`@${HANDLE} in 10 min\`` +**When using this bot in a group**, telegram prevents it from listening to normal messages. In this case you need to have two choices: + +- Mention the bot in every message. For example: \`/a@${HANDLE} U Friedrichstr.\` and later \`@${HANDLE} in 10 min\`. +- Always reply to the bot with the reply feature.` const help = async (ctx, next) => { const group = ctx.chat.type === 'group' diff --git a/index.js b/index.js index 04b9db2..317bc43 100644 --- a/index.js +++ b/index.js @@ -4,7 +4,7 @@ const path = require('path') const Bot = require('telegraf') const url = require('url') -const inGroupsOnlyMentions = require('./lib/in-groups-only-mentions') +const inGroupsOnlyMentionsAndReplies = require('./lib/in-groups-only-mentions-and-replies') const textWithoutMention = require('./lib/text-without-mention') const logging = require('./lib/logging') const session = require('./lib/session') @@ -33,7 +33,7 @@ const pathToDb = path.join(__dirname, 'vbb-telegram.ldb') const bot = new Bot(TOKEN) -bot.use(inGroupsOnlyMentions) +bot.use(inGroupsOnlyMentionsAndReplies) bot.use(textWithoutMention) bot.use(logging) bot.use(session(pathToDb)) diff --git a/lib/in-groups-only-mentions.js b/lib/in-groups-only-mentions-and-replies.js similarity index 55% rename from lib/in-groups-only-mentions.js rename to lib/in-groups-only-mentions-and-replies.js index 3da78d0..d77d5c7 100644 --- a/lib/in-groups-only-mentions.js +++ b/lib/in-groups-only-mentions-and-replies.js @@ -8,16 +8,20 @@ if (!HANDLE) { const mention = ('@' + HANDLE).toLowerCase() -const inGroupsOnlyMentions = (ctx, next) => { +const inGroupsOnlyMentionsAndReplies = (ctx, next) => { if (!ctx.chat || !ctx.chat.type || !ctx.message) return null if (ctx.chat.type === 'group') { + const replyMsg = ctx.message.reply_to_message + const isReply = replyMsg && replyMsg.from.username === HANDLE const text = ctx.message.text - if (text) { + if (!isReply && text) { const i = text.toLowerCase().indexOf(mention) - if (i < 0) return null // no mention + if (i < 0) { + return null // no mention + } } } next() } -module.exports = inGroupsOnlyMentions +module.exports = inGroupsOnlyMentionsAndReplies diff --git a/lib/text-without-mention.js b/lib/text-without-mention.js index ad0f630..8271a0c 100644 --- a/lib/text-without-mention.js +++ b/lib/text-without-mention.js @@ -21,22 +21,24 @@ const textWithoutMention = (ctx, next) => { next() return } - const text = ctx.message.text + const text = (ctx.message.text + '').toLowerCase() const i = text.indexOf(mention) - if (i < 0) return null - - const afterSpace = text[i - 1] === ' ' - const atTheStart = i === 0 - const beforeSpace = text[i + mention.length] === ' ' - const atTheEnd = text.length === (i + mention.length) - - let start = i, end = i + mention.length - if (afterSpace && atTheEnd) start-- - else if (beforeSpace && atTheStart) end++ - else if (beforeSpace && afterSpace) end++ - - ctx.message.textWithoutMention = text.slice(0, start) + text.slice(end) + if (i < 0) { + ctx.message.textWithoutMention = text + } else { + const afterSpace = text[i - 1] === ' ' + const atTheStart = i === 0 + const beforeSpace = text[i + mention.length] === ' ' + const atTheEnd = text.length === (i + mention.length) + + let start = i, end = i + mention.length + if (afterSpace && atTheEnd) start-- + else if (beforeSpace && atTheStart) end++ + else if (beforeSpace && afterSpace) end++ + + ctx.message.textWithoutMention = text.slice(0, start) + text.slice(end) + } next() }