I'm trying to build a twitter retweeting bot that is listening to another account, but if there are any keywords in the tweet that are part of my exclusion list, it won't retweet those tweets. I'd like for this bot to retweet in real-time and not some prescribed interval(s) (if possible).
I already have my bot's twitter account setup and the Developer account created with a project, app, & API keys.
I'm using:
- Windows 10 (64-bit)
- PyCharm Community Edition 2023.1.1
- Tweepy 4.14.0
I have two script files: config.py that has all my Twitter API keys and bot.py which is the main script.
The python error that I'm getting is:
Traceback (most recent call last):
File "C:\Users\1PXSN22\PycharmProjects\PGSC\bot.py", line 6, in <module>
class RetweetStreamListener(tweepy.StreamListener):
AttributeError: module 'tweepy' has no attribute 'StreamListener'
Process finished with exit code 1
Here is my code:
import tweepy
import config
exclusion_keywords = ["keyword1", "keyword2", "keyword3"] # List of keywords to exclude
class RetweetStreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.user.screen_name == "Example_Account" and not any(keyword in status.text.lower() for keyword in exclusion_keywords):
try:
api.retweet(status.id) # Retweet the tweet
print("Retweeted:", status.text)
except tweepy.TweepError as e:
print("Error:", e.reason)
auth = tweepy.OAuthHandler(config.consumer_key, config.consumer_secret)
auth.set_access_token(config.access_token, config.access_token_secret)
api = tweepy.API(auth)
stream_listener = RetweetStreamListener()
stream = tweepy.Stream(auth=api.auth, listener=stream_listener)
stream.filter(follow=["Example_Account"])
I'm on the latest version of tweepy and I've tried using multiple classes:
class RetweetStreamListener(StreamListener):
then
class RetweetStreamListener(tweepy.Stream):
then
class RetweetStreamListener(tweepy.StreamListener):
then
class RetweetStreamListener():
But none of them seem to work.
Where's my code going wrong?