Python – API.send_direct_message() in Tweepy
Last Updated :
12 Jun, 2020
Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user. These keys will help the API for authentication.
API.send_direct_message()
The send_direct_message()
method of the API
class in Tweepy module is used to send a direct message as the authenticated user.
Syntax : API.send_direct_message(parameters)
Parameters :
- recipient_id : ID of the recipient.
- text : text of the direct message.
- attachment_type : attachment media or location to be sent.
- attachment_media_id : ID of the attachment media to be sent.
Returns : an object of class DirectMessage
Example 1 :
import tweepy
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
recipient_id =
text = "This is a Direct Message."
direct_message = api.send_direct_message(recipient_id, text)
print (direct_message.message_create[ 'message_data' ][ 'text' ])
|
Output :
This is a Direct Message.
Example 2 : Sending a direct message with the following media attachment :
recipient_id =
text = "This is a Direct Message."
attachment_media_id =
direct_messages = api.send_direct_message(recipient_id, text,
attachment_media_id = attachment_media_id)
|
Output :
Share your thoughts in the comments
Please Login to comment...