【发布时间】: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");
}
任何建议都非常感谢!
【问题讨论】: