DM response differs from doc

restapi
directmessages

#1

Hi,
I am using this https://api.twitter.com/1.1/direct_messages/events/new.json endpoint to publish direct messages with media.
For this endpoint,I am getting response like this

 {
	"event": {
		"id": "867703375537684483",
		"message_create": {
			"message_data": {
				"text": "gud afnun https://t.co/yNV09vS3ui",
				"entities": {
					"symbols": [],
					"urls": [{
						"expanded_url": "https://twitter.com/messages/media/867703375537684483",
						"indices": [10, 33],
						"display_url": "pic.twitter.com/yNV09vS3ui",
						"url": "https://t.co/yNV09vS3ui"
					}],
					"hashtags": [],
					"user_mentions": []
				}
			},
			"sender_id": "4211191093",
			"target": {
				"recipient_id": "4211191093"
			}
		},
		"created_timestamp": "1495711580534",
		"type": "message_create"
	}
}

Which differs from the response given in twitter documentation,

{
	"event": {
		"type": "message_create",
		"id": "1234858585",
		"created_timestamp": "1392078023507",
		"message_create": {
			"target": {
				"recipient_id": "844385345234"
			},
			"sender_id": "1241124",
			"message_data": {
				"text": "Hello World!",
				"entities": {
					"hashtags": [],
					"symbols": [],
					"urls": [],
					"user_mentions": []
				},
				"quick_reply": { ...
				},
				"attachment": {
					"type": "media",
					"media": { ...
					}
				}
			}
		}
	}
}

Can anyone please help me in this?

Thanks!


#2

I’m not sure I see the problem. The elements are mostly the same, the message_data contains different things because of the data that was sent in the docs example compared to what you’re sending but all the outer elements are there.

Are you encountering a specific problem or can you ask a more direct question about one of the elements?


#3

Not getting attachment element now.But I received that previously in the response.


#4

Well, it’s a beta feature so I would expect it to change. I generally do sanity checks to make sure data exists before trying to use it. I’m guessing they’re experimenting with removing empty fields to trim down the payload. What language do you program in? I use php and for handling these requests (which is probably why I didn’t notice) I would run something such as:

if (!empty($payload['attachments'])) { // loop only if it exists }


#5

I got this response when I try to attach media to direct message.attachment element is not an empty field in this case.I don’t think so it’s because of trimming empty fields.


#6

Please share your code. I’m trying to send directs with the new entity: https://api.twitter.com/1.1/direct_messages/events/new.json. But doesn’t work for me.