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:

sudo apt-get install python-pip -y

Step 1. Get the Github Project

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

git clone https://github.com/rhiever/TwitterFollowBot.git
cd TwitterFollowBot
sudo pip install twitter

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

sudo python setup.py install
sudo make

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.

mkdir FollowBot && cd FollowBot
nano bot.py

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

#!/usr/bin/python/

from TwitterFollowBot import TwitterBot

#Creating your bot
my_bot = TwitterBot()

# Updating followers list in followers.txt (DO NOT DO OFTEN)
my_bot.sync_follows()

After exiting editor make it executable:

chmod +x bot.py

4. Setup Config File

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

nano config.txt

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:

python bot.py

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:

cat followers.txt

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.

#!/usr/bin/python/

from TwitterFollowBot import TwitterBot

#Creating your bot
my_bot = TwitterBot()

result = my_bot.search_tweets("Raspberry Pi", count=3)
for tweet in result["statuses"]:
	print 15 * '-', '\n'
	print "USERNAME: ", tweet["user"]["screen_name"]
	print "TWEET: ", tweet["text"]
	print 15 * '-', '\n'

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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.