-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
61 lines (50 loc) · 1.64 KB
/
bot.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
console.log('BumbleBot is starting...');
// Ensure NPM packages are installed
var Twit = require('twit');
var config = require('./config');
var exec = require('child_process').exec;
var fs = require('fs');
var T = new Twit(config);
loop();
function loop() {
var now = new Date();
if ((now.getHours() % 2 === 0) && now.getMinutes() === 0) {
tweetIt();
}
now = new Date(); // allow for time passing
var delay = 60000 - (now % 60000); // exact ms to next minute interval
setTimeout(loop, delay);
}
function tweetIt() {
// Path to Processing sketch
var cmd = 'bumblebot/bumblebot';
exec(cmd, processing);
function processing() {
var imgFilename = 'bumblebot/output.png'; // Path to image file output from Processing
var imgParams = {
encoding: 'base64'
}
var imgBee = fs.readFileSync(imgFilename, imgParams); // Read image
// var txtFilename = 'bumblebot/output.txt'; // Path to text file output from Processing
// var txtParams = {
// encoding: 'utf8'
// }
// var txtBee = fs.readFileSync(txtFilename, txtParams); // Read text
T.post('media/upload', { media_data: imgBee }, uploaded); // Upload image before tweeting
function uploaded(err, data, response) {
var id = data.media_id_string; // Read uploaded image
var tweet = {
status: '', // Tweet text content
media_ids: [id] // Tweet image content
}
T.post('statuses/update', tweet, tweeted); // Send tweet
}
function tweeted(err, data, response) {
if (err) {
console.log('Error! Tweet not sent.');
} else {
console.log('Success! Tweet sent.');
}
}
}
}