Skip to content

Commit

Permalink
wip: journeys part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 8, 2018
1 parent 0933f2f commit a3392da
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
10 changes: 9 additions & 1 deletion commands/departures/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict'

const searchStations = require('vbb-stations-autocomplete')
const allStations = require('vbb-stations/simple')
const parseTime = require('parse-messy-time')
const linesAt = require('vbb-lines-at')
const hafas = require('vbb-hafas')

const findStation = require('../../lib/find-station')
const commandKeys = require('../../lib/commands-keyboard')
const whenKeys = require('../../lib/when-keyboard')
const getFrequentStationsKeys = require('../../lib/frequent-stations-keyboard')
Expand All @@ -23,6 +24,13 @@ const unknownStation = `\
I don't know about this station, please double-check for typos.
If you're sure it's my fault, please let my creator @derhuerst know.`

const findStation = (name) => {
let [station] = searchStations(name, 1, false, false) // non-fuzzy
if (!station) [station] = searchStations(name, 1, true, false) // fuzzy
if (station) station = allStations.find(s => s.id === station.id) // get details
return station
}

const parseWhere = async (where, ctx) => {
const station = findStation(where)
if (station) await ctx.replyWithMarkdown(`I found ${station.name}.`)
Expand Down
34 changes: 27 additions & 7 deletions commands/journeys/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict'

const searchStations = require('vbb-stations-autocomplete')
const allStations = require('vbb-stations/simple')
const parseTime = require('parse-messy-time')
const Markup = require('telegraf/markup')
const hafas = require('vbb-hafas')

const findStation = require('../../lib/find-station')
const commandKeys = require('../../lib/commands-keyboard')
const whenKeys = require('../../lib/when-keyboard')
const getFrequentStationsKeys = require('../../lib/frequent-stations-keyboard')
Expand All @@ -21,12 +22,31 @@ const promptOrigin = `\
Enter a location like "U mehringdamm", "Kaiserdamm 26" or send your location.`
const promptDest = `\
*Where do you want to go?*`
// const unknownStation = `\
// I don't know about this station, please double-check for typos.
// If you're sure it's my fault, please let my creator @derhuerst know.`

const parseWhere = async (where, ctx) => {
// todo
const unknownStation = `\
I don't know about this station, please double-check for typos.
If you're sure it's my fault, please let my creator @derhuerst know.`
const textOrLocation = `\
Only location names like "U mehringdamm", "Kaiserdamm 26" and locations are supported.`

const parseWhere = async (msg, ctx) => {
if (msg.text) {
let [loc] = searchStations(msg.text, 1, false, false) // non-fuzzy
if (loc) {
loc = allStations.find(s => s.id === loc.id) // get details
} else {
[loc] = await hafas.locations(msg.text, {results: 1})
}
const text = loc ? `I found ${loc.name || loc.address}.` : unknownStation
await ctx.replyWithMarkdown(text)
return loc
} else if (msg.location) {
return {
type: 'location',
address: 'current location',
latitude: msg.location.latitude,
longitude: msg.location.longitude
}
} else ctx.replyWithMarkdown(textOrLocation)
}

const parseWhen = async (when, ctx) => {
Expand Down
13 changes: 0 additions & 13 deletions lib/find-station.js

This file was deleted.

0 comments on commit a3392da

Please sign in to comment.