Skip to content

Instantly share code, notes, and snippets.

@CapacitorSet
Created January 17, 2016 20:56
Show Gist options
  • Save CapacitorSet/6e5510c65b9c482b18e9 to your computer and use it in GitHub Desktop.
Save CapacitorSet/6e5510c65b9c482b18e9 to your computer and use it in GitHub Desktop.
"use strict";
var async = require("async"),
net = require("net"),
TelegramBot = require('node-telegram-bot-api');
var q = async.queue((item, cb) => {
console.log(item);
mandaComando(item, cb);
});
var token = '172674961:AAEZgpIZpxRPux2dDn-VzJhWg1COD5Z2Zys';
// Setup polling way
var bot = new TelegramBot(token, {polling: true});
bot.on('message', function (msg) {
console.log("");
console.log(msg);
var fromId = msg.from.id,
text = msg.text;
bot.sendMessage(fromId, "Processo " + text + "...");
var ping = text.match(/\/?ping/);
if (ping) {
console.log("Pinged.");
bot.sendMessage(fromId, "Pong!");
return;
}
var inizia = text.match("\/inizia");
if (inizia) {
bot.sendMessage(fromId, "Inizio un ciclo.");
q.push(97);
return;
}
var stop = text.match("\/stop");
if (stop) {
bot.sendMessage(fromId, "Faccio uno stop di emergenza.");
q.push(98);
return;
}
var incremento = text.match(/\/(in|de)crementa (andata|ritorno|pausa)(?: (\d+))?/);
if (incremento) {
var id = {"andata": 0, "ritorno": 1, "pausa": 2}[incremento[2]];
console.log(id);
bot.sendMessage(fromId,
(incremento[1] == "in" ? "Incremento" : "Decremento") + " il tempo di " + incremento[2] + ".");
/* 99: andata--
100: andata++
101: ritorno--
102: ritorno++
103: pausa--
104: pausa++
*/
var num = 1*incremento[3] || 1;
console.log("num:", num);
while(num > 0) {
console.log("num =", num);
q.push(99 + (incremento[1] == "in") + 2*id);
num--;
}
return;
}
bot.sendMessage(fromId, "Comando non riconosciuto.");
});
var server_IP = "127.0.0.1";
function mandaComando(contents, cb) {
contents = String(contents);
var client = net.createConnection({port: 8080}, () => {
client.write(contents);
client.end();
if (cb) cb();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment