Python – API.list_direct_messages() 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.list_direct_messages()
The list_direct_messages()
method of the API
class in Tweepy module is used to fetch all the direct messages within the last 30 days the authenticated user.
Syntax : API.list_direct_messages(count)
Parameters :
- count : number of direct messages to be fetched.
- full_text : boolean indicating whether or not the full text of a message should be returned or not.
Returns : list of objects 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)
direct_messages = api.list_direct_messages()
for direct_message in direct_messages:
print (direct_message.created_timestamp)
|
Output :
1591868314610
1591868289514
1591868232626
1591867665376
1591867346546
1591867323014
Example 2 : Counting the number of direct messages in the last 30 days.
direct_messages = api.list_direct_messages()
print ( len (direct_messages))
|
Output :
6
Share your thoughts in the comments
Please Login to comment...