Created
April 19, 2020 12:00
-
-
Save nidhinkumar06/5e4ddc3dad09a1fc0221065176e3cc5c to your computer and use it in GitHub Desktop.
Automated Tweets
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
function sendTweets() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
var startRowNumber = 1; | |
var endRowNumber = sheet.getLastRow(); | |
var twitterKeys = { | |
TWITTER_CONSUMER_KEY: "//add your API key", | |
TWITTER_CONSUMER_SECRET: "//add your API secret key", | |
TWITTER_ACCESS_TOKEN: "//add your access token key", | |
TWITTER_ACCESS_SECRET: "//add your access token secret key", | |
} | |
var props = PropertiesService.getScriptProperties(); | |
props.setProperties(twitterKeys); | |
var params = new Array(0); | |
var service = new Twitterlib.OAuth(props); | |
var quote; | |
var identifier; | |
for (var currentRowNumber = startRowNumber; currentRowNumber <= endRowNumber; currentRowNumber++) { | |
var row = sheet.getRange(currentRowNumber + ":" + currentRowNumber).getValues(); | |
// check that the second column (Date) is equal to today | |
if (isToday(row[0][1])) { | |
quote = row[0][0]; | |
identifier = currentRowNumber - 1; | |
if (!service.hasAccess()) { | |
console.log("Authentication Failed"); | |
} else { | |
console.log("Authentication Successful"); | |
var status = quote + "\n\n" + "#Quotes #Motivation"; | |
try { | |
var response = service.sendTweet(status, params); | |
console.log(response); | |
} catch (e) { console.log(e) } | |
} | |
break; | |
} | |
} | |
} | |
function isToday(date) { | |
var today = new Date(); | |
var dateFromRow = new Date(date); | |
return dateFromRow.getDate() == today.getDate() && | |
dateFromRow.getMonth() == today.getMonth() && | |
dateFromRow.getFullYear() == today.getFullYear() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment