birdy
birdy copied to clipboard
Example of post request with JSON parameters?
In the POST example for a static update, the payload consists of simple key-value pairs:
status='Hello @pybirdy!'
I've successfully created a direct message using the deprecated direct_messages/new endpoint:
response = self.client.api.direct_messages.new.post(screen_name='cherdt', text='hello there')
I'm unclear how to accomplish the same using the direct_messages/events/new endpoint (https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event).
I've tried creating a string representing the JSON, but I've been getting the following error:
birdy.twitter.TwitterApiError: Unable to decode JSON response.
Here's an example request I tried that produced the error:
response = self.client.api.direct_messages.events.new.post(event='{"type": "message_create", "message_create": {"target": {"recipient_id": "6039192"}, "message_data": {"text": "hi there"}}}')
Passing parameters as JSON is something new to Twitter API, as it wasn't an option when birdy
was written and it looks like we can't currently handle this use case.
I have a general idea how to handle this and should have a new version out in a day or two, time permitting.
Thanks for bringing this to my attention, haven't really worked with twitter API in a while, so this change went unnoticed.
@cherdt can you check out this branch (features/json-params) and let me know if it works. I don't have time to test this fully, but a quick test with requestb.in indicates that it should.
For API methods that require JSON params, the syntax is:
params = {
"type": "message_create",
"message_create": {
"target": {
"recipient_id": "6039192"
},
"message_data": {
"text": "hi there"
}
}
}
response = self.client.api.direct_messages.events.new.post(params)
You just pass a dict
to post() and birdy
will handle the rest.
If all is OK, i'll release a new version ASAP.