-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwitter.gs
More file actions
125 lines (107 loc) · 4.14 KB
/
twitter.gs
File metadata and controls
125 lines (107 loc) · 4.14 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function Start() {
var props = PropertiesService.getScriptProperties();
props.setProperties({
TWITTER_CONSUMER_KEY: "",
TWITTER_CONSUMER_SECRET: "",
TWITTER_ACCESS_TOKEN: "",
TWITTER_ACCESS_SECRET: "",
MAX_TWITTER_ID: '',
TWITTER_TARGET_HASHTAG: '#السعودية',
});
// Delete exiting triggers, if any
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
// Setup a time-based trigger for the Bot to fetch and process incoming Tweets
// every minute. If your Google Script is running out of quota, change the
// time to 5 or 10 minutes though the bot won't offer real-time answers then.
ScriptApp.newTrigger("a5tabot_twitterBot")
.timeBased()
.everyMinutes(1)
.create();
ScriptApp.newTrigger("favorite_hashtag")
.timeBased()
.everyMinutes(1)
.create();
}
function clearProps(){
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteAllProperties();
}
function favorite_hashtag(){
Logger.log("entering hashtag");
var props = PropertiesService.getScriptProperties(),
twit = new Twitter.OAuth(props);
var phrase = ScriptProperties.getProperty('TWITTER_TARGET_HASHTAG');
try{
Logger.log("inside try");
var twit = new Twitter.OAuth(props);
if (twit.hasAccess()) {
Logger.log("has access");
var tweets = twit.fetchTweets(phrase, function(tweet) {
// Ignore tweets that are sensitive (NSFW content)
if (!tweet.possibly_sensitive) {
Logger.log("not sensitive");
return {id_str: tweet.id_str};
}
}, { multi: true, count: 6, });
Logger.log("before length");
if (tweets.length) {
Logger.log("inside length");
// Process the tweets in FIFO order
for (var i = tweets.length - 1; i >= 0; i--) {
Logger.log(twit.favorite(tweets[i]));
Logger.log("inside for");
// Wait a second to avoid hitting the rate limits
Utilities.sleep(1000);
}
}
}
}
catch (f) {
Logger.log("Error: " + f.toString());
}
}
function reverse(s) {
return s.split("").reverse().join("");
}
function a5tabot_twitterBot() {
try {
var props = PropertiesService.getScriptProperties(),
twit = new Twitter.OAuth(props);
// Are the Twitter access tokens are valid?
if (twit.hasAccess()) {
var tweets = twit.fetchTweets("to:" + "a5tabot", function(tweet) {
// Ignore tweets that are sensitive (NSFW content)
if (!tweet.possibly_sensitive) {
var question = tweet.text.toLowerCase().replace("@a5tabot","").trim();
var answer = reverse(question);
if (answer) {
return {
answer: "@" + tweet.user.screen_name + " " + answer,
id_str: tweet.id_str
};
}
}
}, { multi: true, count: 5, since_id: props.getProperty("MAX_TWITTER_ID")});
if (tweets.length) {
// The MAX_TWITTER_ID property store the ID of the last tweet answered by the bot
props.setProperty("MAX_TWITTER_ID", tweets[0].id_str);
// Process the tweets in FIFO order
for (var i = tweets.length - 1; i >= 0; i--) {
// The bot replies with an answer
twit.sendTweet(tweets[i].answer, {
in_reply_to_status_id: tweets[i].id_str
});
// Wait a second to avoid hitting the rate limits
Utilities.sleep(1000);
}
}
}
}
catch (f) {
// You can also use MailApp to get email notifications of errors.
Logger.log("Error: " + f.toString());
}
}