This repository has been archived by the owner on Feb 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
98 lines (87 loc) · 3.43 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
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
var fs = require('fs'),
Twit = require('twit'),
async = require('async');
var T = null,
livestreams = null;
start();
function start() {
console.log('Initializing bot...');
initTwit();
}
function end(error) {
if (error) console.error(error);
process.exit(1);
}
function initTwit() {
fs.readFile('config.json', 'utf-8', function (err, fileContents) {
if (err) {
if (process.env.CONSUMER_KEY && process.env.CONSUMER_SECRET && process.env.ACCESS_TOKEN && process.env.ACCESS_TOKEN_SECRET) {
T = new Twit({
"consumer_key": process.env.CONSUMER_KEY,
"consumer_secret" : process.env.CONSUMER_SECRET,
"access_token": process.env.ACCESS_TOKEN,
"access_token_secret": process.env.ACCESS_TOKEN_SECRET
});
} else {
end('Config cound not be loaded, startup cannot continue. Exiting...');
}
} else {
T = new Twit(JSON.parse(fileContents));
}
listen(T);
});
fs.readFile('onlinestreams.json', 'utf-8', function (err, fileContents) {
if (err) {
end('Cannot find online streams data, startup cannot continue. Exiting...');
}
livestreams = JSON.parse(fileContents);
});
}
function listen(T) {
var stream = T.stream('user', {});
console.log('Bot initialized successfully. Now streaming.');
stream.on('tweet', function (tweet) {
checkHashtag(tweet);
checkZone(tweet);
});
}
function checkHashtag(tweet) {
var matches = tweet.text.toLowerCase().match(/#crimeisdown/);
if (matches && tweet.text.toLowerCase().indexOf("spotnewsonig")==-1 && tweet.user.screen_name.toLowerCase()!=="spotnewsonig") {
T.post('statuses/retweet/:id', { id: tweet.id_str }, function (err, data, response) {
if (err) console.error(err);
// console.log(data);
});
}
}
function checkZone(tweet) {
var matches = tweet.text.toLowerCase().match(/(((citywide |cw)[1,6])|((zone |z)(1[0-3]|[1-9])))|(^((main)|(englewood))$)/);
if (matches) {
async.each(tweet.text.toLowerCase().split(' '), function (value, callback) {
if (value.indexOf(matches[0])>0 && (value.indexOf('://')>0 || value.indexOf('@')===0)) callback('bad match');
else callback();
}, function (err) {
if (!err) retweet(tweet, matches);
});
}
}
function retweet(tweet, matches) {
var rt = '\nhttps://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id_str;
if (tweet.text.length < 31 && tweet.text.toLowerCase().indexOf("spotnewsonig")==-1) {
var channel = livestreams[matches[0].toUpperCase()
.replace('ZONE ', 'Z')
.replace('CITYWIDE ', 'CW')
.replace('MAIN', 'CFD-Fire')
.replace('ENGLEWOOD', 'CFD-Fire')];
if (channel) {
var statusupdate = 'LISTEN LIVE to ' + channel.shortname + ' at ' + channel.feedUrl + '/web' + rt;
T.post('statuses/update', {
status: statusupdate,
in_reply_to_status_id: tweet.id_str
}, function (err, data, response) {
if (err) console.error(err);
console.log(statusupdate);
});
}
}
}