Node.js twitter bot只转推父推文

时间:2017-09-05 09:35:31

标签: javascript node.js twitter

我试图创建一个Twitter机器人转发只有父推文(IE没有回复其他推文或页面转发的东西),我设法阻止机器人发推转推,但找不到任何关于如何停止它转发回复其他推文,任何人都可以帮忙吗?

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

1 个答案:

答案 0 :(得分:1)

知道了:

// 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}`)
            }
        });