Skip to content

Commit

Permalink
make keyboard helpers functions
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Apr 1, 2018
1 parent 60d9084 commit 3be8966
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
8 changes: 4 additions & 4 deletions commands/departures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions commands/help.js
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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()
}

Expand Down
8 changes: 4 additions & 4 deletions commands/journeys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
}

Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions commands/nearby.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 8 additions & 5 deletions lib/commands-keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 10 additions & 3 deletions lib/frequent-stations-keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 5 additions & 2 deletions lib/when-keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 3be8966

Please sign in to comment.