Created and recorded by Vivek Jariwala, May 2022
Music: Call of the Void, by Justin Miles, https://lmms.io/lsp/?action=show&file...
How to use the Twitter API in Python to send a direct message
Twitter is one of the largest social media platforms with 36 million daily active users, and it allows individuals to post messages, interact with content, and communicate with other users. However, if you use Twitter for a business or are trying to grow an audience, it can be incredibly useful to automate some of the processes on Twitter to increase efficiency and productivity. Well thankfully using the Twitter API we can achieve just that. Today, I will show you how to gain access to this API, how to activate it, and then will showcase how to use some of the methods within the API to direct message other users within Python.
To gain access to the Twitter API, you first need to apply for it. First open your internet browser and type in "twitter api" and it should be one of the first few links. When you click sign up, you just need to be signed in to Twitter and provide a reason for why you are signing up for the API. Then go into the developer settings and apply for the "Elevated" access. You will need this to be able to execute the code in this video without an error. However, gaining access to these permissions is essentially automatic and it doesn't take long, so its very easy to get approved.
-- Link: / twitter-api
Now that you have the correct tier of developer access, click here to set up a project. Once you set up a project, you can generate the Access Tokens and API keys. Make sure to copy these codes down because you will need them for your code.
Now you also need to install the Twitter API (known as Tweepy) on your system as well. The easiest method in my opinion is using pip. Since I am using a windows terminal, I will just type in "pip install tweepy" and click enter.
Getting to the program itself, I will now open up PyCharm and you can use whichever IDE you utilize to code in Python, you want to first import the Tweepy API in the top line of your program. Afterwards, you need to input the keys that I mentioned earlier in four variables. The way you do this is standard so you can just copy the syntax I am utilizing here and change the passwords to the ones that were generated for you. What we are doing in these lines is creating a Twitter object called 'auth', and then storing it in a variable called api so that we can use this variable to interact with the object using different methods from the API.
Afterwards, we can now dive into the code. Lets first receive an input from the user to store the Twitter username of the account they want to contact in a variable called username. However, this username is not how the Twitter API interprets account names. Instead it has screen names associated with each Twitter username. Therefore, for the API to get the screen name we use the get_user method.
We also need to ask the user what message they want to write to this Twitter account. However, lets say you're sending to same message to multiple people, you can just set a default message to this variable and not get an input.
Now we need to implement a try except statement for the actual direct message part. The reason for this is that some user's have privacy settings configured in a way that only allows users they are following to message them. If the program tries messaging one of these users, then it will result in an error. In the try statement, we use this statement to send the direct message and pass the ID based on the screen name and the text as arguments. Then we break the statement.
In the except statement, we make an exception for a forbidden error and print a statement explaining why the direct message cannot be sent. We also put a break in this too to prevent the program from looping.
Now lets run the program once. For the user, I'm just going to put a celebrity's Twitter account, so for example, Drake's Twitter username is Drizzy so lets just put that in. Then, lets make the message, "hello drake". Now, the code compiles correctly. We can check that it worked by heading over to Twitter and seeing that it was indeed sent.