forked from gdgnewdelhi/twitterbot-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
executable file
·103 lines (89 loc) · 2.93 KB
/
bot.js
File metadata and controls
executable file
·103 lines (89 loc) · 2.93 KB
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
// Debug flag
var debug = false
// Our Twitter library
var Twit = require('twit')
// We need to include our configuration file
var T = new Twit(require('./config.js'))
// This is the URL of a search for the latest tweets on the '#gdgnd' hashtag.
var hastagSearch = {
q: '#gdgcv',
count: 10,
result_type: 'recent'
}
// A user stream
var stream = T.stream('user')
// When someone follows the user
stream.on('follow', followed)
stream.on('tweet', tweetEvent)
// In this callback we can see the name and screen name
function followed (event) {
var name = event.source.name
var screenName = event.source.screen_name
var response = 'Thanks for following me, ' + name + ' @' + screenName + ' !'
// Post that tweet!
T.post('statuses/update', {
status: response
}, tweeted)
console.log('I was followed by: ' + name + ' @' + screenName)
}
// Here a tweet event is triggered!
function tweetEvent (tweet) {
// If we wanted to write a file out
// to look more closely at the data
// var fs = require('fs');
// var json = JSON.stringify(tweet,null,2);
// fs.writeFile("tweet.json", json, output);
// Who is this in reply to?
var replyTo = tweet.in_replyTo_screen_name
// Who sent the tweet?
var name = tweet.user.screen_name
// What is the text?
var txt = tweet.text
// Ok, if this was in reply to me
console.log(replyTo, name, txt)
if (replyTo === 'gdgcvbot') {
// Get rid of the @ mention
txt = txt.replace(/@gdgndbot/g, '')
// Start a reply back to the sender
var reply = 'Hi @' + name + ' ' + ', Thanks for the mention :)'
console.log(reply)
// Post that tweet!
T.post('statuses/update', {
status: reply
}, tweeted)
}
}
// This function finds the latest tweet with the #gdgnd hashtag, and retweets it.
function retweetLatest () {
T.get('search/tweets', hastagSearch, function (error, data) {
var tweets = data.statuses
for (var i = 0; i < tweets.length; i++) {
console.log(tweets[i].text)
}
// If our search request to the server had no errors...
if (!error && data.statuses.length > 0) {
// ...then we grab the ID of the tweet we want to retweet...
var retweetId = data.statuses[0].id_str
// ...and then we tell Twitter we want to retweet it!
T.post('statuses/retweet/' + retweetId, {}, tweeted)
} else {
// However, if our original search request had an error, we want to print it out here.
if (debug) {
console.log('There was an error with your hashtag search:', error)
}
}
})
}
// Make sure it worked!
function tweeted (err, reply) {
if (err !== undefined) {
console.log(err)
} else {
console.log('Tweeted: ' + reply)
}
}
// Try to retweet something as soon as we run the program...
retweetLatest()
// ...and then every hour after that. Time here is in milliseconds, so
// 1000 ms = 1 second, 1 sec * 60 = 1 min, 1 min * 60 = 1 hour --> 1000 * 60 * 60
setInterval(retweetLatest, 1000 * 60 * 2)