diff --git a/commands/departures/index.js b/commands/departures/index.js index 688e5da..5a5a510 100644 --- a/commands/departures/index.js +++ b/commands/departures/index.js @@ -6,8 +6,8 @@ const parseTime = require('parse-messy-time') const linesAt = require('vbb-lines-at') const hafas = require('vbb-hafas') -const commandKeys = require('../../lib/commands-keyboard') -const whenKeys = require('../../lib/when-keyboard') +const getCommandKeys = require('../../lib/commands-keyboard') +const getWhenKeys = require('../../lib/when-keyboard') const getFrequentStationsKeys = require('../../lib/frequent-stations-keyboard') const renderDeps = require('./render') @@ -50,7 +50,7 @@ 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), commandKeys) + await ctx.replyWithMarkdown(renderDeps(deps), getCommandKeys()) } } @@ -74,7 +74,7 @@ const departures = async (ctx, next) => { await ctx.storage.putData('where', where) await ctx.storage.incLocation(where.id) - await ctx.replyWithMarkdown(promptWhen, whenKeys) + await ctx.replyWithMarkdown(promptWhen, getWhenKeys()) return next() // await next message } diff --git a/commands/help.js b/commands/help.js index ff489ba..6bae558 100644 --- a/commands/help.js +++ b/commands/help.js @@ -1,6 +1,6 @@ 'use strict' -const commandsKeyboard = require('../lib/commands-keyboard') +const getCommandKeys = require('../lib/commands-keyboard') const text = `\ *This bot lets you use public transport in Berlin more easily.* You can do this: @@ -16,7 +16,7 @@ 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, commandsKeyboard) + await ctx.replyWithMarkdown(text, getCommandKeys()) next() } diff --git a/commands/journeys/index.js b/commands/journeys/index.js index 403981f..2172f72 100644 --- a/commands/journeys/index.js +++ b/commands/journeys/index.js @@ -6,8 +6,8 @@ const parseTime = require('parse-messy-time') const Markup = require('telegraf/markup') const hafas = require('vbb-hafas') -const commandKeys = require('../../lib/commands-keyboard') -const whenKeys = require('../../lib/when-keyboard') +const getCommandKeys = require('../../lib/commands-keyboard') +const getWhenKeys = require('../../lib/when-keyboard') const getFrequentStationsKeys = require('../../lib/frequent-stations-keyboard') const renderJourney = require('./render') @@ -97,7 +97,7 @@ const journeys = async (ctx, next) => { await ctx.storage.putData('destination', destination) await ctx.storage.incLocation(destination.id) - await ctx.replyWithMarkdown(promptWhen, whenKeys) + await ctx.replyWithMarkdown(promptWhen, getWhenKeys()) return next() // await next message } @@ -119,7 +119,7 @@ const journeys = async (ctx, next) => { await ctx.replyWithChatAction('typing') const journeys = await hafas.journeys(origin, destination, {when}) for (let j of journeys) { - await ctx.replyWithMarkdown(renderJourney(j), commandKeys) + await ctx.replyWithMarkdown(renderJourney(j), getCommandKeys()) } next() diff --git a/commands/nearby.js b/commands/nearby.js index c2f43aa..4116239 100644 --- a/commands/nearby.js +++ b/commands/nearby.js @@ -3,7 +3,7 @@ const Markup = require('telegraf/markup') const hafas = require('vbb-hafas') -const commandKeys = require('../lib/commands-keyboard') +const getCommandKeys = require('../lib/commands-keyboard') const promptWhere = `Please share your location with me.` const locationOnly = `Please send a location, other stuff is not supported yet.` @@ -38,7 +38,7 @@ 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, commandKeys) + if (i === (l - 1)) await ctx.replyWithMarkdown(text, getCommandKeys()) else await ctx.replyWithMarkdown(text) } next() diff --git a/lib/commands-keyboard.js b/lib/commands-keyboard.js index dd58188..9f548a2 100644 --- a/lib/commands-keyboard.js +++ b/lib/commands-keyboard.js @@ -3,11 +3,14 @@ const Markup = require('telegraf/markup') const keys = [ - ['/a', 'Show departures at a station.'].join('\u2063 '), - ['/r', 'Get routes from A to B.'].join('\u2063 '), - ['/n', 'Show stations around your location.'].join('\u2063 ') + ['/a', 'Show departures at a station.'], + ['/r', 'Get routes from A to B.'], + ['/n', 'Show stations around your location.'] ] -const commandsKeyboard = Markup.keyboard(keys).oneTime().extra() +const getCommandsKeyboard = () => { + const k = keys.map(key => key.join('\u2063 ')) + return Markup.keyboard(k).oneTime().selective(true).extra() +} -module.exports = commandsKeyboard +module.exports = getCommandsKeyboard diff --git a/lib/frequent-stations-keyboard.js b/lib/frequent-stations-keyboard.js index 9f59947..83458e8 100644 --- a/lib/frequent-stations-keyboard.js +++ b/lib/frequent-stations-keyboard.js @@ -6,11 +6,18 @@ const Markup = require('telegraf/markup') const namesById = Object.create(null) for (let station of stations) namesById[station.id] = station.name -const getFrequentStationsKeyboard = (ids, keys = []) => { +const getFrequentStationsKeyboard = (ids, reqLocation) => { + const keys = [] + if (reqLocation) { + keys.push(Markup.locationRequestButton('use current location')) + } for (let id of ids) { - if (id in namesById) keys.push(Markup.button(namesById[id])) + if (id in namesById) { + const name = namesById[id] + keys.push(Markup.button(name)) + } } - return Markup.keyboard(keys).extra() + return Markup.keyboard(keys).selective(true).extra() } module.exports = getFrequentStationsKeyboard diff --git a/lib/when-keyboard.js b/lib/when-keyboard.js index 48143bb..e3f4a96 100644 --- a/lib/when-keyboard.js +++ b/lib/when-keyboard.js @@ -4,6 +4,9 @@ const Markup = require('telegraf/markup') const keys = ['now', 'in 10 min', 'in 1 hour'] -const whenKeyboard = Markup.keyboard(keys).extra() +const getWhenKeyboard = (group) => { + let k = keys + return Markup.keyboard(k).selective(true).extra() +} -module.exports = whenKeyboard +module.exports = getWhenKeyboard