Python – API.destroy_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.destroy_direct_message()
The destroy_direct_message()
method of the API
class in Tweepy module is used to delete a direct message.
Syntax : API.destroy_direct_message(id)
Parameters :
- id : ID of the direct message to be destroyed.
Returns : None
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)
id = print ( "Before using the destroy_direct_message() object" )
if api.get_direct_message( id ):
print ( "This direct message exists." )
api.destroy_direct_message( id )
print ( "\nAfter using the destroy_direct_message() object" )
try :
api.get_direct_message( id )
except :
print ( "This direct message no longer exists." )
|
Output :
Before using the destroy_direct_message() object
This direct message exists.
After using the destroy_direct_message() object
This direct message no longer exists.
Example 2 : Trying to delete a direct message which does not exists will raise an exception.
id = 12345
api.destroy_direct_message( id )
|
Output :
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]
Share your thoughts in the comments
Please Login to comment...