Skip to content

Instantly share code, notes, and snippets.

@PeterBerthelsen
Created January 21, 2019 17:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterBerthelsen/10d759ab4012d0b1e15ac48bef82da7e to your computer and use it in GitHub Desktop.
Save PeterBerthelsen/10d759ab4012d0b1e15ac48bef82da7e to your computer and use it in GitHub Desktop.
OrthoBot tweetThread function
function tweetThread(stringToTweet,delim,img,r){ //pass a string variable with additional option to parse (used for bookending tweets)
//////////////////////////Variables////////////////////////////////////////////////////////
var ss = SpreadsheetApp.getActiveSpreadsheet(); //spreadsheet
var twtMax = 280; //Character limit for Twitter (variable in case it changes)
var tweets = new Array(0); //create final array to populate with tweets
var total = ""; //variable for 'total' tweet-length section if parsing is required
var start = 0; //starting character for .substring() function, default 0
var excess = 0; //number of characters to strip off of 'total' to remove split words at end of tweet
var parsed = ""; //parsed tweet (total - excess) to add to tweets
var url = ""; //parsed image URLs
console.log("tweetThread initiated!"); //log function initiation
console.log("using params: " //log paramaters being passed through function
+ "|stringToTweet:" + stringToTweet
+ "|delim:" + delim
+ "|img:" + img);
//////////////////////////Length Check/////////////////////////////////////////////////////
try { //Attempt to check string value for parsing needs
if (stringToTweet == undefined) {throw "Undefined Value: 'stringToTweet'"}; //thrown when tweet string is undefined
var thread = stringToTweet.split(delim); //split initial string along provided delimeter
console.log("Input Thread Length: " + thread.length); //log number of strings in thread
} catch(e) { //Catch thrown error
console.log("Cannot Split Thread. Error: " + e); //Log error
}
try { //attempt to evaluate parsed tweet thread
if (thread == undefined) {throw "Undefined Value: 'thread'"}; //thrown when thread value doesn't exist
for (i in thread){ //for each string in thread...
if (thread[i].length == 0) {throw "Invalid Value: thread[" + i + "]"}; //thrown when thread has string error
console.log("Thread section characters: " + thread[i].length); //log string length
if (thread[i].length > twtMax){ //check thread[i] length
console.log("Cannot Tweet! Too long! Parsing..."); //if thread [i] too long for tweet, needs to be split
start = 0; //reset starting character for substring
//////////////////////////Parsing Loop/////////////////////////////////////////////////////
while (start<thread[i].length) { //As long as the starting position is NOT at the end of thread[i]...
total = thread[i].substring(start,start + twtMax); //full length of tweet
excess = total.length - total.lastIndexOf(" "); //amount of characters to cut off of 'total' to get to a space (end of last full word)
parsed = thread[i].substring(start,start + twtMax - excess); //new text of tweet-length string ending in full word
start = start + parsed.length + 1; //new start position is the first character after finished tweet
tweets.push(parsed); //adds tweet to thread
console.log("Parsed new tweet," + " length: " +
parsed.length +
" | Tweetable! Adding to tweet thread"); //log parse length, success
console.log("Tweet Added: " + parsed);
} //end parsing loop
console.log("Parsing Complete"); //log parsing complete
} else { //if thread[i] is tweet length...
console.log("Tweetable! Adding to tweet thread"); //log length
tweets.push(thread[i]) //add thread[i] to tweets
console.log("Tweet Added: " + thread[i]);
console.log("Tweet Thread length: " + tweets.length);
}
} //end loop
} catch (e) { //Catch thrown error
console.log("Parsing Error. Error: " + e); //Log error
}
//////////////////////////Split Images/////////////////////////////////////////////////////
if (img){ //if image string has been passed...
var imgs = img.split(","); //split CSV into individual URLs
console.log(img + " contains " + imgs.length + " values!"); //log number of images found
} else { //if no image string has been passed...
console.log("No images passed through threading function"); //log no images
} //end image processing
//////////////////////////Output///////////////////////////////////////////////////////////
if (r) {
var replyTo = r
} else {
var replyTo = 0; //set reply (each tweet will reply to previous, first will be null
}
for (i in tweets){ //for each tweet in thread...
console.log("Tweet Attempt: " + i); //log attempt number (starting @ 0)
try{
if (imgs[i]) { //if valid image URL exists...
url = imgs[i].toString(); //set URL variable for tweet function
console.log("imgs[" + i + "]: " + imgs[i]); //log image
} else {
url = null;
console.log("imgs[" + i + "]: " + imgs[i]);
}
} catch (e) {
url = null; //null variable for tweet function
console.log("imgs[" + i + "] parse error: " + e); //log image error
}
var newTweet = sendTweet(tweets[i], replyTo, url); //set reply value for next tweet as return of tweet function
Utilities.sleep(250); //wait 1/4 second (for return value, to avoid errors)
if (i == 0) { //if first tweet in thread...
var begins = newTweet //set value for first tweet ID
console.log("Thread begins with tweet(ID): " + begins); //log first tweet ID
}
replyTo = newTweet //set replyTo value to response of last tweet
if (replyTo == undefined) { //if no response...
console.log("replyTo undefined! Breaking loop..."); //log undefined response
break; //break thread, do not tweet more
} else { //if response...
console.log("Next Reply To: " + replyTo); //log reply
}
} //end thread tweeting
return begins;
} //END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment