Giter Site home page Giter Site logo

Comments (3)

wiertz avatar wiertz commented on August 23, 2024 2

The example below illustrates how to use url parameters to modify the payload of tweets. See the official twitter docs for further information on fields, expansions and media fields. If you would like to create a filter, also see the official docs as creating, modifying and deleting filter rules happens independent from the stream itself.

const Twitter = require('twitter-v2')

const endpointParameters = {
    'tweet.fields': [ 'author_id', 'conversation_id' ],
    'expansions': [ 'author_id', 'referenced_tweets.id' ],
    'media.fields': [ 'url' ]
}

const client = new Twitter({ bearer_token: process.env.TWITTER_BEARER_TOKEN })
const filteredStream = client.stream('tweets/search/stream', endpointParameters)

from twitter-v2.

Arnique avatar Arnique commented on August 23, 2024 1

Ok I figured it out. You first use the convenience methods to make the calls described by twitter docs e.g to add rules, then request the stream.

    // Define rule as per twitter docs
    const body = {
      "add": [
        {"value": "from:WhaleTrades", "tag": "from whale trades"}
      ]
    }

    // Add above rule
    const r = await client.post("tweets/search/stream/rules", body);
    console.log(r);

    // Define params
    const streamParams = {
      'tweet.fields': [ 'author_id', 'conversation_id' ],
      'expansions': [ 'author_id', 'referenced_tweets.id' ],
      'media.fields': [ 'url' ]
    }
    
   // Request stream
    const stream = client.stream("tweets/search/stream", streamParams);

    for await (const { data } of stream) {
      console.log(data);
    }

from twitter-v2.

Arnique avatar Arnique commented on August 23, 2024

The example below illustrates how to use url parameters to modify the payload of tweets. See the official twitter docs for further information on fields, expansions and media fields. If you would like to create a filter, also see the official docs as creating, modifying and deleting filter rules happens independent from the stream itself.

const Twitter = require('twitter-v2')

const endpointParameters = {
    'tweet.fields': [ 'author_id', 'conversation_id' ],
    'expansions': [ 'author_id', 'referenced_tweets.id' ],
    'media.fields': [ 'url' ]
}

const client = new Twitter({ bearer_token: process.env.TWITTER_BEARER_TOKEN })
const filteredStream = client.stream('tweets/search/stream', endpointParameters)

Thank you for responding.

Another question though: In the docs it says you have to add rules before making a request for filtered stream. e.g

{
  "add": [
    {"value": "cat has:images", "tag": "cats with images"}
  ]
}

How do we do that with with this method?

from twitter-v2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.