Twitter是一种流行的社交网络,用户在其中共享称为推文的消息。 Twitter允许我们使用Twitter API或Tweepy挖掘任何用户的数据。数据将是从用户提取的推文。首先要做的是从每个用户都可以轻松获得Twitter开发人员的使用者 key ,使用者 key ,访问 key 和访问 key 。这些 key 将帮助API进行身份验证。
API.send_direct_message()
的send_direct_message()方法APITweepy模块中的class用于以经过身份验证的用户身份发送直接消息。
用法:API.send_direct_message(parameters)
参数:
- recipient_id:收件人的ID。
 - text:直接消息的文本。
 - attachment_type:附件媒体或要发送的位置。
 - attachment_media_id:要发送的附件媒体的ID。
 
返回值:DirectMessage类的对象
范例1:
# import the module 
import tweepy 
  
# assign the values accordingly 
consumer_key = "" 
consumer_secret = "" 
access_token = "" 
access_token_secret = "" 
  
# authorization of consumer key and consumer secret 
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
  
# set access to user's access key and access secret  
auth.set_access_token(access_token, access_token_secret) 
  
# calling the api  
api = tweepy.API(auth) 
  
# ID of the recipient 
recipient_id = 
  
# text to be sent 
text = "This is a Direct Message."
  
# sending the direct message 
direct_message = api.send_direct_message(recipient_id, text) 
  
# printing the text of the sent direct message 
print(direct_message.message_create['message_data']['text'])输出:
This is a Direct Message.
范例2:发送带有以下媒体附件的直接消息:

# ID of the recipient 
recipient_id = 
  
# text to be sent 
text = "This is a Direct Message."
  
# the ID of the media 
attachment_media_id = 
  
# sending the direct message 
direct_messages = api.send_direct_message(recipient_id, text, 
                    attachment_media_id = attachment_media_id)输出:

相关用法
- Python Tweepy API.create_friendship()用法及代码示例
 - Python Tweepy API.get_user()用法及代码示例
 - Python Tweepy API.me()用法及代码示例
 - Python Tweepy API.lookup_users()用法及代码示例
 - Python Tweepy API.home_timeline()用法及代码示例
 - Python Tweepy API.statuses_lookup()用法及代码示例
 - Python Tweepy API.user_timeline()用法及代码示例
 - Python Tweepy API.retweets_of_me()用法及代码示例
 - Python Tweepy API.mentions_timeline()用法及代码示例
 - Python Tweepy API.update_status()用法及代码示例
 - Python Tweepy API.search_users()用法及代码示例
 - Python Tweepy API.followers()用法及代码示例
 - Python Tweepy API.friends()用法及代码示例
 - Python Tweepy API.get_status()用法及代码示例
 - Python Tweepy API.media_upload()用法及代码示例
 - Python Tweepy API.retweet()用法及代码示例
 - Python Tweepy API.unretweet()用法及代码示例
 - Python Tweepy API.retweets()用法及代码示例
 - Python Tweepy API.destroy_status()用法及代码示例
 
注:本文由纯净天空筛选整理自Yash_R大神的英文原创作品 Python – API.send_direct_message() in Tweepy。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
