: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
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
)
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',
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)