-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
78 lines (55 loc) · 1.9 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
//Using Twit library
var Twit = require('twit')
//Getting the config info from config.js
//Check config.sample.js for the sample code
var Config = require('./config.js');
//Creating a new Twit object
var T = new Twit(Config);
//Creating a user stream
var stream = T.stream('user');
//Listenign to Tweets
stream.on('tweet', tweetEvent);
//When a tweet event happens
function tweetEvent(data) {
//Getting the @ mention
var replyto = data.in_reply_to_screen_name;
//Getting the owner of the tweet
var from = data.user.screen_name;
//Getting the hashtags in tweet
var hashtags = data.entities.hashtags;
//Checking if the tweet is targetted at the bot
if (replyto == 'referee_bot') {
//Looping through the hashtags
hashtags.forEach(function(element) {
//Only going to reply back if the hashtags are either the mentioned one below
//Unnecessary condion, I know. Just to add a small randomization
if (element.text == 'flipacoin') {
//Running the toss function
var result = toss();
//Tweeting back with a spicifc hashtag
tweetback('flippedacoin', result, from);
} else if (element.text == 'tossacoin') {
var result = toss();
tweetback('tossedacoin', result, from)
}
});
}
}
//Toss function
function toss(){
//Random number, 1 or 2
var coin = Math.floor(Math.random() * 2) + 1;
if(coin == 1) {
return 'HEADS';
} else {
return 'TAILS';
}
}
function tweetback(tag, result, from) {
var text = 'Hi .@'+ from + '! You have got ' + result +'. #'+tag+ ' #ID'+ Math.floor(Math.random() * 99999) + 1
//Tweeting back
T.post('statuses/update', { status: text},
function(err, data, response) {
//console.log(data)
});
}