How do I stream direct messages?

directmessages
streaming
python

#1

Hello!

I’m currently using Twython to do some streaming.

The documentation on this in the twitter docs is very sparse The only things I can find are https://dev.twitter.com/streaming/reference/get/user and https://dev.twitter.com/streaming/overview/messages-types#Direct_Messages (which pretty much just redirects me to here: https://dev.twitter.com/rest/reference/get/direct_messages/show for the REST API).

Most of the forum conversations reference something like [node: 4088] which is cryptic or possibly an error or some kind?

In general, how do I stream direct messages? Would anyone be willing to post an example, or some documentation, or a solution they came up with to do this?

Thanks


#2

Ok found it. I’m leaving this here in case anyone else uses Twython and is starting from zero:

`class MyStreamer(TwythonStreamer):
	def on_success(self, data):
		if 'direct_message' in data:
			msg = data['direct_message']
			print(msg['text'].encode('utf-8'))

	def on_error(self, status_code, data):
		print(status_code, data)`

I had no idea it was just an if statement and some kind of nested dictionary. The docs are sometimes hard to figure out here.


#3

Hey @nedrybot - thanks for your post.

I’m sorry that you’ve found the documentation difficult to use in this case. I’m going to see if I can clarify, based on your feedback. Essentially, as you’ve found, you get DMs in the user stream, and the point of the doc was to link back to the REST API where the structure of a DM is defined. It isn’t entirely clear from the docs though, so I apologise for that.

Ahhh - sorry about that - it’s a leftover from an older version of our developer forums, which should have been converted to a link. Unfortunately I don’t know which topic node: 4088 refers to anymore, but either way, it appears to have been from a much earlier version of the API.

Finally - thank you so much for sharing your solution! That is sure to be useful for others in the future! :thumbsup: