【问题标题】:Issues with 'Follow' on Twitter APITwitter API 上的“关注”问题
【发布时间】:2018-12-06 15:29:37
【问题描述】:

我正在做一个项目,我想关注两个独立的推特账户,并在每个账户发推文时调用一个特定的函数。我正在使用 Twitter API、Node.js 和一个名为 Twit 的 NPM 模块。

当一个帐户发推但不是两个都发推时,我可以正常工作:

我相信我的问题可能在这里:

var stream = T.stream('statuses/filter', { follow: ( userID1 , userID2 ) });

如果我只将关注设置为一个用户,它可以正常工作,但是如果有两个,它只能与一个一起工作。此外,它仅适用于列表中的第二个,因此如果它:userID1, user ID2 只有 userID2 将起作用。如果它的 userID2, userID1 只有 userID1 可以工作。

这里有完整的代码/逻辑:

//SetUp info

var Twit = require('twit'); // NPM twit package
var config = require('./config'); // the config file to allow me access Auth Keys etc
var T = new Twit(config);//run config file

 //end of setup

var userID1 = 'XXXXXXXXXXX1'; //TwitterAccount1
var userID2 = 'XXXXXXXXXXX2'; //TwitterAccount2


//these two lines set up the twitter API
var stream = T.stream('statuses/filter', { follow: ( userID1 ,  userID2 ) });  // here seems to be my issue?
stream.on('tweet', function (tweet) {


if (tweet.user.id == userID1 ) { // is tweet.user.id matches UserID1

  DoStuff_1(); //call this function

}  else if  (tweet.user.id == userID2 ) { // if tweet.user.id matches UserID2

DoStuff_2(); // call this function instead

}

 });

//Function for userID1
function DoStuff_1(){
console.log("Doing Stuff 1");
}

//Function for userID2
function DoStuff_2(){
console.log("Doing Stuff 2");

}

任何建议都非常感谢!

【问题讨论】:

    标签: node.js twitter


    【解决方案1】:

    你可以用一些流来做这一切;只需使用用户 ID 创建一个数组,然后将它们加入到以下参数中,如下所示:

    var userId = ['XXXXXXXXXXX1','XXXXXXXXXXX2']; 
    var stream = T.stream('statuses/filter', { follow: userId.join(',') }); 
    

    【讨论】:

      【解决方案2】:

      通过为第二个用户帐户建立一个单独的流,它似乎可以工作:

      //SetUp info
      
      var Twit = require('twit'); // NPM twit package
      var config = require('./config'); // the config file to allow me access Auth Keys etc
      var T = new Twit(config);//run config file
      
       //end of setup
      
      var userID1 = 'XXXXXXXXXXX1'; //TwitterAccount1
      
      //these two lines set up the twitter API
      var stream = T.stream('statuses/filter', { follow: ( userID1  ) });  // here seems to be my issue?
      stream.on('tweet', function (tweet) {
      
      
      if (tweet.user.id == userID1 ) { // is tweet.user.id matches UserID1
      
        DoStuff_1(); //call this function
      
      }  
      
       });
      
      
      var userID2 = 'XXXXXXXXXXX2'; //TwitterAccount2
      
      //Separate stream for UserID2
      
      var stream = T.stream('statuses/filter', { follow: ( userID2  ) });  // here seems to be my issue?
      stream.on('tweet', function (tweet) {
      
      if  (tweet.user.id == userID2 ) { // if tweet.user.id matches UserID2
      
      DoStuff_2(); // call this function instead
      
      }
      
       });
      
      //Function for userID1
      function DoStuff_1(){
      console.log("Doing Stuff 1");
      }
      
      //Function for userID2
      function DoStuff_2(){
      console.log("Doing Stuff 2");
      
      }
      

      【讨论】:

      • 这可能是真的,但是超过 2 个流,您会发现您的流开始断开连接,因此最好在一个流的后续参数中使用一组 ID,而不是尝试使用多个流。
      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-09
      • 1970-01-01
      • 2014-12-29
      • 1970-01-01
      相关资源
      最近更新 更多