Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use Twitter directly in your Google Doc, so no one will ever blame you for being social(backup the tweets of list)
var CONSUMER_KEY = "<< YOUR KEY HERE >>";
var CONSUMER_SECRET = "<< YOUR SECRET HERE >>";
var OWNER_SCREEN_NAME = "<< YOUR LIST OWNER'S NAME HERE >>";
var SLUG = "<< YOUR LIST NAME HERE >>";
//Then fill the 235682008200790016 into E1
function getConsumerKey() {
return CONSUMER_KEY;
}
function getConsumerSecret() {
return CONSUMER_SECRET;
}
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.addMenu("tweets", [
{ name : "get", functionName : "renderTweetDialog" },
{ name: "post", functionName: "refreshTimeline" }
]);
};
function refreshTimeline() {
authorize();
var requestData = {
"method": "GET",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
try {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var maxTweetId = sheet.getRange("E" + 1).getValue() ;//235667475570761728;
var result = UrlFetchApp.fetch(
// "https://api.twitter.com/1/statuses/home_timeline.json?include_rts=1",
"https://api.twitter.com/1/lists/statuses.json?slug=" + SLUG + "&per_page=100&owner_screen_name=" + OWNER_SCREEN_NAME + "&include_rts=true&include_entities=true&since_id="+maxTweetId,
requestData);//235682008200790016
var tweets = Utilities.jsonParse(result.getContentText());
/*
if(1 == tweets.length){
return ;
}
*/
for (var i = tweets.length - 1; i > 0; i--) {//notice here dont use "i > -1" for we will ignore the last tweet to avoid duplicate when next time call this method
sheet.insertRowAfter(1);
sheet.getRange("A" + (3)).setValue("@" + tweets[i].user.screen_name);
sheet.getRange("B" + (3)).setValue(tweets[i].text).clearFormat();
sheet.getRange("C" + (3)).clearContent();
sheet.getRange("D" + (3)).setFontColor("white").setValue(tweets[i].id);
sheet.getRange("E" + 1).setValue(tweets[i].id).clearFormat();
//maxTweetId = tweets[i].id;
if (tweets[i].favorited) {
sheet.getRange("B" + (3)).setBackgroundColor("yellow");
}
if (tweets[i].current_user_retweet) {
sheet.getRange("B" + (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
You can’t perform that action at this time.