Search code examples
javascriptnode.jstwittertwitter-streaming-api

Duplicate Status Error when Tweeting using Twitter API


I am new to JavaScript and I am using Node.js and the Twitter API as a way to learn how to code. I am also using the Twit NPM package. I am trying to create a "game" where a specific phrase has to be tweeted in order to begin. I have a function where if the specific phrase is not tweeted to the account, the account also replies to try again.

Whether the phrase is met or not, this function works once. However, what if the same user tries to tweet a phrase again (matching the phrase or not)? How do I avoid getting a duplicate status error? I am not sure what the best approach is here as it is out of my knowledge scope. Here is a simplified version of the code that returns a duplicate status error after both if conditions have been met by the same user and then they try again to tweet:

var Twit          = require('twit');
var T             = new Twit(config);

// Start a stream for listening for @mentions
var stream = T.stream('statuses/filter', { track: ['@someusername'] });
stream.on('tweet', somePhrase);

function somePhrase(tweet) {

  var request = tweet.text;
  var trigger = /(?:[^"']|^)(\bsome phrase\b)(?!["'])/ig;
  var name    = tweet.user.screen_name;
  var nameID  = tweet.id_str;

  // check to see if the tweet does not meet the request trigger
  if (request != trigger) {

    var reply   = '@' + name + "try again";
    var params  = { status: reply };

  }
  // check to see if the request matches the request trigger
  if (request.match(trigger)) {

    var question = "what do you want to know?";
    var params = {
        status: question + ' @' + name,
        in_reply_to_status_id: nameID
    };
  }

  T.post('statuses/update', params, function(err, data, response) {
    if (err !== undefined) {
      console.log(err);
    } else {
      console.log('Tweeted: ' + params.status);
    }
  })
};

This is the error code that the API returns:

{ [Error: Status is a duplicate.]
  message: 'Status is a duplicate.',
  code: 187,
  allErrors: [ { code: 187, message: 'Status is a duplicate.' } ],
  twitterReply: { errors: [ [Object] ] },
  statusCode: 403 }

Solution

  • Twitter compares the post text against recent texts to determine whether or not it's a duplicate. I don't know how strict this comparison is. But, one possible way to get around this could be to append something unique to each status text.

    A useful unique number could be a counter for the number of games that have been played. Ex: "What do you want to know? @jaime #0001"

    To create a counter like that, you could use a database, but if you don't want to use a database, you can use the Twit npm package to get the number you used in your last tweet and increment it. That way, it's always a unique number.

    You could also just generate a random string to append, but that's not a good practice.

    Edit: If character length is not an issue (since Twitter is standing by the 140-character limit), you can just append the UNIX timestamp, which will always be a unique number. This is very easy to include with the Date() object.