-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (82 loc) · 2.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var request = require("request"),
_ = require("underscore"),
twitter = require("twitter"),
util = require("util");
var Sparky = require('sparky');
var sparky = new Sparky({
deviceId: '',
token: '',
});
// Shhhh this is secret
var twit = new twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
var queue = [], lastID = process.argv[2] || 409408836098097150;
// Get tweets from our proxy server
getData = function() {
request({url: 'http://166.78.153.236/rc2013'}, handleResponse);
}
// Parse tweets
var handleResponse = function(err, res, body) {
if (!err) {
var jsonBody = JSON.parse(body);
body = jsonBody.searchTweets.result;
// reorder these so the earliest is at the top
body = _.sortBy(body, function(result){ return result.id; });
_.each(body, function(result, key) {
var i, l;
// If this tweet is new
if (result.id > lastID) {
lastID = result.id;
// filter out some strings we expect and should ignore
var text = String(result.text).toUpperCase().replace('FLOATBOT', '').replace('ROBOTSCONF', '');
var command = '', pauseLength = 0;
for (var i=0, j=text.length; i<j; i++) {
if ('FBLRPS'.indexOf(text[i]) !== -1) {
command += text[i];
}
}
command = command.substring(0,63);
for (var i=0, j=command.length; i<j; i++) {
text[i] = 'S' ? pauseLength += 4 : pauselenth += 1;
}
console.log('Command received: ' + command + ' from ' + result.user.screen_name + 'id: ' + result.id);
queue.unshift( {user: result.user.screen_name, cmd: command, id: result.id, sent: false, complete: false, pause: pauseLength });
}}
);
}
}
// Handle response from spark.core
var commandResponse = function(err, res, body) {
if (err) {
console.log(err);
} else {
queue.pop();
//console.log(body);
}
}
// Let's send the command string to floatbot
var sendCommand = function() {
var thisCommand = queue[queue.length - 1];
if (thisCommand && thisCommand.complete === true) {
queue.pop();
return;
}
if (thisCommand && thisCommand.sent === false) {
console.log('send command:' + thisCommand.cmd + ' from user: ' + thisCommand.user );
thisCommand.sent = true;
sparky.run('drive', thisCommand.cmd, commandResponse);
setTimeout(commandResponse, thisCommand.pause * 1000);
// Send a reply tweet
twit.updateStatus('@' + thisCommand.user + ' Everything\'s shiny, Cap\'n. Not to fret. Running: ' + thisCommand.cmd,
function(data) {
//console.log(util.inspect(data));
}
);
}
}
var getTweetsInterval = setInterval(getData, 5000);
var sendCommandsInterval = setInterval(sendCommand, 1000);