Skip to content

Commit

Permalink
in groups, react to replies as well
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Apr 1, 2018
1 parent a0ccb95 commit 70a7b1d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
7 changes: 4 additions & 3 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 16 additions & 14 deletions lib/text-without-mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 70a7b1d

Please sign in to comment.