Twitter API returning "Status is a Duplicate"

Asked

Viewed 35 times

-2

I’m creating a bot on Twitter to respond every time it’s mentioned with random text. It’s working fine, but the problem is that the API returns the "Status is a Duplicate".

This is the part that randomly chooses the texts that are in an array to publish, but I don’t think the error is in it:

var exibir = message[Math.floor((Math.random() * message.length))];

And that’s the finding, filtering and replying part:

Bot.get("search/tweets", { q: 'hey @namebot' }, (err, data) => {
  if (err) {
    console.log(err.message);
  } else {
    var tweetId = data.statuses[0].id_str;
    var username = data.statuses[0].user.screen_name;
    var tweetsRespondidos = [];
    var i = 1;

    if (tweetsRespondidos.includes(tweetId)) {
      console.log('Esse tweet já foi respondido!');
    } else {
      Bot.post('statuses/update', {
        in_reply_to_status_id: tweetId,
        screen_name: username,
        status: @ ${username} ${exibir}
      }, function(err) {
        if (err) {
          console.log(err.message);
        } else {
          console.log('Resposta enviada! ');
          tweetsRespondidos[i] = tweetId;
          i++;
        }
      })
    }
  }
})
  • Give a console.log to see what status he is going through and to be able to better understand how he is being duplicated. No serious accent is missing \`` no status: @${username} ${view}`?

  • I fixed the seats, but there was no difference. in the log console I went to see and the status are in fact sending the same single text several times to the same tweet. when a new mention appears the bot manages to send a random text as 'hi', and then repeats the 'hi' until another different mention appears and it generates a 'hey', which also repeats...

1 answer

2


There are some problems in your code:

  1. Is not using properly strings template in the status. The line correction is:
Bot.post('statuses/update', {
  in_reply_to_status_id: tweetId,
  screen_name: username,
  status: `@${username} ${exibir}` // Repare nos acentos graves `
}, function(err) {  
  1. You are not correctly adding an element to the array. The Array has a method called push to add an element to it:
tweetsRespondidos.push(tweetId); // Veja que você não precisa mais da variável i
  1. If you’re repeating the status, is repeating the value of username and of exibir. You don’t show where you’re generating the value of exibir, but should generate it whenever it sends a message, that is, within the else:
} else {
  // Gere uma nova mensagem antes de enviá-la
  var exibir = message[Math.floor((Math.random() * message.length))];

  Bot.post('statuses/update', { 
    in_reply_to_status_id: tweetId,
    screen_name: username,
    status: `@${username} ${exibir}` // Aplicando a correção 1
  }, function(err) {  
    if (err) {
      console.log(err.message);
    } else {
      console.log('Resposta enviada! ');
      tweetsRespondidos.push(tweetId); // Aplicando a correção 2
    }    
  })
}

This should work. Note that there are still chances of you responding to the same user with the same random message. In this case, there are two alternatives you can implement:

  • Send a unique value in each status. It can be a number that increases, for example. It can be the tweetId.

  • Store messages sent to each user. This way, you can validate if that user has already received that message before sending it again.

The second alternative is not necessarily the best. It may happen that the user "spend" all the messages from his bot. Twitter has a time that allows the status repeat, if I’m not mistaken, but I can’t confirm that.

If you want to solve this problem, I believe the first option is valid. If you have too many random messages and too few calls from the same user in a short amount of time, you might not even have to worry about it.

  • Thank you, it was a problem to show off even though I was out of Else, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.