I am working on a project where I want to follow two separate twitter accounts, and for a specific function to be called when each account tweets. I am using the Twitter API, Node.js and a NPM Module called Twit.
I have it working no problem when one account tweets but not for both of them:
I believe my issue may be here:
var stream = T.stream('statuses/filter', { follow: ( userID1 , userID2 ) });
If I have follow set to just one User it works fine, however with two it will only work with one. Furthermore, it only works with the second in the list so if its: userID1, user ID2
only userID2 will work. If its userID2, userID1
only userID1 will work.
Full code/logic here:
//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");
}
Any suggestions greatly appreciated!
2
Answers
By establishing a separate stream for the Second user account it seems to work:
You can do it all with some stream; simply make an array with the user ids and join them in the follow parameter like below: