Going a bit insane here…

I’m modifying my bot @songfinder_bot to use the V2 endpoints finally, but in my testing, despite what I’ve tried, the stream is not returning the correct tweets.

Checking my filtered stream rules only shows I have one rule, as it should be:

$ curl -X GET 'https://api.twitter.com/2/tweets/search/stream/rules' -H "Content-type: application/json" -H "Authorization: Bearer $BEARER_TOKEN"

{"data":[{"id":"1546763511157510145","value":"@songfinder_bot"}],"meta":{"sent":"2022-07-12T08:02:34.275Z","result_count":1}}

However when I initiate a stream with the EXACT same Bearer token, right after checking that my rules are correct, it yields irrelevant tweets, e.g:

{'id': '1546768745334439936', 'text': 'RT @AudiOfficial: Like or share: Express yourself with the Audi TT RS Coupé. Like for Python Yellow. Share for Dew Silver. https://t.co/zDV…'}
{'id': '1546768746479374336', 'text': 'RT @CrownedHead06: Things you can become after learning Python 🐍 ... #ML #DataScience #SQL #Cybersecurity #BigData #Analytics #AI #IIoT #Py…'}

Looking at what all the returned results have in common, it seems to be filtering results for python. NOWHERE in my code have I chosen to return that query. The application I’m using is just one I use for testing things, so maybe at some point in time I DID set it to return all tweets with python in them, however it shouldn’t be doing that anymore, as I’ve verified what rules I want to filter for already.

I’ve double-triple-quadruple-checked that I’m using the correct Bearer token, so I’m going a little crazy trying to work out what I’m doing wrong.

So then I stripped down all the code, and I tested the stream using this script, to make 100% sure I’m not doing something wrong. It deletes any existing rules and then sets the EXACT rule I want.

Here is the exact program I ran:

import requests, json

BEARER_TOKEN = ""

headers = {"Authorization": f"Bearer {BEARER_TOKEN}"}

response = requests.get("https://api.twitter.com/2/tweets/search/stream/rules", headers=headers)

current_rules = response.json()
print(current_rules)

if current_rules is None or "data" not in current_rules:
    pass
else:
    ids = list(map(lambda rule: rule["id"], current_rules["data"]))
    payload = {"delete": {"ids": ids}}
    response = requests.post("https://api.twitter.com/2/tweets/search/stream/rules", headers=headers, json=payload)
    print("Deleted existing rules")

sample_rules = [{"value": "@songfinder_bot"}]
payload = {"add": sample_rules}
response = requests.post("https://api.twitter.com/2/tweets/search/stream/rules", headers=headers, json=payload)

response = requests.get("https://api.twitter.com/2/tweets/search/stream", headers=headers, stream=True)
print(response.status_code)

for response_line in response.iter_lines():
    if response_line:
        json_response = json.loads(response_line)
        print(json_response['data'])

But still, I get stupid results like this:

{'id': '1546773459522445315', 'text': 'RT @KirkDBorne: [Download FREE 1213-page PDF eBook] "Learning Python" (4th Edition) at https://t.co/u9KcUcpX50\n————\n#BigData #DataScience #…'}
{'id': '1546773459509952512', 'text': 'RT @JobPreference: NEED a #JOB?\nSign up now https://t.co/o7lVlsCHXv\nFREE. NO MIDDLEMEN\n#Robotics #AIEthics #MachineLearning #AI #Python #Da…'}

What is happening. Have I found some sort of bug? I have no idea if the issue is reproducible with other apps or not. The results I’m getting make absolutely no sense. Have I really done something wrong, and I’m unable to see it? I’m seriously lost… Streaming on v1.1 has no issues for me.

Thanks in advance for any help.

3 Likes

Hi!

I’m pretty much having the same issue for the V2 filtered stream API.

Despite listing and subsequently deleting all rules, my stream returns responses for an old rule (in my case the matching rule has ID 1545438009478217730).
Again, this matching rule is not listed and I get an error saying the rule doesn’t exist when I’m attempting to delete it by ID.
When trying to add new rules, they are properly listed and I can delete them again, but this one rule with ID 1545438009478217730 somehow remains enabled without being listed or deletable.

This completely breaks the endpoint for me because the “invisible” rule it a rather generic one that I used for testing and the filtered stream is flooded with posts (thus also depleting my quota).

Any chance to get help on this or a fix in the near future?

Addendum: Another (recent) topic with the same issue: https://twittercommunity.com/t/bug-deleted-streaming-client-rules-dont-show-up-but-still-make-matches/173932

3 Likes

Sounds like it’s definitely a bug then. Hopefully this gets noticed and someone fixes it.

In the meantime I guess I’m gonna have to stick with v1.1 for a while longer.

1 Like

Same problem with the matching rule, the id provided on ‘on_data’ is not listed in the get_rules’s ids.

1 Like

Hey everyone – We’re aware of this and investigating internally. Sorry for the confusion and inconvenience!

3 Likes

We are actively working on a solution for this. More to come shortly!

3 Likes

We’ve implemented a fix and are monitoring the situation. If you see any further irregularities in this realm, please let me know!

4 Likes

Looks like the fix is working! Thanks to you and the team for the fast work! :smiley:

1 Like