Skip to content

Commit

Permalink
add mention to keyboards if in a group
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Apr 1, 2018
1 parent 3be8966 commit 95f3225
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 17 deletions.
9 changes: 6 additions & 3 deletions commands/departures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

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

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

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

Expand Down
11 changes: 6 additions & 5 deletions commands/journeys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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
}

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

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

Expand Down
15 changes: 12 additions & 3 deletions lib/frequent-stations-keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down
11 changes: 11 additions & 0 deletions lib/when-keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 95f3225

Please sign in to comment.