diff --git a/commands/departures/index.js b/commands/departures/index.js index 5a5a510..15266cc 100644 --- a/commands/departures/index.js +++ b/commands/departures/index.js @@ -50,7 +50,8 @@ const parseWhen = async (when, ctx) => { const printDeps = async (allDeps, ctx) => { for (let i = 0; i < allDeps.length; i += 10) { const deps = allDeps.slice(i, i + 10) - await ctx.replyWithMarkdown(renderDeps(deps), getCommandKeys()) + const group = ctx.chat.type === 'group' + await ctx.replyWithMarkdown(renderDeps(deps), getCommandKeys(group)) } } @@ -63,7 +64,8 @@ const departures = async (ctx, next) => { } if (ctx.command) { const ids = await ctx.storage.getTopLocations() - await ctx.replyWithMarkdown(promptWhere, await getFrequentStationsKeys(ids)) + const group = ctx.chat.type === 'group' + await ctx.replyWithMarkdown(promptWhere, await getFrequentStationsKeys(ids, group)) return next() // await next message } @@ -74,7 +76,8 @@ const departures = async (ctx, next) => { await ctx.storage.putData('where', where) await ctx.storage.incLocation(where.id) - await ctx.replyWithMarkdown(promptWhen, getWhenKeys()) + const group = ctx.chat.type === 'group' + await ctx.replyWithMarkdown(promptWhen, getWhenKeys(group)) return next() // await next message } diff --git a/commands/help.js b/commands/help.js index 6bae558..b1b79a2 100644 --- a/commands/help.js +++ b/commands/help.js @@ -16,7 +16,8 @@ When specifying time, you can use the following formats: The data behind this bot is from VBB, so departures & routing will be just as (in)accurate as in the BVG & VBB apps.` const help = async (ctx, next) => { - await ctx.replyWithMarkdown(text, getCommandKeys()) + const group = ctx.chat.type === 'group' + await ctx.replyWithMarkdown(text, getCommandKeys(group)) next() } diff --git a/commands/journeys/index.js b/commands/journeys/index.js index 2172f72..9cba010 100644 --- a/commands/journeys/index.js +++ b/commands/journeys/index.js @@ -60,9 +60,8 @@ const parseWhen = async (when, ctx) => { const getFrequentStationKeys = async (ctx) => { const ids = await ctx.storage.getTopLocations() - return getFrequentStationsKeys(ids, [ - Markup.locationRequestButton('use current location') - ]) + const group = ctx.chat.type === 'group' + return getFrequentStationsKeys(ids, group, true) } const journeys = async (ctx, next) => { @@ -97,7 +96,8 @@ const journeys = async (ctx, next) => { await ctx.storage.putData('destination', destination) await ctx.storage.incLocation(destination.id) - await ctx.replyWithMarkdown(promptWhen, getWhenKeys()) + const group = ctx.chat.type === 'group' + await ctx.replyWithMarkdown(promptWhen, getWhenKeys(group)) return next() // await next message } @@ -118,8 +118,9 @@ const journeys = async (ctx, next) => { // fetch & render journeys await ctx.replyWithChatAction('typing') const journeys = await hafas.journeys(origin, destination, {when}) + const group = ctx.chat.type === 'group' for (let j of journeys) { - await ctx.replyWithMarkdown(renderJourney(j), getCommandKeys()) + await ctx.replyWithMarkdown(renderJourney(j), getCommandKeys(group)) } next() diff --git a/commands/nearby.js b/commands/nearby.js index 4116239..03ab356 100644 --- a/commands/nearby.js +++ b/commands/nearby.js @@ -11,14 +11,17 @@ const locationOnly = `Please send a location, other stuff is not supported yet.` const keyboard = Markup.keyboard([ Markup.locationRequestButton('send current location') ]).extra() +const noKeyboard = Markup.keyboard([]).extra() const nearby = async (ctx, next) => { + const group = ctx.chat.type === 'group' + if (ctx.command) { - await ctx.replyWithMarkdown(promptWhere, keyboard) + await ctx.replyWithMarkdown(promptWhere, group ? noKeyboard : keyboard) return next() } if (!ctx.message.location) { - await ctx.replyWithMarkdown(locationOnly, keyboard) + await ctx.replyWithMarkdown(locationOnly, group ? noKeyboard : keyboard) return next() } @@ -38,7 +41,8 @@ const nearby = async (ctx, next) => { await ctx.replyWithLocation(s.location.latitude, s.location.longitude) // todo: link to departures, link to routes const text = `${s.distance}m *${s.name}*` - if (i === (l - 1)) await ctx.replyWithMarkdown(text, getCommandKeys()) + const group = ctx.chat.type === 'group' + if (i === (l - 1)) await ctx.replyWithMarkdown(text, getCommandKeys(group)) else await ctx.replyWithMarkdown(text) } next() diff --git a/lib/commands-keyboard.js b/lib/commands-keyboard.js index 9f548a2..040aba2 100644 --- a/lib/commands-keyboard.js +++ b/lib/commands-keyboard.js @@ -2,14 +2,29 @@ const Markup = require('telegraf/markup') +const HANDLE = process.env.HANDLE +if (!HANDLE) { + console.error('Missing HANDLE env var.') + process.exit(1) +} + +const mention = ('@' + HANDLE).toLowerCase() + const keys = [ ['/a', 'Show departures at a station.'], ['/r', 'Get routes from A to B.'], ['/n', 'Show stations around your location.'] ] -const getCommandsKeyboard = () => { - const k = keys.map(key => key.join('\u2063 ')) +const getCommandsKeyboard = (group) => { + let k = keys + if (group) { + k = k.map(key => [ + key[0] + mention, + ...key.slice(1) + ]) + } + k = k.map(key => key.join('\u2063 ')) return Markup.keyboard(k).oneTime().selective(true).extra() } diff --git a/lib/frequent-stations-keyboard.js b/lib/frequent-stations-keyboard.js index 83458e8..f2aa69c 100644 --- a/lib/frequent-stations-keyboard.js +++ b/lib/frequent-stations-keyboard.js @@ -3,17 +3,26 @@ const stations = require('vbb-stations/simple') const Markup = require('telegraf/markup') +const HANDLE = process.env.HANDLE +if (!HANDLE) { + console.error('Missing HANDLE env var.') + process.exit(1) +} + +const mention = ('@' + HANDLE).toLowerCase() + const namesById = Object.create(null) for (let station of stations) namesById[station.id] = station.name -const getFrequentStationsKeyboard = (ids, reqLocation) => { +const getFrequentStationsKeyboard = (ids, group, reqLocation) => { const keys = [] - if (reqLocation) { + if (reqLocation && !group) { keys.push(Markup.locationRequestButton('use current location')) } for (let id of ids) { if (id in namesById) { - const name = namesById[id] + let name = namesById[id] + if (group) name = mention + ' ' + name keys.push(Markup.button(name)) } } diff --git a/lib/when-keyboard.js b/lib/when-keyboard.js index e3f4a96..9541036 100644 --- a/lib/when-keyboard.js +++ b/lib/when-keyboard.js @@ -2,10 +2,21 @@ const Markup = require('telegraf/markup') +const HANDLE = process.env.HANDLE +if (!HANDLE) { + console.error('Missing HANDLE env var.') + process.exit(1) +} + +const mention = ('@' + HANDLE).toLowerCase() + const keys = ['now', 'in 10 min', 'in 1 hour'] const getWhenKeyboard = (group) => { let k = keys + if (group) { + k = k.map(key => mention + ' ' + key) + } return Markup.keyboard(k).selective(true).extra() }