telegram.js (3273B)
1 var telegram_token = process.env.telegram_api 2 if (typeof telegram_token === 'undefined' && !telegram_token) { 3 console.log("No Telegram token defined") 4 } 5 var botan_token = process.env.botan_api 6 if (typeof botan_token === 'undefined' && !botan_token) { 7 console.log("No Botan token defined") 8 } 9 var TelegramBot = require('node-telegram-bot-api'); 10 var jsonfile = require('jsonfile') 11 var getNodes = require('./parseNodes.js'); 12 var botan = require('botanio')(botan_token); 13 var util = require('util'); 14 var async = require("async"); 15 var _ = require("lodash"); 16 // Setup polling way 17 var bot = new TelegramBot(telegram_token, {polling: true}); 18 module.exports = { 19 start: function () { 20 async.auto({ 21 nodes: function (callback) { 22 bot.onText(/\/nodes (.+)/, function (msg, match) { 23 var fromId = msg.chat.id; 24 var resp = match[1]; 25 bot.sendChatAction(fromId, "typing") 26 jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) { 27 if (err) {throw new Error(err);} 28 var communities = obj 29 _.forEach(communities, function (key, ccode) { 30 if (ccode.toLowerCase() === resp.toLowerCase()) { 31 getNodes.countNodes(resp.toLowerCase(), "telegram", fromId, msg, "", bot, botan) 32 } 33 }); 34 }); 35 }); 36 callback() 37 }, 38 node: function (callback) { 39 bot.onText(/\/node (.+)/, function (msg, match) { 40 var fromId = msg.chat.id; 41 var resp = match[1]; 42 var args = _.split(resp, ' '); 43 bot.sendChatAction(fromId, "typing") 44 jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) { 45 if (err) {throw new Error(err);} 46 var communities = obj 47 _.forEach(communities, function (key, ccode) { 48 if (ccode.toLowerCase() === args[0].toLowerCase()) { 49 getNodes.NodeInfo(args[0].toLowerCase(), args[1].toLowerCase(), "telegram", fromId, msg, "", bot, botan) 50 } 51 }); 52 }); 53 }); 54 callback() 55 }, 56 communities: function (callback) { 57 bot.onText(/\/communities/, function (msg, match) { 58 var fromId = msg.chat.id; 59 var resp = match[1]; 60 bot.sendChatAction(fromId, "typing") 61 jsonfile.readFile('handlers/tmp/communities.json', 'utf8', function (err,obj) { 62 var communities = obj 63 var communities_list = "" 64 _.forEach(communities, function (key, ccode) { 65 var name = key["name"] 66 communities_list = communities_list + name + ": " + ccode + "\n" 67 }); 68 botan.track(msg, 'communities', function (err, res, body) { 69 if (err) { 70 throw new Error("[BOTAN] ERR: " + err); 71 } 72 console.log("[BOTAN] RES: " + res.statusCode); 73 console.log("[BOTAN] BODY: " + util.inspect(body, {showHidden: false, depth: null})); 74 }); 75 bot.sendMessage(fromId, communities_list); 76 }); 77 }); 78 callback() 79 } 80 }, 81 function(err, result) { 82 if (err) { 83 throw new Error(err) 84 } 85 } 86 ); 87 } 88 }