如何使用 Twitter 回复提及 API

How to respond to mentions using Twitter API

我有一个功能正常的 Twitter 机器人。当他们回复我时,它补充了某人,这是当@myTwitterHandle 是推文中的第一件事时。以下代码允许我回复他们:

function tweetEvent(tweet) {

  // Who is this in reply to?
  var reply_to = tweet.in_reply_to_screen_name;
  // Who sent the tweet?
  var name = tweet.user.screen_name;
  // What is the text?
  var txt = tweet.text;

  // Ok, if this was in reply to me
  // Replace myTwitterHandle with your own twitter handle
  console.log(reply_to, name, txt);
  if (reply_to === 'myTwitterHandle') {

  ¦ // Get rid of the @ mention
  ¦ txt = txt.replace(/@selftwitterhandle/g, '');

  ¦ // Start a reply back to the sender
  ¦ var reply = "You mentioned me! @" + name + ' ' + 'You are super cool!';

  ¦ console.log(reply);
  ¦ // Post that tweet!
  ¦ T.post('statuses/update', { status: reply }, tweeted);
  }
}

我只想在有人在推文正文中@提及我时发送完全相同的回复。我正在使用 Node.js 和 twit api client

您可能正在参考找到的教程 here

我相信这就是您正在寻找的

I just want to send the exact same reply whenever anyone @mentions me somewhere in the body of their tweet.

此脚本实现了预期的结果:

var stream = T.stream('statuses/filter', { track: ['@myTwitterHandle'] });
stream.on('tweet', tweetEvent);

function tweetEvent(tweet) {

    // Who sent the tweet?
    var name = tweet.user.screen_name;
    // What is the text?
    // var txt = tweet.text;
    // the status update or tweet ID in which we will reply
    var nameID  = tweet.id_str;

     // Get rid of the @ mention
    // var txt = txt.replace(/@myTwitterHandle/g, "");

    // Start a reply back to the sender
    var reply = "You mentioned me! @" + name + ' ' + 'You are super cool!';
    var params             = {
                              status: reply,
                              in_reply_to_status_id: nameID
                             };

    T.post('statuses/update', params, function(err, data, response) {
      if (err !== undefined) {
        console.log(err);
      } else {
        console.log('Tweeted: ' + params.status);
      }
    })
};