Twitter Bot Raspberry Pi Tutorial in Python

The Twitter Bot Raspberry Pi Tutorial in Python will show you the simple steps necessary to create your ownTwitter Bot! We are going to use the project by RHiever found here. You will need install TwitterFollowBot. You can get pip by running the command:

Step 1. Get the Github Project

Start off by cloning the project into your /home/pi/ directory.

Next we can run the setup (I am currently in /home/pi/TwitterFollowBot/) with the command:

That should take a few minutes to install all of the dependencies.

Step 2. Twittter Application

We need to setup our Twitter Application. I am using the Twitter account @RasberryPiTuts. A few notes:

  • You will need to use a phone number associated with your twitter account to register the application.
  • There are rate limits for the API (more on this later)

Head to https://apps.twitter.com/ to create your application. After entering the information go to the Keys and Access Tokens tab.TwitterFollowBotApplication

Make sure the App Permissions are set to read and write and then generate an access token. From this we will need the following:

  • Access Token
  • Access Secret
  • Consumer Key
  • Consumer Secret

 3. Creating the Twitter Bot

Head to the directory /home/pi/FollowBot/and create a bot.py file.

I am going to use the following python code to test out the bot.

After exiting editor make it executable:

4. Setup Config File

Once you have that information go back to your TwitterFollowBot Directory to edit the /home/pi/FollowBot/config.txt file.

And enter your information from above. Then hit CTRL-X, press y, then enter to exit the nano editor. NOTE: You do not need parentheses around your tokens and secrets.

OAUTH_TOKEN:<Access Token>
OAUTH_SECRET:<Access Secret>
CONSUMER_KEY:<Consumer Key>
CONSUMER_SECRET:<Consumer Secret>
TWITTER_HANDLE:RasberryPiTuts
ALREADY_FOLLOWED_FILE:already-followed.txt
FOLLOWERS_FILE:followers.txt
FOLLOWS_FILE:following.txt
USERS_KEEP_FOLLOWING:
USERS_KEEP_UNMUTED:
USERS_KEEP_MUTED:
FOLLOW_BACKOFF_MIN_SECONDS:10
FOLLOW_BACKOFF_MAX_SECONDS:60

NOTE: The last two bolded configurations have been added in the repository so that your application does not get banned by Twitter. Use at your own discretion! 

 

5. Testing the Bot

Run the once bot with:

A few files text have been created automatically. They will update each time you run the sync_follows() method. You should now be able to run:

to see the user ID’s of all of the people that follow you.

Optional – Bot Configurations

Now I am going to test out the search_tweets() method changing my bot.py file.

This will show you the username and tweet posted by the most recent people who used the term “Rasbperry Pi”. There are tons of options for changing and automating this bot. Take a look at the Twitter API Rate Limit so you don’t send too many HTTP requests.

Post any links to bots you’ve made or comment questions and suggestions below!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *