From e7d52c816808cb525f1cd660d942219c113d57a3 Mon Sep 17 00:00:00 2001 From: Francesco Esposito <33671357+frab1t@users.noreply.github.com> Date: Fri, 22 Feb 2019 10:05:24 +0100 Subject: [PATCH] implements devices not found --- server.js | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index bbc040e..fc4911a 100644 --- a/server.js +++ b/server.js @@ -34,11 +34,21 @@ bot.on('/list', (msg) => { } api.getDevices() - .then(res => res.json()) + .then((res) => { + console.log(res.status); + if (res.status === 200) { + return res.json(); + } + return null; + }) .then((json) => { - json.forEach((elem) => { - bot.sendMessage(msg.from.id, utils.templateDevicesList(elem), { parseMode: 'Markdown' }); - }); + if (json) { + json.forEach((elem) => { + bot.sendMessage(msg.from.id, utils.templateDevicesList(elem), { parseMode: 'Markdown' }); + }); + } else { + bot.sendMessage(msg.from.id, utils.templateDeviceNotFound()); + } }) .catch((err) => { bot.sendMessage(msg.from.id, utils.templateError()); @@ -52,15 +62,24 @@ bot.on('/position', (msg) => { } api.getDevices() - .then(res => res.json()) + .then((res) => { + console.log(res.status); + if (res.status === 200) { + return res.json(); + } + return null; + }) .then((devicesJSON) => { - const devices = []; - devicesJSON.forEach((device) => { - devices.push(device.name); - }); + if (devicesJSON) { + const devices = []; + devicesJSON.forEach((device) => { + devices.push(device.name); + }); + const replyMarkup = bot.keyboard([devices], { resize: true, once: true }); + return bot.sendMessage(msg.from.id, 'Select device', { ask: 'devicePosition', replyMarkup }); + } - const replyMarkup = bot.keyboard([devices], { resize: true, once: true }); - bot.sendMessage(msg.from.id, 'Select device', { ask: 'devicePosition', replyMarkup }); + return bot.sendMessage(msg.from.id, utils.templateDeviceNotFound()); }) .catch((err) => { bot.sendMessage(msg.from.id, utils.templateError());