I'm looking for a way to send a tweet whenever a new row appears in a Google Spreadsheet. I thought this might be a good starting point, but I'm going to need some help with editing it.
I'd like to be able to automatically send tweets when a new row appears in a certain sheet. In addition to that, I'd like to be able to define a few Twitter parameters which would be represented by data in specific columns. For example:
- Column A: in_reply_to_status_id
- Column B: status
I'd be really grateful if anyone could help me with it!
Here's the original code:
function sendTweet() {
authorizeTwitter();
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
for (var i in data) {
tweet(data[i]);
Utilities.sleep(1000);
}
}
function authorizeTwitter() {
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("CONSUMER_KEY");
oauthConfig.setConsumerSecret("CONSUMER_SECRET");
}
function tweet(tweet) {
var encodedTweet = encodeURIComponent(tweet);
var requestData = {
"method": "POST",
"oAuthServiceName": "twitter",
"oAuthUseToken": "always"
};
try {
var result = UrlFetchApp.fetch(
"https://api.twitter.com/1.1/statuses/update.json?status=" + encodedTweet,
requestData);
catch (e) {
Logger.log(e);
}
}