Open In App

Python – API.destroy_direct_message() in Tweepy

Improve
Improve
Like Article
Like
Save
Share
Report

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 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)
  
# direct message ID
id = print("Before using the destroy_direct_message() object")
if api.get_direct_message(id):
    print("This direct message exists.")
  
# deleting the direct message
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.




# invalid direct message ID
id = 12345
  
# deleting the direct message
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.'}]


Last Updated : 12 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads