From bdedf8984d56df959867887d5f5f2009719c6f27 Mon Sep 17 00:00:00 2001 From: Kiros Choi Date: Thu, 3 Sep 2020 12:07:44 +0800 Subject: [PATCH] Complete LRT arrival time check --- lib/company/lrt.js | 2 +- lib/keyboard.js | 29 ++++++++++++++++++++++- lib/scenes/bus.js | 1 + lib/scenes/lrt.js | 58 +++++++++++++++++++++++++++++++++++++++++++--- lib/scenes/mtr.js | 1 + 5 files changed, 86 insertions(+), 5 deletions(-) diff --git a/lib/company/lrt.js b/lib/company/lrt.js index 6d9c51f..288294b 100644 --- a/lib/company/lrt.js +++ b/lib/company/lrt.js @@ -17,9 +17,9 @@ exports.getLRTETA = async (stationId) => { const GET_LRT_ETA = gql` query ETA($stationId: ID!) { LRT_platforms(stationId: $stationId) { + id etas { train_length - arrival_departure dest_ch time_ch route_no diff --git a/lib/keyboard.js b/lib/keyboard.js index b3369d1..5de4d21 100644 --- a/lib/keyboard.js +++ b/lib/keyboard.js @@ -60,7 +60,7 @@ exports.direction = (company, route, routes) => { } break; case 'MTR': - for (const {id, description_zh} of routes) { + for (const { id, description_zh } of routes) { let text = description_zh; let callback_data = callback + `,${id}`; keyboard.push([{ text, callback_data }]); @@ -166,5 +166,32 @@ exports.zones = () => { keyboard.push([{ text: zone.zone, callback_data: callback + zone.zone + ',' }]); } + return keyboard; +}; + +// Build a keyboard consists of stations of LRT +exports.lrtStations = zoneName => { + const callback = `${constants.LRT_ETA_ACTION},`; + let keyboard = []; + + const zones = readJSON('station-lrt'); + + const { stations } = zones.find(zone => zone.zone === zoneName); + for (let i = 0; i < stations.length; i += 2) { + const stationA = stations[i]; + + if (i + 1 === stations.length) { + keyboard.push([{ text: stationA.chi_name, callback_data: callback + stationA.station_id }]); + } else { + const stationB = stations[i + 1]; + const button = [ + { text: stationA.chi_name, callback_data: callback + stationA.station_id }, + { text: stationB.chi_name, callback_data: callback + stationB.station_id } + ]; + + keyboard.push(button); + } + } + return keyboard; }; \ No newline at end of file diff --git a/lib/scenes/bus.js b/lib/scenes/bus.js index 4ee43f0..77d2f5e 100644 --- a/lib/scenes/bus.js +++ b/lib/scenes/bus.js @@ -18,6 +18,7 @@ const transformResponse = [res => { }]; scene.command('mtr', ctx => ctx.scene.enter(constants.MTR_SCENE_ID)); +scene.command('lrt', ctx => ctx.scene.enter(constants.LRT_SCENE_ID)); scene.enter(ctx => readRoute(ctx)); diff --git a/lib/scenes/lrt.js b/lib/scenes/lrt.js index 26f0e14..723ff5c 100644 --- a/lib/scenes/lrt.js +++ b/lib/scenes/lrt.js @@ -17,13 +17,43 @@ scene.enter(async ctx => { let keyboard = buildKeyboard.zones(); ctx.reply( - '直接輸入站名或選擇區域🚆', - Markup.inlineKeyboard(keyboard).extra() + // '直接輸入站名或選擇區域🚆', + '選擇區域🚆', + Markup.inlineKeyboard(keyboard).extra() ); analytics.send(ctx, { ec: 'lrt', ea: 'start' }); }); +// Ask for a station of a zone +scene.action(constants.ZONES_ACTION_REGEX, ctx => { + const [, zone] = ctx.update.callback_query.data.split(','); + let keyboard = buildKeyboard.lrtStations(zone); + + ctx.reply( + '請選擇車站🚉', + Markup.inlineKeyboard(keyboard).extra() + ); +}); + +// Check for ETA +scene.action(constants.LRT_ETA_ACTION_REGEX, async ctx => { + const [, stationId] = ctx.update.callback_query.data.split(','); + const platforms = await getLRTETA(stationId); + let str = '預計到站時間如下⌚'; + str += '\n------------------------'; + + str += extractETA(platforms); + ctx.reply(str); + + let params = { + ec: 'lrt', + ea: stationId, + }; + analytics.send(ctx, params); +}); + + // Parse the input station name async function readStation(ctx) { let station; @@ -34,11 +64,33 @@ async function readStation(ctx) { if (stationId) { const platforms = await getLRTETA(stationId); + ctx.reply(extractETA(platforms)); } else { ctx.reply('無此站名❌'); } - analytics.send(ctx, { ec: 'bus', ea: 'start' }); + analytics.send(ctx, { ec: 'lrt', ea: 'start' }); +} + +function extractETA(platforms) { + let str = ''; + for (const { id, etas } of platforms) { + str += `\n月台 - ${id}\n`; + str += formatETAMessage(etas); + } + return str; +} + +function formatETAMessage(etas) { + let str = ''; + if (!etas) { + str += '沒有到站時間預報❌\n'; + } + for (const { route_no, dest_ch, train_length, time_ch } of etas) { + str += `${route_no} - ${dest_ch} - ${train_length}卡 - ${time_ch === '-' ? '即將離開' : time_ch}\n`; + } + str += '------------------------'; + return str; } module.exports = scene; diff --git a/lib/scenes/mtr.js b/lib/scenes/mtr.js index 5962121..26ae7c2 100644 --- a/lib/scenes/mtr.js +++ b/lib/scenes/mtr.js @@ -9,6 +9,7 @@ const { getStationName, getMTRETA } = require('../helper'); const scene = new Telegraf.BaseScene(constants.MTR_SCENE_ID); scene.command('bus', ctx => ctx.scene.enter(constants.BUS_SCENE_ID)); +scene.command('lrt', ctx => ctx.scene.enter(constants.LRT_SCENE_ID)); scene.on('message', ctx => ctx.scene.enter(constants.BUS_SCENE_ID)); // Ask for a metro line