Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide support for filtered streams #80

Open
iAdityaEmpire opened this issue May 6, 2021 · 3 comments
Open

Provide support for filtered streams #80

iAdityaEmpire opened this issue May 6, 2021 · 3 comments

Comments

@iAdityaEmpire
Copy link

Not sure if this is already implemented, but adding support for creating filtered streams with rules will be great.

@wiertz
Copy link

wiertz commented May 8, 2021

Filtered streams are supported, see Readme https://github.com/HunterLarco/twitter-v2#streaming-api. However, twitter-v2 does not manage the filter rules – is that what you mean? Given that rules are independent of an active stream (i.e. they persist even if there is no active stream and they can be modified while the stream is running) it does not seem useful to include them as a property of the stream. However, writing your own utility function to set and update rules is rather simple, given that it only takes a simple post request:

curl -X POST 'https://api.twitter.com/2/tweets/search/stream/rules' \ -H "Content-type: application/json" \ -H "Authorization: Bearer $BEARER_TOKEN" -d \ '{ "add": [ {"value": "cat has:images", "tag": "cats with images"} ] }'

(Example taken from https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/quick-start)

@Arnique
Copy link

Arnique commented May 10, 2021

I was also stuck. Turns out you just use client.post() to add the filters as per official the docs.

Check out code sample here: #82

@gustawdaniel
Copy link

Try:

const Twitter = require('twitter-v2');
const client = new Twitter({ bearer_token: process.env.TWITTER_API_BEARER_TOKEN });
client.post('tweets/search/stream/rules', { add: [ {value: 'cat has:images'} ] }).then(console.log);

You should see

 {
  data: [ { value: 'cat has:images', id: '1407767983888470021' } ],
  meta: {
    sent: '2021-06-23T18:30:17.966Z',
    summary: { created: 1, not_created: 0, valid: 1, invalid: 0 }
  }
}

Then you can get your rules:

> client.get('tweets/search/stream/rules').then(console.log);
Promise { <pending> }
> {
  data: [
    {
      id: '1407650287389712386',
      value: 'from:PreciseLabPL OR from:elonmusk'
    },
    { id: '1407767983888470021', value: 'cat has:images' }
  ],
  meta: { sent: '2021-06-23T18:30:51.091Z' }
}

or remove them

> client.post('tweets/search/stream/rules', { delete: {ids: ['1407767983888470021']} }).then(console.log);
Promise { <pending> }
> {
  meta: {
    sent: '2021-06-23T18:33:00.049Z',
    summary: { deleted: 1, not_deleted: 0 }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants