Pergunta

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.

Foi útil?

Solução

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();

Outras dicas

You have to add a parameter in_reply_to_status_id in order to start a conversation with the Twitter User.

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();

Note that in_reply_to_status_id won't work if you don't mention the username to whom you are replying in the tweet_text. Also, you cant use the same tweet_text, or else that would count as spamming.

Source: https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-update

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top