heroZero
heroZero

Reputation: 173

Issues with 'Follow' on Twitter API

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!

Upvotes: 0

Views: 130

Answers (2)

prospector
prospector

Reputation: 3469

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:

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

Upvotes: 1

heroZero
heroZero

Reputation: 173

By establishing a separate stream for the Second user account it seems to work:

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

}

Upvotes: 0

Related Questions