From eaff8204b838a61198ff9f00d6c3309d8ef79077 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 30 Nov 2017 21:15:32 +0200 Subject: [PATCH] Add very crude ping command (ref #14) --- src/commands.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/commands.js b/src/commands.js index c314fc1c..8b4e3222 100644 --- a/src/commands.js +++ b/src/commands.js @@ -299,6 +299,22 @@ commands.api = async (sender, args, reply, app) => { } } +function timeout(promise, ms = 2500) { + return new Promise((resolve, reject) => { + promise.then(resolve, reject) + setTimeout(() => reject(new Error("API call response not received")), ms) + }) +} + +commands.ping = async (sender, args, reply) => { + try { + await timeout(sender.telegramPuppet.client("contacts.getContacts", {})) + reply("Connection seems OK.") + } catch (err) { + reply(`Not connected: ${err}`) + } +} + module.exports = { commands, run,