-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (62 loc) · 2.73 KB
/
app.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
console.log('Twitter bot is starting...');
// needed for timestamps
var d = new Date();
d.toLocaleDateString
// Twitter API login credentials
const config = require('./config');
// Twitter API client for node
const Twit = require('twit');
const T = new Twit(config);
// filter tweets
const stream = T.stream('statuses/filter', {track: "eat um up tigers, eat um' up tigers, eat um up #tigers, eat um' up #tigers, #eatumuptigers, headed comerica tigers, on my way comerica tigers, see tigers comerica, watch tigers comerica game, road trip comerica excited"});
// stream all public tweets based on filter
stream.on('tweet', function(tweet) {
// console.log(tweet);
// save the ID and screen name of the tweet author
const tweetUserID = tweet.id_str;
const tweetUserName = tweet.user.screen_name;
const vidPath = './eatumup.mp4';
// only reply to tweets - NOT retweets or replies
if (!isReply(tweet)) {
console.log('New tigers tweet detected');
// post video via the chunked media upload API.
T.postMediaChunked({file_path: vidPath}, function(err, data, response) {
// console.log(data);
if (!err) {
const mediaIdStr = data.media_id_string;
const metaParams = {media_id: mediaIdStr};
// use the mediaId we received from postMediaChunked
T.post('media/metadata/create', metaParams, function(err, data, response) {
if (!err) {
// now we can reference the media and post a tweet (media will attach to the tweet)
const params = {in_reply_to_status_id: tweetUserID, status: '@' + tweetUserName, media_ids: mediaIdStr};
T.post('statuses/update', params, function(err, data, response) {
if (!err) {
console.log('Reply successfully sent! - https://twitter.com/EatUmUpTigers/status/' + data.id_str);
} else {
console.log('ERROR calling statuses/update - ' + err);
}
});
} else {
console.log('ERROR calling media/metadata/create - ' + err);
}
});
} else {
console.log('ERROR calling postMediaChunked - ' + err);
}
});
}
});
/**
* Returns true if the tweet is a retweet or a reply
*/
function isReply(tweet) {
if (tweet.retweeted_status
|| tweet.in_reply_to_status_id
|| tweet.in_reply_to_status_id_str
|| tweet.in_reply_to_user_id
|| tweet.in_reply_to_user_id_str
|| tweet.in_reply_to_screen_name) {
return true;
}
}