Getting started with the Twitter API
Twitter is a web service that provides an application programming interface (API), which means you can write software that communicates with the live Twitter service—perhaps to read tweets in real time or to automatically publish tweets.
The API is free to use, but you have to have a Twitter account and to register your application in order to get access to the API, but that’s easy enough.
Make Your Twitter Developer Account
Once you have a twitter account with phone number linked, it’s time to create a Twitter app. To make a Twitter app first go to the twitters developer site.
- click on “Create an app”. This will take you to an application form, where you have to fill in details. Make sure you fill in real information, and submit the application. It might take a few hours or a couple of days for your application to get approved.
2. After your account is reviewed, you can create an app. Give it a name, and describe your app. Make sure you enter a URL in the field. If you don’t have a website, just add a valid URL.
3.The app will not be created if you leave the URL block empty. Also check the “Enable Sign in with Twitter” option. After the information is filled in, submit form, and you will be redirected to your application.
4.Here you will see three tabs, click on “Permissions” and select “Read, write and direct messages” and save it.
5. Next go to “Keys and tokens” and click on “Create Access Tokens” or “Regenerate Access Tokens”. You will notice some hash codes which we will use in our bot script. These are very important codes! DO NOT SHARE these with anyone, or they can gain access to your Twitter account!
Setting Up the Raspberry Pi
- With our API tokens handy we can now start setting up and writing our Raspberry Pi Twitter bot. The first thing we should do is make sure our Raspberry Pi is up to date by running the following commands on it.
2. With the Raspberry Pi now updated let’s install the tools we will need to set up our Raspberry Pi Twitter Bot. We can utilize the Python Library twython or tweepy to interact with Twitter’s API. Here we have made use of python library Tweepy. Visit Here to set up using twython.
To install Tweepy Python Library for twitter API we use pip command
Create a Twitter Bot with the Raspberry Pi
1.We start by importing the packages required for the code. The package, tweepy, is imported as tp which will be used to connect and control our Twitter account . OS and time are preinstalled packages.
2.Next, we have to add the credentials used to log in into our Twitter account. These credentials are nothing but tokens/keys we generate using the Twitter app.
In place of the hash you have to enter your keys. Replace the 'your-xxxx'
with your account credentials. We saved the keys into variables so we can use them later to authenticate the Twitter login.
Now we have to authenticate our credentials so our bot can login to our account.
3.We use the authhandler function to validate the tokens. Next, we save the validation in a variable called, api. This is used for actions such tweeting/retweeting, liking, etc. Now we can start using the API to interact with our account.
4.We can use the me()
function to fetch user data and print it to the terminal. The code, user.name
and user.location
, prints our username and location. Use this code to verify that the script is working, and the connection is established.
Example 1 : Write some code that will follow everyone that is following you
Example 2: Find Keywords & HashTags Using Twitter Bot
Let’s examine this code further in a quick code walk-through.
First we save some keywords. These are the words you want to look for in a tweet, here we used arduino, raspberrypi, esp8266 and learnrobotics. You can use any words you want. Tweepy will search for tweets with these words. In the next line we have a for loop where we search the tag in the function Cursor(). The result_type can either be “popular”, “recent” or “mixes”. The phrase lang=”en” will only search for tweets that are in English.
Furthermore, let’s look at the code, .items(). Here the item is empty but you can provide a parameter. (For example item(10)). The bot will only select 10 tweets with this code. If you leave it empty, it will make the search longer, and all the tweets with these keywords will be selected.
There is another try block inside the for loop. First, the tweet is “favorited” using the favorite()
function. Then the retweet()
function retweets the tweet on your account. Finally, sleep(60)
pauses the loop for 60 seconds before selecting next tweet. You can increase or decrease the time in seconds. I recommend using a delay greater than 60 seconds because Twitter might disable your account for spamming.
Finally, there is an except block. This block will throw an exception or warning if there is an error with the tweets. That’s all for the code, now you can save the file as tweetbot.py
and run the code through terminal using the following command:
To stop the execution, just close the terminal window. Add a copy of code, above, that you can use as a starting point. Just enter your tokens/keys, and you are good to go.