From e156467c37cd3d575d299330c6ed7fe0a78bdf85 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Wed, 7 Mar 2018 18:21:54 +0100 Subject: [PATCH] wip: command mocks --- commands/departures.js | 11 +++++++++++ commands/help.js | 23 +++++++++++++++++++++++ commands/journeys.js | 11 +++++++++++ commands/nearby.js | 11 +++++++++++ index.js | 24 ++++++++++++++++++++++++ lib/commands-keyboard.js | 13 +++++++++++++ 6 files changed, 93 insertions(+) create mode 100644 commands/departures.js create mode 100644 commands/help.js create mode 100644 commands/journeys.js create mode 100644 commands/nearby.js create mode 100644 lib/commands-keyboard.js diff --git a/commands/departures.js b/commands/departures.js new file mode 100644 index 0000000..a49d5f1 --- /dev/null +++ b/commands/departures.js @@ -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 diff --git a/commands/help.js b/commands/help.js new file mode 100644 index 0000000..ff489ba --- /dev/null +++ b/commands/help.js @@ -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 diff --git a/commands/journeys.js b/commands/journeys.js new file mode 100644 index 0000000..c8fe7d5 --- /dev/null +++ b/commands/journeys.js @@ -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 diff --git a/commands/nearby.js b/commands/nearby.js new file mode 100644 index 0000000..64a981e --- /dev/null +++ b/commands/nearby.js @@ -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 diff --git a/index.js b/index.js index 8f09907..2011ecc 100644 --- a/index.js +++ b/index.js @@ -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.') @@ -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 diff --git a/lib/commands-keyboard.js b/lib/commands-keyboard.js new file mode 100644 index 0000000..dd58188 --- /dev/null +++ b/lib/commands-keyboard.js @@ -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