Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Use Twitter directly in your Google Doc, so no one will ever blame you for being social
function authorize_(method) {
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
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(UserProperties.getProperty("CONSUMER_KEY"));
oauthConfig.setConsumerSecret(UserProperties.getProperty("CONSUMER_SECRET"));
return {
oAuthServiceName: "twitter",
oAuthUseToken: "always",
method: (method) ? method : "POST",
};
}
function onInstall() {
// Set Twitter API keys
var cKey = Browser.inputBox("Twitter API Access", "Enter Consumer Key:", Browser.Buttons.OK);
var cSecret = Browser.inputBox("Twitter API Access", "Enter Consumer Secret:", Browser.Buttons.OK);
if(!cKey || !cSecret) return false;
UserProperties.setProperty("CONSUMER_KEY", cKey);
UserProperties.setProperty("CONSUMER_SECRET", cSecret);
// Install triggers
var triggers = ScriptApp.getScriptTriggers();
for(i in triggers) ScriptApp.deleteTrigger(triggers[i]);
ScriptApp.newTrigger("refreshTimeline").timeBased().everyMinutes(1).create();
ScriptApp.newTrigger("processCommand").forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet()).onEdit().create();
// Creat header row
SpreadsheetApp.getActiveSheet().setFrozenRows(1);
SpreadsheetApp.flush();
SpreadsheetApp.getActiveSheet().getRange(1, 1, 1, 3).setValues([["User", "Status","Action"]]).setFontWeight("bold");
Browser.msgBox("Please run refreshTimeline function from Script Editor to authorize access to Twitter API.");
}
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.addMenu("Tweetsheet", [
{ name : "Tweet", functionName : "sendTweetDialog" },
{ name: "Refresh timeline", functionName: "refreshTimeline" }
]);
};
function refreshTimeline() {
var count = 25;
try {
var result = UrlFetchApp.fetch("https://api.twitter.com/1/statuses/home_timeline.json?count="+count+"&include_my_retweet=1", authorize_("GET"));
var tweets = Utilities.jsonParse(result.getContentText());
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var fRows = sheet.getFrozenRows();
for (var i=0; i<tweets.length; i++) {
var rowIndex = i+fRows+1;
sheet.getRange("A" + (rowIndex)).setValue("@" + tweets[i].user.screen_name);
sheet.getRange("B" + (rowIndex)).setValue(tweets[i].text).clearFormat();
sheet.getRange("C" + (rowIndex)).clearContent();
sheet.getRange("D" + (rowIndex)).setFontColor("white").setValue(String(tweets[i].id_str));
if (Boolean(tweets[i].favorited)) {
sheet.getRange("B" + (rowIndex)).setBackgroundColor("yellow");
SpreadsheetApp.flush();
}
if (tweets[i].current_user_retweet) {
sheet.getRange("B" + (rowIndex)).setFontWeight("bold");
}
}
}
catch(err) {
Logger.log(err);
}
}
function processCommand(e) {
var command = e.value.toLowerCase();
if (e.range.getColumnIndex() == 3 && command != "") {
var rowId = e.range.getRowIndex();
var authorId = SpreadsheetApp.getActiveSheet().getRange("A"+rowId).getValue().toString();
var tweetId = SpreadsheetApp.getActiveSheet().getRange("D"+rowId).getValue().toString();
switch(command) {
case "<3":
fave_(tweetId);
break;
case "rt":
retweet_(tweetId);
break;
case "reply":
sendTweetDialog(tweetId, authorId);
break;
}
}
}
function fave_(e) {
if(e) {
try {
UrlFetchApp.fetch("https://api.twitter.com/1/favorites/create/" + e + ".json", authorize_());
SpreadsheetApp.getActive().toast("Added to favorites!");
} catch (err) {
Logger.log(err);
var message = Utilities.jsonParse(String(err).substr(String(err).indexOf("{")));
SpreadsheetApp.getActive().toast(message.error);
}
}
}
function retweet_(e) {
if(e) {
try {
UrlFetchApp.fetch("https://api.twitter.com/1/statuses/retweet/" + e + ".json", authorize_());
SpreadsheetApp.getActive().toast("Retweeted!");
} catch (err) {
Logger.log(err);
var message = Utilities.jsonParse(String(err).substr(String(err).indexOf("{")));
SpreadsheetApp.getActive().toast(message.errors);
}
}
}
function sendTweet_(tweet, tweetId) {
if(tweet) {
var requestData = authorize_();
requestData.payload = {status: tweet};
if (tweetId) requestData.payload.in_reply_to_status_id = tweetId;
try {
UrlFetchApp.fetch("https://api.twitter.com/1/statuses/update.json", requestData);
SpreadsheetApp.getActive().toast("Tweet sent!");
} catch (err) {
Logger.log(err);
}
}
}
function sendTweetDialog(tweetId, authorId) {
var input = Browser.inputBox("Compose new Tweet", "What's happening?", Browser.Buttons.OK_CANCEL);
if(authorId) input = authorId+" "+input;
if(input && input!="cancel") sendTweet_(input, tweetId);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment