Harrison Cramer
Harrison Cramer

Reputation: 4496

Twitter API, Multiple User ID Stream

I'm working on streaming data using the Node.js and the Twitter API. Here's my code, after I've imported the correct dependencies:

param =  {track: 'syria'}
twitterClient.stream('statuses/filter',param,function(stream) {
  stream.on('data', function(tweet) {
   console.log(tweet.text)
  });

  stream.on('error', function(error) {
    console.log(error);
  });
});

How do I pass in multiple arguments to the 'statuses/filter' search? I want to be able to filter by multiple search terms. I know to do with with the "q" parameter for Twitter's REST API, you just pass them in as a single string, separated by spaces. I've tried passing them in as an array for the STREAM API, as the Twitter documentation says here, but with no luck.

Similarly, I want to be able to track multiple users. Right now, I'm using the "statuses/filter" parameter to narrow my stream down to one user, but I don't know how to pass in multiple user IDs. My code for one user looks like this:

 param =  {follow: '17594077'} // The ID of the account I want

 twitterClient.stream('statuses/filter',param,function(stream) {
   stream.on('data', function(tweet) {
      console.log(`Sent message: `, message.body);
   });

   stream.on('error', function(error) {
     console.log(error);
   });
 });

When I try to pass in multiple values for a Twitter ID, for example, my code looks like this:

param =  {follow: ['17594077','239548513']} // The IDs of the account I want

 twitterClient.stream('statuses/filter',param,function(stream) {
   stream.on('data', function(tweet) {
      console.log(`Sent message: `, message.body);
   });

   stream.on('error', function(error) {
     console.log(error);
   });
 });

This gives me the following error:

Error: Status Code: 401
    at Request.<anonymous> (/Users/harrisoncramer/Desktop/newBot/node_modules/twitter/lib/twitter.js:277:28)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at Request.onRequestResponse (/Users/harrisoncramer/Desktop/newBot/node_modules/request/request.js:1068:10)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:551:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:117:23)
    at TLSSocket.socketOnData (_http_client.js:440:20)
    at emitOne (events.js:116:13)

What am I doing wrong?

Upvotes: 1

Views: 1004

Answers (1)

glortho
glortho

Reputation: 13200

For your multiple user request try:

param = {follow: '17594077,239548513'}

From the api documentation:

follow   optional   A comma separated list of user IDs, 

Upvotes: 1

Related Questions