Code: Connect your Twitter Bot to Python with Twurl
Last year, at the beginning of the global vaccination campaign, we wrote a ‘good bot’ for Twitter with my dear friend Kayra. Our vaccination bot basically produced (and still produces to this date) informational graphs on a daily basis about how the vaccination is going in Turkey. To do that, we regularly get the data from the Turkish Health Ministry by scraping it, put it in our database in a remote server, get that to Python, and create daily images/figures/videos like this or this. The great thing is: This whole process is automated! In fact, the last error message was more than 6 months ago and that was because the ministry had changed their website! So it’s going pretty good - though people lost their interest in tracking vaccination.
In this post I’m gonna talk about how to create a bot account from Twitter.
First, go to the Twitter developer account and sign up, if you haven’t already. I mentioned this process in my previous post here. You can find more guidelines here. Then, get all your tokens from here and paste them somewhere. You’re gonna need them.
Then - go to Twitter. Create your bot account.
Now you have got two things: On the back, you have a developer account. On the front, you have the Twitter account. How to make the connection between the two?
The logic is as follows: You will need to create an app from a developer account. Then, going back to our bot account, you’ll need to give permission to access and write to that app we just created by our developer account. This app will be posting the tweets since you (bot account) give the permission.
The reasoning sounds good. But how to do that connection actually?
I’m not going to dive into the last details here - but here is a general guide.
First, you need to set up twurl. This is the tool which will grant an access token to a client application for a specified user (in this case, for our bot account).
To set up twurl, you first need to set up ruby. Now ruby’s installation can be completed sometimes. The best explanation that I was able to find is here.
After setting up Ruby and twurl, we open the terminal (if you’re a mac user, I would suggest using iterm2), then enter the consumer key and secret key. Remember we had already copied that from our developer account on Twitter.
Then, go back to the terminal, enter the following code. Remember to change CONSUMERKEY with your actual consumer key (something like TvtKoGyjEfFMLrcyo20btvvjZ) and CONSUMERSECRET with your actual consumer secret (this is a bit longer).
twurl authorize --consumer-key CONSUMERKEY --consumer-secret
CONSUMERSECRET
This code is gonna produce a link. Copy that, and paste it in a browser where your bot account is already signed in. Basically we are introducing the app that we created on Twitter developer platform to the bot account we want to use. (So you can do this to any account actually. This way, taccount will be giving you the access to write tweets on behalf of them.). After pasting it, the link is gonna give me a PIN number. Copy that, paste it back in the terminal.
Then you should see “Authorization successful”.
Then we write the following code to the terminal:
nano ~/.twurlrc
This will give us all the Twitter accounts that our app has access to.
Then, copy TOKEN
and SECRET
info from here, paste them in
relevant pieces of your code on Python where you were asked token and
secret keys when using Tweepy.
This post was originally written in April 18, 2021. Last updated: March 13, 2022.