freifunk-bot

Modular parser for the hackerspace door status with handlers for multiple services
git clone git://archive.git.mtrnord.blog/MTRNord/freifunk-bot.git
Log | Files | Refs | README | LICENSE

commit d936c382b58feee015ad3d4b29aa0c9dfc5a2ae8
parent 37fee2389fb4313615521e242cea90c8643aded3
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sun, 11 Sep 2016 00:02:32 +0200

speedup telegram

Diffstat:
Mhandlers/telegram.js | 83+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 49 insertions(+), 34 deletions(-)

diff --git a/handlers/telegram.js b/handlers/telegram.js @@ -13,46 +13,61 @@ var botan = require('botanio')(botan_token); var ent = require('ent'); var decode = require('ent/decode'); var util = require('util'); +var async = require("async"); var _ = require("underscore"); // Setup polling way var bot = new TelegramBot(telegram_token, {polling: true}); module.exports = { start: function () { - bot.onText(/\/nodes (.+)/, function (msg, match) { - var fromId = msg.chat.id; - var resp = match[1]; - bot.sendChatAction(fromId, "typing") - jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) { - var communities = obj - _.find(communities.communities, function (key) { - var ccode = key["ccode"]; - if (ccode.toLowerCase() === resp.toLowerCase()) { - getNodes.countNodes(resp.toLowerCase(), "telegram", fromId, msg, "", bot, botan) - } + async.auto({ + nodes: function (callback) { + bot.onText(/\/nodes (.+)/, function (msg, match) { + var fromId = msg.chat.id; + var resp = match[1]; + bot.sendChatAction(fromId, "typing") + jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) { + var communities = obj + _.find(communities.communities, function (key) { + var ccode = key["ccode"]; + if (ccode.toLowerCase() === resp.toLowerCase()) { + getNodes.countNodes(resp.toLowerCase(), "telegram", fromId, msg, "", bot, botan) + } + }); + }); }); - }); - }); - bot.onText(/\/communities/, function (msg, match) { - var fromId = msg.chat.id; - var resp = match[1]; - bot.sendChatAction(fromId, "typing") - jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) { - var communities = obj - var communities_list = "" - _.find(communities.communities, function (key) { - var ccode = decode(key["ccode"]) - var name = decode(key["name"]) - communities_list = communities_list + name + ": " + ccode + "\n" + callback() + }, + communities: function (callback) { + bot.onText(/\/communities/, function (msg, match) { + var fromId = msg.chat.id; + var resp = match[1]; + bot.sendChatAction(fromId, "typing") + jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) { + var communities = obj + var communities_list = "" + _.find(communities.communities, function (key) { + var ccode = decode(key["ccode"]) + var name = decode(key["name"]) + communities_list = communities_list + name + ": " + ccode + "\n" + }); + botan.track(msg, 'communities', function (err, res, body) { + if (err) { + console.log("[BOTAN] ERR: " + err); + } + console.log("[BOTAN] RES: " + res.statusCode); + console.log("[BOTAN] BODY: " + util.inspect(body, {showHidden: false, depth: null})); + }); + bot.sendMessage(fromId, communities_list); + }); }); - botan.track(msg, 'communities', function (err, res, body) { - if (err) { - console.log("[BOTAN] ERR: " + err); - } - console.log("[BOTAN] RES: " + res.statusCode); - console.log("[BOTAN] BODY: " + util.inspect(body, {showHidden: false, depth: null})); - }); - bot.sendMessage(fromId, communities_list); - }); - }); + callback() + } + }, + function(err, result) { + if (err) { + console.log(err) + } + } + ); } }