0

I've been running the script below using tweepy, but the on_direct_message() is never called. I'd like to use this function so I can receive new direct messages. I've used tweepy for the past month without any issue until now. There seem to be others out there will a similar issue: Tweepy streaming: on_direct_message() is never called

I'm on a Mac OS X 10.10.5 and I'm using Python 2.7.

Any help would be really appreciated.

class MyStreamListener(tweepy.StreamListener):

    def on_direct_message(self, status):

        print "status: "
        print status

myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener = MyStreamListener(), timeout = None, retry_count = None)
myStream.filter(track=["filter"], async=False)
Community
  • 1
  • 1
byron
  • 13
  • 2
  • Maybe have a look [here](http://stackoverflow.com/questions/31116213/streaming-twitter-direct-messages), it seems that a solution to stream direct messages has been found. – Efferalgan Aug 26 '16 at 14:00
  • @Efferalgan, thanks for the feedback. I saw that post previously and regenerated my access token and secret accordingly. I decided to test their code and I noticed that 'print(status, flush = True)' raises a SyntaxError. Have you had any experience with this flush parameter? I haven't seen it elsewhere in tweepy. – byron Aug 28 '16 at 08:56
  • The "flush" argument has been added in Python 3.3, according to the [documentation](https://docs.python.org/3.3/library/functions.html#print). Since you are using Python 2.7, you will have to remove it. From what I have understood from [this question](http://stackoverflow.com/questions/15608229/what-does-prints-flush-do), it seems that you can either go for `print(status)` or `print(status+"\n")`, without much consequence. Or, as it is only a `print` statement, you can remove it and the program will be the exact same. – Efferalgan Aug 28 '16 at 13:26
  • @Efferalgan, thanks again. That is useful for sure. I removed the flush argument. However, on_direct_message() did not work. On the plus-side I found that on_data() might provide me with the information I'm hoping to receive. If on_direct_message() were to work, it would not provide more data than on_data(), correct? – byron Aug 30 '16 at 09:43
  • I'm not sure to understand what you mean. The answer to the question I linked explains that on_direct_message() did not work and that everything arrived on on_data(), which grabs both tweets and messages. If on_direct_message() worked, it would get only messages and on_data() would only get tweets, but the averall quantity of data you get would be the exact same, just split between these two functions. – Efferalgan Aug 30 '16 at 09:51

0 Answers0