-
-
Save zfz/3330615 to your computer and use it in GitHub Desktop.
Use Twitter directly in your Google Doc, so no one will ever blame you for being social
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CONSUMER_KEY = "<< YOUR KEY HERE >>"; | |
var CONSUMER_SECRET = "<< YOUR SECRET HERE >>"; | |
function getConsumerKey() { | |
return CONSUMER_KEY; | |
} | |
function getConsumerSecret() { | |
return CONSUMER_SECRET; | |
} | |
function onOpen() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
sheet.addMenu("Tweetsheet", [ | |
{ name : "Tweet", functionName : "renderTweetDialog" }, | |
{ name: "Refresh timeline", functionName: "refreshTimeline" } | |
]); | |
}; | |
function refreshTimeline() { | |
authorize(); | |
var requestData = { | |
"method": "GET", | |
"oAuthServiceName": "twitter", | |
"oAuthUseToken": "always" | |
}; | |
try { | |
var result = UrlFetchApp.fetch( | |
"https://api.twitter.com/1/statuses/home_timeline.json?include_rts=1", | |
requestData); | |
var tweets = Utilities.jsonParse(result.getContentText()); | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
for (var i = 0; i < tweets.length; i++) { | |
sheet.getRange("A" + (i+3)).setValue("@" + tweets[i].user.screen_name); | |
sheet.getRange("B" + (i+3)).setValue(tweets[i].text).clearFormat(); | |
sheet.getRange("C" + (i+3)).clearContent(); | |
sheet.getRange("D" + (i+3)).setFontColor("white").setValue(tweets[i].id); | |
if (tweets[i].favorited) { | |
sheet.getRange("B" + (i+3)).setBackgroundColor("yellow"); | |
} | |
if (tweets[i].current_user_retweet) { | |
sheet.getRange("B" + (i+3)).setFontWeight("bold"); | |
} | |
} | |
} | |
catch(e) { | |
Logger.log(e); | |
} | |
} | |
function onEdit() { | |
try { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet(); | |
if (sheet.getActiveCell().getColumn() != 3) { | |
return; | |
} | |
var rowId = sheet.getActiveCell().getRow(); | |
var command = sheet.getRange("C" + rowId).getValue(); | |
var tweetId = sheet.getRange("D" + rowId).getValue(); | |
if (command == "") { | |
return; | |
} | |
var validCommands = { | |
"<3": "fave", | |
"rt": "retweet", | |
"reply": "reply" | |
}; | |
var app = UiApp.createApplication().setTitle('What do you want to tweet today?'); | |
var handler; | |
if (command in validCommands) { | |
switch(validCommands[command]) { | |
case "fave": | |
handler = app.createServerHandler("fave"); | |
break; | |
case "retweet": | |
handler = app.createServerHandler("retweet"); | |
break; | |
case "reply": | |
handler = app.createServerHandler("sendTweet"); | |
break; | |
} | |
} | |
var tweet = app.createTextBox().setName("tweetId").setWidth("100%").setValue(tweetId); | |
tweet.setVisible(false); | |
handler.addCallbackElement(tweet); | |
app.setHeight(100); | |
var sendButton = app.createButton("Yes", handler); | |
var dialogPanel = app.createFlowPanel(); | |
dialogPanel.add(tweet); | |
if (command == "reply") { | |
var tweetText = app.createTextBox().setName("tweet").setWidth("100%").setValue(sheet.getRange("A" + rowId).getValue()); | |
dialogPanel.add(tweetText); | |
handler.addCallbackElement(tweetText); | |
} | |
dialogPanel.add(sendButton); | |
app.add(dialogPanel); | |
sheet.show(app); | |
} catch (e) { | |
Logger.log(err); | |
} | |
} | |
function fave(e) { | |
var requestData = { | |
"method": "POST", | |
"oAuthServiceName": "twitter", | |
"oAuthUseToken": "always" | |
}; | |
try { | |
authorize(); | |
var result = UrlFetchApp.fetch( | |
"https://api.twitter.com/1/favorites/create/" + e.parameter.tweetId + ".json", | |
requestData); | |
} catch (err) { | |
Logger.log(err); | |
} | |
var app = UiApp.getActiveApplication(); | |
app.close(); | |
return app; | |
} | |
function retweet(e) { | |
authorize(); | |
var requestData = { | |
"method": "POST", | |
"oAuthServiceName": "twitter", | |
"oAuthUseToken": "always" | |
}; | |
try { | |
var result = UrlFetchApp.fetch( | |
"https://api.twitter.com/1/statuses/retweet/" + e.parameter.tweetId + ".json", | |
requestData); | |
} catch (err) { | |
Logger.log(Utilities.jsonStringify(err)); | |
} | |
var app = UiApp.getActiveApplication(); | |
app.close(); | |
return app; | |
} | |
function renderTweetDialog() { | |
var doc = SpreadsheetApp.getActiveSpreadsheet(); | |
var app = UiApp.createApplication().setTitle("Send Tweet"); | |
app.setHeight(100); | |
var helpLabel = app.createLabel("Enter your tweet below:"); | |
helpLabel.setStyleAttribute("text-align", "justify"); | |
var tweet = app.createTextBox().setName("tweet").setWidth("100%"); | |
var sendHandler = app.createServerClickHandler("sendTweet"); | |
sendHandler.addCallbackElement(tweet); | |
var sendButton = app.createButton("Send Tweet", sendHandler); | |
var dialogPanel = app.createFlowPanel(); | |
dialogPanel.add(helpLabel); | |
dialogPanel.add(tweet); | |
dialogPanel.add(sendButton); | |
app.add(dialogPanel); | |
doc.show(app); | |
} | |
function authorize() { | |
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(getConsumerKey()); | |
oauthConfig.setConsumerSecret(getConsumerSecret()); | |
var requestData = { | |
"method": "GET", | |
"oAuthServiceName": "twitter", | |
"oAuthUseToken": "always" | |
}; | |
try { | |
var result = UrlFetchApp.fetch( | |
"https://api.twitter.com/1/statuses/mentions.json", | |
requestData); | |
} catch(e) { | |
Logger.log(e); | |
} | |
} | |
function sendTweet(e) { | |
var tweet = e.parameter.tweet; | |
var tweetId = e.parameter.tweetId; | |
authorize(); | |
// Tweet must be URI encoded in order to make it to Twitter safely | |
var encodedTweet = encodeURIComponent(tweet); | |
var requestData = { | |
"method": "POST", | |
"oAuthServiceName": "twitter", | |
"oAuthUseToken": "always" | |
}; | |
if (tweetId) { | |
requestData.payload = { "in_reply_to_status_id": tweetId }; | |
} | |
try { | |
var result = UrlFetchApp.fetch( | |
"https://api.twitter.com/1/statuses/update.json?status=" + encodedTweet, | |
requestData); | |
} catch (e) { | |
Logger.log(e); | |
} | |
var app = UiApp.getActiveApplication(); | |
app.close(); | |
return app; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment