Skip to content

Commit

Permalink
strip mention from messages & commands
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Apr 1, 2018
1 parent 133cb7e commit 2f8e257
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
4 changes: 2 additions & 2 deletions commands/departures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const departures = async (ctx, next) => {

let where = await ctx.storage.getData('where')
if (!where) {
where = await parseWhere(ctx.message.text, ctx)
where = await parseWhere(ctx.message.textWithoutMention, ctx)
if (!where) return next() // await next message
await ctx.storage.putData('where', where)
await ctx.storage.incLocation(where.id)
Expand All @@ -80,7 +80,7 @@ const departures = async (ctx, next) => {

let when = await ctx.storage.getData('when')
if (!when) {
when = await parseWhen(ctx.message.text, ctx)
when = await parseWhen(ctx.message.textWithoutMention, ctx)
if (!when) return next() // await next message
await ctx.storage.putData('when', when)
}
Expand Down
8 changes: 4 additions & 4 deletions commands/journeys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ const textOrLocation = `\
Only location names like "U mehringdamm", "Kaiserdamm 26" and locations are supported.`

const parseWhere = async (msg, ctx) => {
if (msg.text) {
let [loc] = searchStations(msg.text, 1, false, false) // non-fuzzy
if (msg.textWithoutMention) {
let [loc] = searchStations(msg.textWithoutMention, 1, false, false) // non-fuzzy
if (loc) {
loc = allStations.find(s => s.id === loc.id) // get details
} else {
[loc] = await hafas.locations(msg.text, {results: 1})
[loc] = await hafas.locations(msg.textWithoutMention, {results: 1})
}
const text = loc ? `I found ${loc.name || loc.address}.` : unknownStation
await ctx.replyWithMarkdown(text)
Expand Down Expand Up @@ -103,7 +103,7 @@ const journeys = async (ctx, next) => {

let when = await ctx.storage.getData('when')
if (!when) {
when = await parseWhen(ctx.message.text, ctx)
when = await parseWhen(ctx.message.textWithoutMention, ctx)
if (!when) return next() // await next message
await ctx.storage.putData('when', when)
}
Expand Down
3 changes: 2 additions & 1 deletion 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 textWithoutMention = require('./lib/text-without-mention')
const logging = require('./lib/logging')
const session = require('./lib/session')
const storage = require('./lib/storage')
Expand Down Expand Up @@ -31,11 +32,11 @@ const pathToDb = path.join(__dirname, 'vbb-telegram.ldb')

const bot = new Bot(TOKEN)
bot.use(logging)
bot.use(textWithoutMention)
bot.use(session(pathToDb))
bot.use(command)
bot.use(storage)
bot.use((ctx, next) => {
if (!ctx.message) return next()
const cmd = ctx.command || ctx.prevCommand || 'help'
ctx.storage.getData = ctx.storage.createGetData(cmd)
ctx.storage.putData = ctx.storage.createPutData(cmd)
Expand Down
16 changes: 14 additions & 2 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

const invisibleSpace = '\u2063'

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

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

const commandMiddleware = async (ctx, next) => {
if (!ctx.message) return next()
if (!ctx.session) return Promise.reject('ctx.session is missing')
Expand All @@ -10,9 +18,13 @@ const commandMiddleware = async (ctx, next) => {
ctx.prevCommand = await ctx.session.get('cmd')

if (!Array.isArray(msg.entities)) return next()
const entity = msg.entities.find(e => e.type === 'bot_command' && e.offset === 0)
const entity = msg.entities.find(e => e.type === 'bot_command')
if (!entity) return next()
const cmd = msg.text.substr(entity.offset, entity.length).slice(1).toLowerCase()
const cmd = msg.text
.substr(entity.offset, entity.length)
.slice(1)
.toLowerCase()
.replace(mention, '')
ctx.command = cmd

const argsStart = entity.offset + entity.length + 1
Expand Down
43 changes: 43 additions & 0 deletions lib/text-without-mention.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

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

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

// '/foo@bot bar' -> '/foo bar'
// '/foo@bot' -> '/foo'
// '/foo @bot bar' -> '/foo bar'
// '/foo @bot' -> '/foo'
// '@bot foo' -> 'foo'
// '@bot' -> ''
// '@bot /foo bar' -> '/foo bar'
// ' @bot /foo bar' -> ' /foo bar'
const textWithoutMention = (ctx, next) => {
if (!ctx.message || !ctx.message.text) { // ignore
next()
return
}
const text = ctx.message.text

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)
next()
}

module.exports = textWithoutMention

0 comments on commit 2f8e257

Please sign in to comment.