Skip to content

Commit

Permalink
wip: command mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 7, 2018
1 parent 3fb57b2 commit e156467
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
11 changes: 11 additions & 0 deletions commands/departures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const commandsKeyboard = require('../lib/commands-keyboard')

const departures = async (ctx, next) => {
await ctx.replyWithMarkdown('`departures` command', commandsKeyboard)
// todo
next()
}

module.exports = departures
23 changes: 23 additions & 0 deletions commands/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

const commandsKeyboard = require('../lib/commands-keyboard')

const text = `\
*This bot lets you use public transport in Berlin more easily.* You can do this:
\`/a(bfahrt)\` – Show departures at a station.
\`/r(oute)\` – Get routes from A to B.
\`/n(earby)\` – Show stations around.
When specifying time, you can use the following formats:
- \`now\`
- \`in 10min\`
- \`tomorrow 17:20\`
- \`8 pm\`
- \`tuesday at 6\`
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)
next()
}

module.exports = help
11 changes: 11 additions & 0 deletions commands/journeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const commandsKeyboard = require('../lib/commands-keyboard')

const journeys = async (ctx, next) => {
await ctx.replyWithMarkdown('`journeys` command', commandsKeyboard)
// todo
next()
}

module.exports = journeys
11 changes: 11 additions & 0 deletions commands/nearby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const commandsKeyboard = require('../lib/commands-keyboard')

const nearby = async (ctx, next) => {
await ctx.replyWithMarkdown('`nearby` command', commandsKeyboard)
// todo
next()
}

module.exports = nearby
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ const logging = require('./lib/logging')
const storage = require('./lib/storage')
const command = require('./lib/command')

const help = require('./commands/help')
const departures = require('./commands/departures')
const journeys = require('./commands/journeys')
const nearby = require('./commands/nearby')

const commands = {
help, h: help,
abfahrt: departures, a: departures,
route: journeys, r: journeys,
nearby, n: nearby
}

const TOKEN = process.env.TOKEN
if (!TOKEN) {
console.error('Missing TOKEN env var.')
Expand All @@ -15,5 +27,17 @@ if (!TOKEN) {
const bot = new Bot(TOKEN)
bot.use(logging)
bot.use(command)
bot.use((ctx, next) => {
const {cmd, prevCmd} = ctx.state

let handler = commands.help
if (cmd) {
if (commands[cmd]) handler = commands[cmd]
} else if (prevCmd) {
if (commands[prevCmd]) handler = commands[prevCmd]
}

return handler(ctx, next)
})

bot.startPolling() // todo: web hook
13 changes: 13 additions & 0 deletions lib/commands-keyboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

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 ')
]

const commandsKeyboard = Markup.keyboard(keys).oneTime().extra()

module.exports = commandsKeyboard

0 comments on commit e156467

Please sign in to comment.