Update API.send_direct_message
authorHarmon <Harmon758@gmail.com>
Fri, 21 Jun 2019 04:48:12 +0000 (23:48 -0500)
committerHarmon <Harmon758@gmail.com>
Fri, 21 Jun 2019 16:42:32 +0000 (11:42 -0500)
docs/api.rst
tweepy/api.py
tweepy/binder.py

index afefb437d28f66db9f56dfbef527f72f81b44352..7ebaccc0d5919534d13535624864974589c7776f 100644 (file)
@@ -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
 
 
index 9e6149b82985ec89528aaceb554e83ddf0e359a4..cc2e3bde893cf6cd4856d401963b4a8415eb9620 100644 (file)
@@ -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
         )
 
index 414bfd95a1bb97563e8fcfcaf6ce7246a20e31c0..20f961ce507dc1f51b788903e980756f5b505dbf 100644 (file)
@@ -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)