twitter的节点回复脚本实际上没有回复

时间:2016-08-18 01:52:09

标签: node.js twitter bots reply

这个Twitter脚本目前可以在推文时发布所选用户的推文,但不是在推文下面实际回复,而是将其作为独立的新推文推文。如何让它实际回复而不是发新的推文?我使用的是Twit API:https://github.com/ttezel/twit

以下是我所拥有的:

    console.log('The bot is starting');

var Twit = require('twit');

var config = require('./config');
var T = new Twit(config);

//Setting up a user stream
var stream = T.stream('user');

stream.on('tweet', tweetEvent);

function tweetEvent(eventMsg) {

   var replyto = eventMsg.user.screen_name;
   var text = eventMsg.text;
   var from = eventMsg.user.screen_name;

   console.log(replyto + ' '+ from);

   if (replyto =='other user's handle') {
    var newtweet = '@' + from + ' Hello!';
    tweetIt(newtweet);
   }

}

function tweetIt(txt) {

    var tweet = {
     status: txt
     }

    T.post('statuses/update', tweet, tweeted);

    function tweeted(err, data, response) {
      if (err) {
        console.log("Something went wrong!");
      } else {
        console.log("It worked!");
      }
    }
}

1 个答案:

答案 0 :(得分:1)

要使用Twitter API在推文会话中显示回复,您需要以下内容:

// the status update or tweet ID in which we will reply
var nameID  = tweet.id_str;

还需要in_reply_to_status_id状态中的参数tweet。请参阅下面的代码更新,它现在应该保留对话:

console.log('The bot is starting');

var Twit = require('twit');

var config = require('./config');
var T = new Twit(config);

//Setting up a user stream
var stream = T.stream('user');

stream.on('tweet', tweetEvent);

function tweetEvent(tweet) {
    var reply_to = tweet.in_reply_to_screen_name;
    var text       = tweet.text;
    var from       = tweet.user.screen_name;
    var nameID     = tweet.id_str;
    // params just to see what is going on with the tweets
    var params     = {reply_to, text, from, nameID};
    console.log(params);

    if (reply_to === 'YOUR_USERNAME') {
        var new_tweet = '@' + from + ' Hello!';
        var tweet = {
            status: new_tweet,
            in_reply_to_status_id: nameID
        }

        T.post('statuses/update', tweet, tweeted);

        function tweeted(err, data, response) {
            if (err) {
                console.log("Something went wrong!");
            } else {
                console.log("It worked!");
            }
        }
    }
}