So in this post, which is part 2, I’m going to show you how to post a tweet using a Python program on the Raspberry Pi.
Create Your Twitter App
Give Your App Permission to Write
Then make your app read/write. You won’t be able to tweet unless you give it permission to write. Newly created apps are read-only by default. Click on the settings tab and scroll down until you see this…
…then make sure that Read and Write is selected. Then click “Update this twitter application’s settings“.
Install Tweepy (if you haven’t already)
Now We’re Ready to Code
A lot of our code from last time can be re-used. But if you’ve created a new app, or changed the permissions on an old one, you might have to create new ‘Access tokens’.
The main new tweepy function we’re using is…
tweet_text is the text we type in at the time of running the program (in quotes)
update_status() is the function to post a tweet in tweepy.
The first 17 lines set up authorisation. Pretty much the same as before, but we’ve added
import sys to help us with the command line input arguments.
19-20 pulls in the command line text (if any) to tweet
22-23 provides a ‘default’ tweet in case no text was entered
25 checks the tweet text isn’t too long
26 sends the tweet
27-28 if the entered tweet is too long it warns you and nothing is sent
#!/usr/bin/env python2.7 # tweet.py by Alex Eames http://raspi.tv/?p=5908 import tweepy import sys # Consumer keys and access tokens, used for OAuth consumer_key = 'type in your consumer key here' consumer_secret = 'type in your consumer secret here' access_token = 'type in your access token here' access_token_secret = 'type in your access token secret here' # OAuth process, using the keys and tokens auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) # Creation of the actual interface, using authentication api = tweepy.API(auth) if len(sys.argv) >= 2: tweet_text = sys.argv else: tweet_text = "Still messing about with tweepy and twitter API. :)" if len(tweet_text) <= 140: api.update_status(status=tweet_text) else: print "tweet not sent. Too long. 140 chars Max."
I called the program tweet.py and made it executable with
chmod +x tweet.py
then ran it with...
./tweet.py "This is a test tweet using a Python program. :) ===>"
If you run it this way, the
sys.argv will work correctly.
If you prefer to run it as...
python tweet.py, you'll need to change
sys.argv in line 19.
Note that I put the whole tweet in quotes. If you don't do that, Python treats each word as a separate command line 'argument'. Also note, that if you use single quotes and your tweet text includes an apostrophe, it'll get all messed up. (i.e. your tweet will be shortened).
Trying without a tweet...
...gives you this...
Next time we'll do something more advanced and interesting with the 'default' settings, so something else will happen if we type no tweet text. We'll take some information from somewhere and tweet about it. Click here to go there now.