Skip to content

Instantly share code, notes, and snippets.

@mickstevens
Last active August 29, 2015 14:15
Show Gist options
  • Save mickstevens/6947433bb2e52a4e67a6 to your computer and use it in GitHub Desktop.
Save mickstevens/6947433bb2e52a4e67a6 to your computer and use it in GitHub Desktop.
/* O U T O F O F F I C E F O R T W I T T E R */
/* - - - - - - - - - - - - - - - - - - - - - */
/* Written by Amit Agarwal http://labnol.org?p=27911 */
/* For customization and support, see http://ctrlq.org */
function start() {
// Please enter dates in YYYY-MM-DD HH:MM format
var OUTOFOFFICE_START_DATE = "2015/02/15 19:00";
var OUTOFOFFICE_END_DATE = "2015/02/18 08:00";
// This is your Out-Of-Office reply. Keep it less than 120 characters.
var OUTOFOFFICE_TEXT = "I am currently out of the office until Wed, I'll get back to you then!";
// Get your Twitter keys from apps.twitter.com
var CONSUMER_KEY = "YOUR_KEY_HERE";
var CONSUMER_SECRET = "YOUR_SECRET_HERE";
var TWITTER_HANDLE = "YOUR_HANDLE_HERE";
// Ignore everything after this line
storeKeys( CONSUMER_KEY, CONSUMER_SECRET, OUTOFOFFICE_TEXT, TWITTER_HANDLE );
initialize( OUTOFOFFICE_START_DATE, OUTOFOFFICE_END_DATE );
// Make sure that Twitter oAuth is working
doTwitter();
}
function removeTriggers() {
var triggers = ScriptApp.getScriptTriggers();
for(var i=0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
function storeKeys(key, secret, text, handle) {
var props = PropertiesService.getScriptProperties();
props.setProperty("TWITTER_CONSUMER_KEY", key);
props.setProperty("TWITTER_CONSUMER_SECRET", secret);
props.setProperty("OUTOFOFFICE_TEXT", text);
props.setProperty("MAX_TWITTER_ID", 0);
props.setProperty("TWITTER_HANDLE", handle);
}
// Setup triggers for the START and END dates
function initialize(start, end) {
var startDate = new Date(start);
var endDate = new Date(end);
removeTriggers();
ScriptApp.newTrigger("autoReply")
.timeBased()
.at(startDate)
.create();
ScriptApp.newTrigger("removeTriggers")
.timeBased()
.at(endDate)
.create();
}
function autoReply() {
ScriptApp.newTrigger("outOfOffice")
.timeBased()
.everyMinutes(5)
.create();
}
function oAuth() {
var props = PropertiesService.getScriptProperties();
var oauthConfig = UrlFetchApp.addOAuthService("labnol");
oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(props.getProperty("TWITTER_CONSUMER_KEY"));
oauthConfig.setConsumerSecret(props.getProperty("TWITTER_CONSUMER_SECRET"));
}
// This function will poll twitter every 5 minutes for any @mentions
function outOfOffice() {
oAuth();
var props = PropertiesService.getScriptProperties();
var twitter_handle = props.getProperty("TWITTER_HANDLE");
var phrase = "to:" + twitter_handle;
var search = "https://api.twitter.com/1.1/search/tweets.json?count=10"
+ "&include_entities=false&result_type=recent&q="
+ encodeString(phrase) + "&since_id="
+ props.getProperty("MAX_TWITTER_ID");
var options =
{
"method": "get",
"oAuthServiceName":"labnol",
"oAuthUseToken":"always"
};
var key = "Twitter" + Utilities.formatDate(new Date(), "UTC", 'yw');
try {
var result = UrlFetchApp.fetch(search, options);
if (result.getResponseCode() === 200) {
var data = Utilities.jsonParse(result.getContentText());
if (data) {
var tweets = data.statuses;
if (tweets.length) {
var db = props.getProperty(key) || "";
var reply = props.getProperty("OUTOFOFFICE_TEXT");
for (var i=tweets.length-1; i>=0; i--) {
var sender = "#" + tweets[i].user.screen_name + "#";
if (db.indexOf(sender) == -1) {
props.setProperty(key, db + sender)
sendTweet(tweets[i].user.screen_name, tweets[i].id_str, reply, props);
}
}
}
}
}
} catch (e) {
Logger.log(e.toString());
}
}
// If an @mention is found, send an Out of Office tweet to that user.
function sendTweet(user, reply_id, tweet, props) {
var options =
{
"method": "POST",
"oAuthServiceName":"labnol",
"oAuthUseToken":"always"
};
var text = encodeString("@" + user + " " + tweet).substr(0, 140);
var status = "https://api.twitter.com/1.1/statuses/update.json"
+ "?status=" + text + "&in_reply_to_status_id=" + reply_id;
try {
var result = UrlFetchApp.fetch(status, options);
props.setProperty("MAX_TWITTER_ID", reply_id);
}
catch (e) {
Logger.log(e.toString());
}
}
function doTwitter() {
try {
var req = "https://api.twitter.com/1.1/application/rate_limit_status.json?resources=search";
var options =
{
"method": "get",
"oAuthServiceName":"labnol",
"oAuthUseToken":"always"
};
oAuth();
var result = UrlFetchApp.fetch(req, options);
} catch (e) {
throw(e.toString());
}
}
function encodeString (q) {
q = q.replace(/\!|\*|\(|\)|\'/g, '');
return encodeURIComponent(q);
//str = str.replace(/!/g,'%21');
//str = str.replace(/\*/g,'%2A');
//str = str.replace(/\(/g,'%28');
//str = str.replace(/\)/g,'%29');
//str = str.replace(/'/g,'%27');
//return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment