NOTE: This post was sitting unpublished for almost exactly 1 year. I went ahead and gave it database storage and implemented scheduled posting You can find the tweetbot on GitHub and I even have a working version that is deamonized.
I’ve wanted to build a Twitter bot for some time. Mostly just something to send the occasional tweet. That could easily be extended to something that would become a scheduled tweet bot and a database could even be added to store future tweets.
I also wanted to monitor for mentions and notify me of them. Watching for something to occur and then running an action could also be extended in many ways, especially if a live search stream were to be added to the mix.
The basics of what the bot does is relatively simple. It needs to be able to access various streams (my notifications, a search stream). It has to be able to parse them and invoke something based on a given result. It needs to be capable of posting a tweet from my account.
Since I plan on using my Raspberry Pi for this and Python is a popular language to use on it I looked around for some reference points. There’s a very nice Python library written that is capable of doing the heavy lifting of sending requests to the Twitter API for me. It’s called Tweepy and I found it through GitHub.
Using Tweepy I should be able to easily connect and post/get to the Twitter API. Let’s see how that goes.
You will need to create an app and get some access credentials from Twitter to make your API calls – especially since the plan is to make it actually post to accounts.
Installing Tweepy
First I need to install Tweepy. You can run pip install tweepy
to do it – and I did on my laptop and that worked just fine. On my RPi though I will be cloning it from Github and installing manually. There are certain base level dependencies of Tweepy, or of it’s dependencies, that are probably already installed on most systems. They were not available on my Pi though and the setup.py
script doesn’t handle those. A quick Google of the problem told me to run pip install --upgrade pip
to get them. That worked.
git clone https://github.com/tweepy/tweepy.git cd tweepy sudo python setup.py install
Since I also plan to eventually use a database to store things in I also installed mysql-server
but that’s not absolutly necessary for right now.
sudo apt-get install mysql-server
Writing the Bot Script
After that I used the code I found on this site to make a bot that was able to tweet things out that it read from a text file. I called the script bot.py and the text file with the tweets tweets.txt.
#!/usr/bin/env python # -*- coding: utf-8 -*- # from: http://www.dototot.com/how-to-write-a-twitter-bot-with-python-and-tweepy/ import tweepy, time, sys argfile = str(sys.argv[1]) #enter the corresponding information from your Twitter application: CONSUMER_KEY = '123456'#keep the quotes, replace this with your consumer key CONSUMER_SECRET = '123456'#keep the quotes, replace this with your consumer secret key ACCESS_KEY = '123456-abcdefg'#keep the quotes, replace this with your access token ACCESS_SECRET = '123456'#keep the quotes, replace this with your access token secret auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_KEY, ACCESS_SECRET) api = tweepy.API(auth) filename=open(argfile,'r') f=filename.readlines() filename.close() for line in f: api.update_status(line) time.sleep(60)#Tweet every 1 minute
The script needs to be given a text file containing the tweets you want it to post. Make a .txt file in the same directory containing some tweets. Then call the script passing the .txt file. Assuming the script is called ‘bot.py’ and the tweets are in a file called ‘tweets.txt’ this is the command.
python bot.py tweets.txt
It’ll run for as long as it takes to post all the tweets from your file and it’ll wait 60 seconds between posting each one. When I ran it myself I got an InsecurePlatformWarning. It seems that’s down to the version of Python that I ran it with and the version of requests
that it uses. To fix it I ran installed the requests[security]
package as per this StackOverflow answer.
As of now you should be totally up and running with a Twitter Bot that can post tweets for you. It’s not the most useful of things considering it’ll only post through a list from a text file at a fixed interval.
Next steps in this project will be to add database support and time scheduling into the system.