babusi
babusi

Reputation: 520

Tweepy reply to tweet in stream

I have a twitter bot that responds to tweets containing a hashtag. I've been using the twt = api.search(q='param') to pull tweets and it's been working perfectly but I've since switched to myStreamListener = MyStreamListener() twt = tweepy.Stream(auth = api.auth, listener=myStreamListener()) to pull tweets in real time. this isn't working though and I don't know why. Here's my code:

myStreamListener = MyStreamListener()
twt = tweepy.Stream(auth = api.auth, listener=myStreamListener())

twt.filter(track=['#www'], async=True)

#list of specific strings we want to omit from responses
badWords = ['xxx','yyy' ]

#list of specific strings I want to check for in tweets and reply to

genericparam = ['@zzz']

def does_contain_words(tweet, wordsToCheck):
    for word in wordsToCheck:
        if word in tweet:
            return True
    return False

for currentTweet in twt:
    #if the tweet contains a good word and doesn't contain a bad word
    if does_contain_words(currentTweet.text, genericparam) and not does_contain_words(currentTweet.text, badWords):
        #reply to tweet
        screen = currentTweet.user.screen_name
        message = "@%s this is a reply" % (screen)
        currentTweet = api.update_status(message, currentTweet.id)

Upvotes: 2

Views: 1650

Answers (2)

rtn
rtn

Reputation: 2034

Just stumbled across this.

When you create myStreamListener = MyStreamListener(self.api) self.api instance you want to use needs to be passed to StreamListner constructor or it will be null.

Upvotes: 1

Jerry Yates
Jerry Yates

Reputation: 36

I believe your problem is you're trying to send the reply through the stream instead of using an http request in a separate thread. It's not possible to do this as explained here:

https://dev.twitter.com/streaming/overview

Upvotes: 2

Related Questions