Google Script/Sheets - Twitter API 集成 POST statuses/update in_reply_to_status_id

Google Script/Sheets - Twitter API Integration POST statuses/update in_reply_to_status_id

我不是开发人员,所以如果我的问题真的很基础,我提前道歉。我已经成功地安装了下面这个 Google 脚本 Twitter API 集成并从 Google sheet 发送推文(提供了代码 here)。我只是在一个单元格中使用 =sendTweet(message),将 message 替换为我拥有推文文本的单元格引用,例如 =sendTweet(C6),新推文将包含预先准备好的文本在 C6.

单元格中

我想做的是在脚本中添加发送一条推文以回复另一条推文的选项。阅读 Twitter's API documentation,我了解到 in_reply_to_status_id 参数需要在 API 调用 URL 中传递 in_reply_to_status_id,但就我的理解而言。

我不知道如何定义这个新的 tweet_id 变量以及如何让它在正确的位置传递 in_reply_to_status_id=tweet_id 字符串以便它起作用。理想的情况是使用相同的公式,但在回复中添加 tweet_id 作为第二个变量。例如 =sendTweet(message, tweet_id).

非常感谢您的帮助

// 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);
}

你应该可以

  1. 在函数签名中添加 tweet_id 作为参数
function sendTweet(message, tweet_id) { }
  1. 然后将其包含在有效负载中。
var options = {
  method: 'POST',
  payload: {
    status: tweet,
    in_reply_to_status_id: tweet_id
  }
};

完整代码:

// 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);
}

完全免责声明,我还没有为自己设置 Twitter API 访问权限来验证这是否有效。如果您有任何问题,请告诉我。