As a follow up from my ealier post introducing my twitter bot I thought I'd tell you how I did it and how to set one up on you own RaspberryPi or other Linux server.
It's not that tricky when you think it through, it is just hooking a few existing processes together to create your working bot. The only real tricky part for me was that I hadn't used Python before and had to learn it from scatch!
Here's a quick run through of what I used:
Twitter API - stating the obvious really but you need to use the api to do anything on twitter!
Twython - a Python wrapper for the the twitter API, it takes all the hard work out of posting and reading tweets
Python3 - you'll need to do all your programming in python to make use of the Twython
Python daemon - for running you script as in the background as a service
Wolfram|Alpha API - to answer questions
Wikipedia API - to get more answers
There is quite a lot of things to cover here so I'll not go into much detail about those tasks which are easy to complete with a little googling and I'll concentrate on the things which I struggled with.
First off you'll need a twitter account, I started a new one just for my RaspberryPi to tweet from. Then you'll need to go ahead and create an app within your twitter account and generate some keys for use with the API.
Same here, open an account and set up an app to get an API key
You'll need a folder to put all your code in, I set mine up in a directory in my home drive, something like:
Use these commands to get you going:
cd ~ md bin cd bin md tweeter cd tweeter
Next up you need to download the Daemon class suitable for Python 3.x - daemon3x.py script and save it in the directory. You can do this in command prompt:
run the following in the command prompt:
sudo apt-get update sudo apt-get install python3
run this from the command prompt:
sudo apt-get update sudo apt-get install python-setuptools sudo easy_install pip sudo pip install twython
Install my tweetbot script
Download tweetbot-d.py and save it to your directory.
Or you can get hold of the code over at GitHub:
Open it up in your favourite text editor and add in your API keys for Twitter and Wolfram|Alpha, then change the twitter ID of the stream you want to follow and additionally the superuser id which will enable you to turn the tweetbot off remotely should you need to:
Save and exit:
We should now be ready to start it up, you can use the following commands to start, stop and restart the bot
python3 tweetbot-d.py start python3 tweetbot-d.py stop python3 tweetbot-d.py restart
By default the script will log all activity from the bot to a file named
you can check on everything your tweetbot has been up to in this file:
Start the tweetbot when your Pi boots up
If you want to start the bot up automatically when the server boots up follow these commands:
sudo nano /etc/rc.local
then add these lines (obviously change the directory to the one you have saved your script in)
# Start tweetbot service echo "Starting tweetbot script" python3 /home/pi/bin/tweeter/tweetbot-d.py start
The script runs in the background monitoring any tweet containing your @twitter_handle, it then kicks in to analyse the content of the tweet, on discovery of the following hashtags:
it will run a run some command line code to reply back to the user with the appropriate answer. On detection of:
it will run a search through Wolfram|Alpha of the rest of the text in the tweet and reply with the answer. If Wolfram doesn't come back with anything it will try wikipedia as well.
Should you need to turn the twitterbot off for some reason when you can't access the Pi by usual means there is a kill switch built in. Simply tweet:
from your specified superuser account and the tweetbot will shut itself down.