-
Notifications
You must be signed in to change notification settings - Fork 9
/
code.gs
59 lines (51 loc) · 1.71 KB
/
code.gs
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
// Fill out the Twitter Keys and then choose Run -> Start Bot
TWITTER_CONSUMER_KEY = "";
TWITTER_CONSUMER_SECRET = "";
TWITTER_ACCESS_TOKEN = "";
TWITTER_ACCESS_SECRET = "";
TWITTER_SEARCH_PHRASE = ""; // ex. "#DevFest -RT" (the "-RT" excludes re-Tweets)
function Start_Bot() {
var props = PropertiesService.getScriptProperties();
props.setProperties({
TWITTER_CONSUMER_KEY: TWITTER_CONSUMER_KEY,
TWITTER_CONSUMER_SECRET: TWITTER_CONSUMER_SECRET,
TWITTER_ACCESS_TOKEN: TWITTER_ACCESS_TOKEN,
TWITTER_ACCESS_SECRET: TWITTER_ACCESS_SECRET,
SINCE_TWITTER_ID: 0
});
ScriptApp.newTrigger("hashtag_twitterBot")
.timeBased()
.everyMinutes(10)
.create();
}
function hashtag_twitterBot() {
try {
var props = PropertiesService.getScriptProperties(),
twit = new Twitter.OAuth(props);
if (twit.hasAccess()) {
var tweets = twit.fetchTweets(
TWITTER_SEARCH_PHRASE, function(tweet) {
// Skip tweets that contain sensitive content
if (!tweet.possibly_sensitive) {
return tweet.id_str;
}
}, {
multi: true,
lang: "en", // Process only English tweets
count: 5, // Process 5 tweets in a batch
since_id: props.getProperty("SINCE_TWITTER_ID")
});
if (tweets.length > 0) {
props.setProperty("SINCE_TWITTER_ID", tweets[0]);
for (var i = tweets.length - 1; i >= 0; i--) {
twit.retweet(tweets[i]);
twit.favorite(tweets[i]);
/* Wait between 10 seconds and 1 minute */
Utilities.sleep(Math.floor(Math.random()*50000) + 10000);
}
}
}
} catch (f) {
Logger.log("Error: " + f.toString());
}
}