Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – DirectMessage object in Tweepy

  • Last Updated : 18 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.

DirectMessage

The DirectMessage object in Tweepy module contains the information about a place.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Here are the list of attributes in the DirectMessage object :



  • type : The type of the direct message.
  • id : The ID of the direct message.
  • created_timestamp : The timestamp when the direct message was created.
  • message_create : The details of the contents of the direct message like sender’s ID, text, media etc.

Example : Use get_direct_message() method to fetch the direct message.




# 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 direct message 
id = 1271013844639313927
    
# fetching the direct message 
direct_message = api.get_direct_message(id
  
# printing the information
print("The type is : " + direct_message.type)
print("The id is : " + direct_message.id)
print("The created_timestamp is : " + direct_message.created_timestamp)
  
# inside message_create
print("The recipient_id is : " + direct_message.message_create['target']['recipient_id'])
print("The sender_id is : " + direct_message.message_create['sender_id'])
print("The source_app_id is : " + direct_message.message_create['source_app_id'])
print("The text is : " + str(direct_message.message_create['message_data']['text']))
print("The entities are : " + str(direct_message.message_create['message_data']['entities']))
print("The media attachment is : " + str(direct_message.message_create['message_data']['attachment']))

Output :

The type is : message_create
The id is : 1271013844639313927
The created_timestamp is : 1591868289514
The recipient_id is : 2344960135
The sender_id is : 4801790172
The source_app_id is : 3033300
The text is : https://t.co/wn8dK13pzf
The entities are : {‘hashtags’: [], ‘symbols’: [], ‘user_mentions’: [], ‘urls’: [{‘url’: ‘https://t.co/wn8dK13pzf’, ‘expanded_url’: ‘https://twitter.com/messages/media/1271013844639313927’, ‘display_url’: ‘pic.twitter.com/wn8dK13pzf’, ‘indices’: [1, 24]}]}
The media attachment is : {‘type’: ‘media’, ‘media’: {‘id’: 1271013835181154304, ‘id_str’: ‘1271013835181154304’, ‘indices’: [1, 24], ‘media_url’: ‘https://ton.twitter.com/1.1/ton/data/dm/1271013844639313927/1271013835181154304/gIf1lwJ9.png’, ‘media_url_https’: ‘https://ton.twitter.com/1.1/ton/data/dm/1271013844639313927/1271013835181154304/gIf1lwJ9.png’, ‘url’: ‘https://t.co/wn8dK13pzf’, ‘display_url’: ‘pic.twitter.com/wn8dK13pzf’, ‘expanded_url’: ‘https://twitter.com/messages/media/1271013844639313927’, ‘type’: ‘photo’, ‘sizes’: {‘small’: {‘w’: 225, ‘h’: 225, ‘resize’: ‘fit’}, ‘medium’: {‘w’: 225, ‘h’: 225, ‘resize’: ‘fit’}, ‘thumb’: {‘w’: 150, ‘h’: 150, ‘resize’: ‘crop’}, ‘large’: {‘w’: 225, ‘h’: 225, ‘resize’: ‘fit’}}}}




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!