Skip to content

Instantly share code, notes, and snippets.

@ArielLeslie
Created December 3, 2015 15:27
Show Gist options
  • Save ArielLeslie/7a4e6e648fe5b3d2a3ef to your computer and use it in GitHub Desktop.
Save ArielLeslie/7a4e6e648fe5b3d2a3ef to your computer and use it in GitHub Desktop.
/** R T & F A V T W I T T E R B O T **/
/** ======================================= **/
/** Written by Amit Agarwal @labnol on 31/07/2015 **/
/** Tutorial link: http://www.labnol.org/?p=28967 **/
/** Live demo at http://twitter.com/DearAssistant **/
// Fill the values above and then choose Run -> Start
TWITTER_CONSUMER_KEY = "2PYfqeZn6OeC745fCHGOr4ubl";
TWITTER_CONSUMER_SECRET = "jX07VkbpS1vFp51s1GxEeXw6hCEs8cHW2we3gRAzMsw4fgtbMm";
TWITTER_ACCESS_TOKEN = "3904229192-NzpP6LdGcbOxJeeq5auP22ZqgRifZzjizaqJoCB";
TWITTER_ACCESS_SECRET = "uvoGTZi2wKNYajbaE4WcV8GYD2ib08ne9y6O5X8hWhNj3";
TWITTER_SEARCH_PHRASE = "else -RT -filter:links ";
// Need help in customizing the bot? Email amit@labnol.org
// Premium support at www.ctrlq.org
// DO NOT CHANGE ANYTHING BELOW THIS LINE
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"
});
if (typeof Twitter === "undefined") {
throw new Error("Please add the Twitter library. See www.labnol.org/?p=28967 for help");
}
var twit = new Twitter.OAuth(props);
// Test the favorite/retweet/fetch Twitter operations
if (!twit.retweet("628173817559453696")) {
if (!twit.favorite("628173817559453696")) {
try {
twit.fetchTweets("labnol RT to win", null, {count:1});
Logger.log("Twitter authorization is successful.");
} catch (f) {
throw new Error("Please check your Twitter access tokens");
return;
}
}
}
// Delete exiting triggers, if any
Stop_Bot();
// Setup trigger to read Tweets every five minutes
ScriptApp.newTrigger("labnol_twitterBot")
.timeBased()
.everyMinutes(30)
.create();
}
function Stop_Bot() {
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function labnol_twitterBot() {
try {
var props = PropertiesService.getScriptProperties(),
twit = new Twitter.OAuth(props);
if (twit.hasAccess()) {
var tweets = twit.fetchTweets(
TWITTER_SEARCH_PHRASE, function(tweet) {
if (!tweet.possibly_sensitive) {
var txt = tweet.text;
if(txt.indexOf("@") <0 && txt.indexOf("http") <0){
return tweet.id_str;
}}
}, {
multi: true,
lang: "en",
count: 5,
since_id: props.getProperty("SINCE_TWITTER_ID")
});
if (tweets.length) {
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());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment