We're going to make a Twitter bot! Unfortunately we have to jump through way too many hoops to get to that point.
If you don't have one already, visit twitter.com and make an account for yourself.
You'll need to check your email and confirm, just so they know who you are.
And since email addresses just aren't enough, you'll also need to add your phone number to your account. Otherwise your bot will only be able to read from Twitter, not write to Twitter!
Be sure to turn off the text notifications after you confirm your phone.
Now hop on over to http://apps.twitter.com and create a new Twitter application. "But this isn't an application" you yell! Twitter doesn't care about your semantics, though, this is just how Twitter manages all of your API keys.
Don't stress too much about your application details, you're going to be the only one seeing them. They're mostly important if you're getting other people to sign up for your application.
Now you'll want to make sure you can write two twitter. Select the Permissions tab on your project page...
...and set it to Read and Write. Then save.
Now you're going to need to grab your API keys.
Copy them into a new IPython Notebook, calling them consumer_token
and consumer_secret
.
consumer_token = 'blahblahblah'
consumer_secret = 'blahblahblah'
We aren't calling them api_key
and api_secret
because all code ever made ever in history ever ever calls them consumer_token
and consumer_secret
.
Your Twitter bot needs to use a different account than your real, live primary account. You'll need to sign out of your main Twitter account and create a new account.
I recommend giving it a plus addressing email address, where you do +blahblahblah after your email username - you'll still get the emails, but it acts like it's a separate account.
Now we need to authorize our robot account for the application.
From your IPython notebook that includes consumer_key
and consumer_secret
, let's install tweepy, a module for interacting with Twitter. From Terminal you'll want to run
pip install tweepy
or if it complains about permissions, use sudo
sudo pip install tweepy
Now we need to authenticate with Twitter. Tweepy's outline for this is
- Get a request token from twitter
- Redirect user to twitter.com to authorize our application
- Get the verifier code from the user.
- Exchange the authorized request token for an access token.
Sounds like fun, right? Let's do it.
import tweepy
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
Now get the authorization URL we'll need to visit
auth.get_authorization_url()
Mine was something like https://api.twitter.com/oauth/authorize?oauth_token=eUOvtXeDBfntN0OQI123znvgLG9fSFFRRWQNyV703KPs
. Visit this URL in your browser, while signed in as the bot account.
It will ask you to confirm that you'd like to grant access to the application, you say yes, then you're giving a special code.
Now hop back to your IPython notebook and feed it to tweepy.
pin = '8640XXXX'
Run the authorization code
token = auth.get_access_token(verifier=pin)
and now get your keys
print token.key
print token.secret
and save them somewhere
access_token = token.key
access_secret = token.secret
Additionally, copy and paste these values into a comment, you don't want to try to authenticate again.
api = tweepy.API(auth)
api.update_status('done and done')
Now visit http://twitter.com/yourusername
and see your brand-new post!
Whenever you're using your bot, you're going to need the access key + secret, as well as your consumer key + secret. If you start a new notebook to do that, you'll use
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
to create your tweetable API object, and then
api.update_status('done and done')
to tweet. Want to know what all tweepy can do? Check out the docs!