Search code examples
javascriptnode.jstwitterbotsntwitter

Using nTwitter to reply to a Tweet with node.js


I have created a very simple twitter bot using node.js & nTwitter.

It searches for a 'certain word' & replies the user with a random quote. I have managed to do everything right. The code runs & streams perfectly.

But there's a huge drawback. When the bot replies to a tweet, on Twitter it doesn't show it as a conversation.

What it does is just tweet the person who tweeted the 'certain word'. As shown in the snap below although it replied to the user, it's not a conversation & the user has NOT deleted his tweet.

Here's a pic of what I'm talking about:

enter image description here

And here's my code. The tokens are handled with auth.js

var ntwitter = require('ntwitter');
var auth = require('../auth');

var bot = new ntwitter(auth);


var callback = function handleError(error) {
   if (error) {
   console.error('response status:', error.statusCode);
   console.error('data:', error.data);
  }
};

function startStreaming() {

  bot.stream('statuses/filter', { track: 'certain word' }, function(stream) {

    console.log('Listening for Tweets...');

    stream.on('data', function(tweet) {

      if (tweet.text.match(/certain\sword/)) {

          bot.updateStatus('@' + tweet.user.screen_name + ' True that' ,
          tweet.user.screen_name , callback);

       }
      });

     });
}


startStreaming();

I guess the issue is with bot.updateStatus().

Please help me out. Thanks in advance.


Solution

  • You would have to add a parameter in_reply_to_status_id to your request (which is the status id of user's tweet you are replying to. Ref: POST statuses/update

    Here's an example.

    function startStreaming() {
        bot.stream('statuses/filter', { track: 'certain word' }, function(stream) {
            console.log('Listening for Tweets...');
            stream.on('data', function(tweet) {
                if (tweet.text.match('certain word')) {
                    bot.updateStatus('@' + tweet.user.screen_name + ' True that' ,
                        {in_reply_to_status_id: tweet.id_str}, callback);
                }
            });
        });
    }
    
    startStreaming();