Google Script/Sheets - Twitter API Integration POST statuses/update in_reply_to_status_id
I am not a developer so I apologize in advance if my question is really basic. I've managed to successfully install this Google script Twitter API integration below and send tweets from a Google sheet (the code was offered here). I simply use =sendTweet(message)
in a cell, replacing message
with the cell reference of where i have the text for the tweet, for example =sendTweet(C6)
and the new Tweet will contain the pre-prepared text in cell C6
.
What i'm trying to do is to add to the script the option of sending a tweet in reply to another tweet. Reading on Twitter's API documentation, I understand that the in_reply_to_status_id
parameter needs to pass the in_reply_to_status_id
in the API call URL but that's as far as my understanding goes.
I don't know how to define this new tweet_id variable and how to get it to pass the in_reply_to_status_id=tweet_id
string in the right place so it will function. The ideal would be to use the same formula but with the addition of tweet_id
for the reply, as a second variable. For example =sendTweet(message, tweet_id)
.
Your help would be much appreciated 🙏
// User-level Twitter API request // Requires the OAuth1 library to be pasted into the script. // https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library var CONSUMER_KEY = '************************'; var CONSUMER_SECRET = '************************'; var ACCESS_TOKEN = '************************'; var ACCESS_SECRET = '************************'; /** * Sends a tweet. * @param {string} message The message to send. * @return {?Object} The complex response object with the status of the send * request. See https://dev.twitter.com/rest/reference/post/statuses/update * for the structure of this object. */ function sendTweet(message) { if (typeof OAuth1 === 'undefined') { var libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library'; throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' + 'library from ' + libUrl + ' and append to the bottom of this script.'); } var params = ''; var tweet = message.substring(0, 160); var options = {method: 'POST', payload: {status: tweet}}; var authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET); var response = authUrlFetch.fetch('https://api.twitter.com/1.1/statuses/update.json', params, options); var responseText = response.getContentText(); return JSON.parse(responseText); }
You should be able to
add tweet_id as an argument in your function signature function sendTweet(message, tweet_id) { } and then include it in the payload. var options = { method: 'POST', payload: { status: tweet, in_reply_to_status_id: tweet_id } };Full code:
// User-level Twitter API request // Requires the OAuth1 library to be pasted into the script. // https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library var CONSUMER_KEY = '************************'; var CONSUMER_SECRET = '************************'; var ACCESS_TOKEN = '************************'; var ACCESS_SECRET = '************************'; /** * Sends a tweet. * @param {string} message The message to send. * @param {string} [tweet_id] - The ID of an existing status that * the update is in reply to. Note: This parameter will be ignored unless * the author of the Tweet this parameter references is mentioned within * the status text. Therefore, you must include @username , where username * is the author of the referenced Tweet, within the update. * @return {?Object} The complex response object with the status of the send * request. See https://dev.twitter.com/rest/reference/post/statuses/update * for the structure of this object. */ function sendTweet(message, tweet_id) { if (typeof OAuth1 === 'undefined') { var libUrl = 'https://developers.google.com/google-ads/scripts/docs/examples/oauth10-library'; throw Error('OAuth1 library not found. Please take a copy of the OAuth1 ' + 'library from ' + libUrl + ' and append to the bottom of this script.'); } var params = ''; var tweet = message.substring(0, 160); var options = { method: 'POST', payload: { status: tweet, in_reply_to_status_id: tweet_id } }; var authUrlFetch = OAuth1.withAccessToken(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET); var response = authUrlFetch.fetch('https://api.twitter.com/1.1/statuses/update.json', params, options); var responseText = response.getContentText(); return JSON.parse(responseText); }Full disclaimer, I haven't setup Twitter API access for myself to verify that this works. Please let me know if you have issues.
Thank you very much for your answer... Tweet goes out with text in "message" however not as a reply... just like before, a stand alone tweet
Strange, I tried following your method and passed another option (url) and twitter accept it. It is just the in_reply_to_twitter_id that it doesn't count for some reason. ` var options = { method: 'POST', payload: { in_reply_to_status_id: tweet_id, status: tweet, attachment_url: url }`
@EladRatson Did you include the username mention in the tweet? Recall the documentation stating: "Note: This parameter will be ignored unless the author of the Tweet this parameter references is mentioned within the status text. Therefore, you must include @username , where username is the author of the referenced Tweet, within the update."
Hi @Diego, looking for a way to contact you. Should you have a moment, could you please have a look at this (I'm using the same code but for another API call): stackoverflow.com/questions/65256374/…