-
Notifications
You must be signed in to change notification settings - Fork 2
/
hugbot.js
67 lines (53 loc) · 1.67 KB
/
hugbot.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
var twitter = require('twode');
function Bot(handle) {
this.handle = handle.charAt(0) === '@' ? handle : '@' + handle;
this.twitter = new twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
});
}
Bot.prototype.handleError = function(error) {
console.error('Status:', error.statusCode);
console.error('Data:', error.data);
};
Bot.prototype.pugs = [
'http://bit.ly/1boVVNH',
'http://bit.ly/1aVWjl3',
'http://bit.ly/Ik6yoi',
'http://bit.ly/1aZrkng',
'http://bit.ly/18oFYqv',
'http://bit.ly/doqglS',
'http://bit.ly/195gk56'
];
Bot.prototype.isPug = function() {
function getRandNum(min, max) {
return Math.floor((Math.random() * max) + min);
}
return getRandNum(1, 10) > 9 ? true : false;
};
Bot.prototype.getResponse = function(userHandle) {
var response = '@' + userHandle;
if (this.isPug()) {
var pugPic = this.pugs[Math.floor(this.pugs.length * Math.random())];
response += ' hugs over the internet can be tricky, but luckily, pugs are plentiful ' + pugPic;
} else {
response += ' HUGBOT SENDS HUGS'
}
return response;
};
Bot.prototype.run = function() {
var bot = this;
this.twitter.stream('statuses/filter', { track: this.handle }, function(stream) {
console.log('Listening for Tweets...');
stream.on('data', function(tweet) {
var status = bot.getResponse(tweet.user.screen_name);
bot.twitter.updateStatus(status, function(err) {
if (err) return bot.handleError(err);
});
});
});
};
var hugbot = new Bot('hugsorpugs');
hugbot.run();