简体   繁体   中英

Node.js twitter bot to only retweet parent tweets

Im trying to create a twitter bot to retweet only parent tweets (IE no replies to other tweets or things the page has retweeted), I've managed to stop the bot tweeting retweets but can't find anything on how to stop it retweeting replies to other tweets, can anyone help?

/requires twit module
var twit = require('twit');
//require the config.js file
var config = require('./config.js');
//pass key values from config to twit to allow it to tweet ect
var Twitter = new twit(config);

//query for tweets
var retweet = function() {
  var params = {
    //q is query, this one looks for tweets from city that are not retweets
    q: 'from:@officialbantams -RT',
    //makes sure to only retweet english twit
    lang: 'en'
    //I've commented this out but it filters for only retweeting a tweet posted since the last time the function ran
    //result_type: 'recent',
  } 

 // for more parametes, see: 
https://dev.twitter.com/rest/reference/get/search/tweets

    Twitter.get('search/tweets', params, function(err, data) {
      // if there no errors
        if (!err) {
          // grab ID of tweet to retweet
            var retweetId = data.statuses[0].id_str;
            // Tell TWITTER to retweet
           Twitter.post('statuses/retweet/:id', {
            id: retweetId
            //post in console that a tweet has been retweeted
        }, function(err, response) {
            if (response) {
                console.log('Retweeted!!!');
            }
            // if there was an error while tweeting
            if (err) {
                console.log('Something went wrong while RETWEETING... Duplication maybe...');
            }
        });
    }
    // if unable to Search a tweet
    else {
      console.log('Something went wrong while SEARCHING...');
    }
});
}

// grab & retweet as soon as program is running...
retweet();
// retweet in every 50 minutes (time is in ms so 100*60*number of minutes wanted
setInterval(retweet, 50*60*100);

Got it :

// Get the tweet Id from the returned data
var id = data.statuses[i].id_str;
      // Tell TWITTER to retweet
      T.post('statuses/retweet/' + id, {}, function(err, response) {
           // If the retweet fails, log the error message
           if(err){
                console.log(err[0].message);
            }
            // If the favorite is successful, log the url of the tweet
            else{
                let username = response.user.screen_name;
                let tweetId = response.id_str;
                console.log('Retweeted: ', `https://twitter.com/${username}/status/${tweetId}`)
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM