Skip to content

Commit

Permalink
Complete LRT arrival time check
Browse files Browse the repository at this point in the history
  • Loading branch information
kirosc committed Sep 3, 2020
1 parent 8be30bd commit bdedf89
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/company/lrt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 28 additions & 1 deletion lib/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]);
Expand Down Expand Up @@ -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;
};
1 change: 1 addition & 0 deletions lib/scenes/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
58 changes: 55 additions & 3 deletions lib/scenes/lrt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
1 change: 1 addition & 0 deletions lib/scenes/mtr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bdedf89

Please sign in to comment.