babusi babusi - 4 months ago 13
Node.js Question

Node reply to tweet does not actually reply

I'm using the

Twit
Node library to reply to tweets in a stream and while it's working perfectly the tweeted reply does not show up as a reply on the timeline, instead, it appears as a standalone tweet, not linked to a prior conversation.

Here's my code:

function tweetEvent(eventMsg) {
var replyto = eventMsg.in_reply_to_screen_name;
var text = eventMsg.text;
var from = eventMsg.user.screen_name;

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

if( (text.indexOf('myhandle') >= 0) || (from != 'myhandle')) {
var reply = replies[Math.floor(Math.random() * replies.length)];
var newtweet = '@' + from + ' ' + reply;
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!");
}
}
}

Answer

In order for the reply to show up in the timeline using the Twitter API, you need the following:

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

Also needed is the parameter in_reply_to_status_id in your tweet status. See the updates to your code below and it should now preserve the conversation:

function tweetEvent(eventMsg) {
    var replyto = eventMsg.in_reply_to_screen_name;
    var text = eventMsg.text;
    var from = eventMsg.user.screen_name;
    // the status update or tweet ID in which we will reply
    var nameID  = eventMsg.id_str;


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

    if( (text.indexOf('myhandle') >= 0) || (from != 'myhandle')) {
        var reply = replies[Math.floor(Math.random() * replies.length)];
        var newtweet = '@' + from + ' ' + reply;
        tweetIt(newtweet);
    }

    function tweetIt(txt) {

        var tweet = {
          status: txt,
          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!");
      }
    }
}
Comments