From 0d7540ada000653fb7d7877d587a0e05c5734103 Mon Sep 17 00:00:00 2001 From: Harmon Date: Thu, 20 Jun 2019 23:48:12 -0500 Subject: [PATCH] Update API.send_direct_message --- docs/api.rst | 14 ++++++++++---- tweepy/api.py | 22 +++++++++++++++++----- tweepy/binder.py | 2 ++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/docs/api.rst b/docs/api.rst index afefb43..7ebaccc 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -268,14 +268,20 @@ Direct Message Methods :rtype: list of :class:`DirectMessage` objects -.. method:: API.send_direct_message(user/screen_name/user_id, text) +.. method:: API.send_direct_message(recipient_id, text, [quick_reply_type], [attachment_type], [attachment_media_id]) Sends a new direct message to the specified user from the authenticating user. - :param user: The ID or screen name of the recipient user. - :param screen_name: screen name of the recipient user - :param user_id: user id of the recipient user + :param recipient_id: The ID of the user who should receive the direct message. + :param text: The text of your Direct Message. Max length of 10,000 characters. + :param quick_reply_type: The Quick Reply type to present to the user: + + * options - Array of Options objects (20 max). + * text_input - Text Input object. + * location - Location object. + :param attachment_type: The attachment type. Can be media or location. + :param attachment_media_id: A media id to associate with the message. A Direct Message may only reference a single media_id. :rtype: :class:`DirectMessage` object diff --git a/tweepy/api.py b/tweepy/api.py index 9e6149b..cc2e3bd 100644 --- a/tweepy/api.py +++ b/tweepy/api.py @@ -431,17 +431,29 @@ class API(object): require_auth=True ) + def send_direct_message(self, recipient_id, text, quick_reply_type=None, attachment_type=None, attachment_media_id=None): + """ Send a direct message to the specified user from the authenticating user """ + json_payload = {'event': {'type': 'message_create', 'message_create': {'target': {'recipient_id': recipient_id}}}} + json_payload['event']['message_create']['message_data'] = {'text': text} + if quick_reply_type is not None: + json_payload['event']['message_create']['message_data']['quick_reply'] = {'type': quick_reply_type} + if attachment_type is not None and attachment_media_id is not None: + json_payload['event']['message_create']['message_data']['attachment'] = {'type': attachment_type} + json_payload['event']['message_create']['message_data']['attachment']['media'] = {'id': attachment_media_id} + + return self._send_direct_message(json_payload=json_payload) + @property - def send_direct_message(self): - """ :reference: https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-message - :allowed_param:'user', 'screen_name', 'user_id', 'text' + def _send_direct_message(self): + """ :reference: https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-event + :allowed_param:'recipient_id', 'text', 'quick_reply_type', 'attachment_type', attachment_media_id' """ return bind_api( api=self, - path='/direct_messages/new.json', + path='/direct_messages/events/new.json', method='POST', payload_type='direct_message', - allowed_param=['user', 'screen_name', 'user_id', 'text'], + allowed_param=['recipient_id', 'text', 'quick_reply_type', 'attachment_type', 'attachment_media_id'], require_auth=True ) diff --git a/tweepy/binder.py b/tweepy/binder.py index 414bfd9..20f961c 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -44,6 +44,7 @@ def bind_api(**config): raise TweepError('Authentication required!') self.post_data = kwargs.pop('post_data', None) + self.json_payload = kwargs.pop('json_payload', None) self.retry_count = kwargs.pop('retry_count', api.retry_count) self.retry_delay = kwargs.pop('retry_delay', @@ -182,6 +183,7 @@ def bind_api(**config): resp = self.session.request(self.method, full_url, data=self.post_data, + json=self.json_payload, timeout=self.api.timeout, auth=auth, proxies=self.api.proxy) -- 2.25.1