Skip to main content

DevStack

Actually using your Raspberry Pi part 4: Twitter Bot

Published:

May 19, 2016

Updated:

March 28, 2023

Share
Raspberry Pi - Part 4: Twitter Bot

If you have been reading along for a few blog posts with me you know that I got a Raspberry Pi for Christmas back in December 2015, and I have struggled to find the time to get much done with it. Two months in, and I have failed at an IP camera project, built a silly (but fun) game in Scratch, and learned some of the basics of Python.

At this point,February has just ended, and I got a little busy for a week or so. In that first week of March I do not turn on my Raspberry Pi, but while I was looking through twitter one day that week I ran into an article on the Twitter API. I started thinking that it would be cool if I could come up with a project that used my Raspberry Pi and the Twitter API.

I enjoy following twitter accounts that talk about the latest things happening in downtown Raleigh, NC like @NewRaleigh. And, I enjoy some funnier twitter accounts that are made up versions of people. A good on is @NotCoachJohnson which is a funny caricature of Georgia Tech’s football coach. This led me to the idea of a twitter account about downtown Raleigh that my Raspberry Pi could run to send out tweets. I live in an apartment with really big windows in downtown Raleigh, so I settled on the idea of pointing the Raspberry Pi’s camera at the corner next to my building and have the Raspberry Pi run @TuckerandBoylan. I went out and took some nice pictures of the street sign and the corner, so take a look at the account here. The idea I came up with was to have this account look for tweets about Downtown Raleigh weather. Then if it finds one, it takes a picture of the corner of Tucker&Boylan, and tweets back that this is what it looks like on the corner of Tucker&Boylan. Hey, I know it’s silly. But, it was a neat little project idea, and I desperately wanted to use my python knowledge to actually make something.

Before we jump into the twitter API, I want to circle back on something that I mentioned in the first blog post about getting a Raspberry Pi. The camera does not point where you want it to. There are a couple of camera stand options that you can buy online for between $7 and $25, but I didn’t want to wait for them to ship. It also seemed like buying a camera mount would not be in the DIY Hacker spirit of what I was doing here. So I looked around my apartment and found a shoebox that just might do what I wanted. If you cut out a corner of the top of a shoebox you can get this.

camera stand

I used 2 thumb tacks to attach the camera to this very DIY stand, and it works perfectly. Now I just point the Raspberry Pi out of my window at the corner and it can take the picture. But, how do I take a picture? The answer I found after a lot of investigation online was that there are a few different ways to take a picture. There is a good section on raspberry pi.org about using the camera in Python, but I ended up using some code that I found on either github or stack overflow and I changed it around to meet my needs.

After I got that working and took a few test pictures and I investigated the twitter API. The API documentation can be summed up in one word… Extensive! Take a look.

The first thing I needed to be able to do was create an application and authenticate the BoylandandTucker account with the twitter API. I found this page below in the documentation site and followed it to set up my API Tokens.

https://developer.twitter.com/en/docs/basics/authentication/overview
twitter screenshot

Just follow the instructions on this handy page to set up an application and then get the

  • App Key:
  • App Secret:
  • OATH Token:
  • OATH Token Secret:


Once I had my authentication ready I was faced with a choice. Do I use the API “straight up” or do I use a wrapper or sdk? I am used to using the Ruby SDK on the Bandwidth Application Platform, so I decided to find something similar. Luckily, a short Google search lead me to Twython. I decided to use the Twython repository on Github that had the most forks, so I used https://github.com/ryanmcgrath/twython.

Following this github documentation to install python before you get started. Once that is done you can start to do some basic usage like this or more advanced usage like this.

To send a tweet on your own you could use the code I put together. Remember to add your own credentials at the top!

This should be enough to get you started!

So what about my project? If you have been reading along through all four blog posts you remember that the amount of time it can take to get anywhere with you Raspberry Pi (or any programming project) when you are just getting started can seem like forever. I spent the first half of March in twython experimenting and getting it to kind of do the basic things I wanted like look up tweets, create a tweet, or take a picture and then post it. In the second half of March I had a big breakthrough where I got better at breaking my code into functions (def’s!) and then having one file that called those different functions. On April 3rd I achieved what I am going to call my 1.0 Twitter Bot App!

Here is what it does

The first file I call “method caller”. This file has a string of weather related words that it goes through one by one and calls the other files (which you can see are imported at the top).

Method Caller

The second file is the first one that method caller uses as it loops through the “weather” list. I call this file “tweet find”. This file has a function that looks for the last tweet about “Downtown Raleigh (insert weather word)”

Tweet Find

After the latest tweet is looked up method caller uses a file I called “date checker”. Date check takes today’s date and compares it to the date of the latest tweet from “tweet find”

Date Checker

If the date checker returns a “no” then method caller stops and moves onto the next weather word in the list because there were no tweets today. If date checker returns “yes” then it uses another file called “who tweeted”. This file looks up the tweet again and returns the twitter handle of whoever sent the tweet. This gives us someone to tweet back at.

Who Tweeted

Note: One of my next steps will be to consolidate “who tweeted” and “tweet find” because I could have “tweet find” also return who tweeted. But hey, this code still works!

Once I have the twitter handle to tweet back at, I use a file I made to take a picture with the Pi camera

Take a Pic

Once I have a picture of the corner of tucker and boylan I then tweet that back at the twitter handle I found earlier.

Tweet Pic

And that is it for now. There are a number of ways I could reduce complexity of the code, and my next big thing is to learn how to use a callback so that the app can respond to a tweet whenever one happens. Currently the program needs to be run manually once a day to really work. But it does work, so I am proud of that. You should give making your own Raspberry Pi app a try! It only took me 3 months and some change! This was a really rewarding experience overall and I learned a ton!

Helpful Links: