Last active
August 29, 2015 14:21
-
-
Save anak10thn/4d276240ec06a5c36135 to your computer and use it in GitHub Desktop.
telegram-parsing-ignsdk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var shark = require('shark.io'); | |
var fs = require('fs'); | |
var _ = require('lodash'); | |
var stripAnsi = require('strip-ansi'); | |
shark.init('127.0.0.1:6969'); | |
var setup = shark.setup; | |
var gpio; | |
setup.on('open',function(event){ | |
gpio = event.gpio; | |
gpio.set(18); | |
gpio.mode("out"); | |
event.sys.exec('startbot',function(spwn){ | |
spwn.out.connect(function(data,err){ | |
var lines = data.toString().split('\n'); | |
if (lines.length > 1) { | |
lines = _.map(lines, function(line) { | |
return stripAnsi(line); | |
}); | |
// Message output should always be in the first line of data | |
if (lines[0].indexOf('>>>') > -1) { | |
console.log(lines[0]); | |
var parseCLI = parseCLIOutput(lines[0]); | |
var cmd = parseCLI.message.toLowerCase().split(" "); | |
if(cmd[0] == "echo"){ | |
var msg = parseCLI.message.replace(cmd[0],"") | |
spwn.write("msg "+parseCLI.sendTo+" juriko ["+cmd[0]+"] : "+msg+"\n"); | |
} | |
else if(cmd[0] == "on"){ | |
gpio.write(18,1); | |
} | |
else if(cmd[0] == "off"){ | |
gpio.write(18,0); | |
} | |
} | |
} | |
}); | |
}); | |
}); | |
function parseCLIOutput(data) { | |
var splitData = data.split(' >>> '); | |
var message = splitData[splitData.length - 1]; | |
var metaData = splitData[0].split(' '); | |
var sendTo = metaData[2]; | |
var user = metaData[metaData.length - 1]; | |
return { | |
sendTo: sendTo, | |
user: user, | |
message: message | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PATH=$PATH:~/tg/bin | |
export PATH | |
telegram-cli -k ~/tg/tg-server.pub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gua jadi di shutdown ga yah, kampret emang