PyLadiesATX Twitter Bot Mini-Workshop

http://irmakramer.com/presentations/twitter_bot.html

The Twitter API

Wait, What is an API?

API

An application program interface (API) is a set of routines and protocols for building software applications. An API specifies how software components should interact. It’s a contract between the provider and consumer.

API

An endpoint(address) on a server that when contacted via a a request, much like an html/webbrowser request, returns data in the form of json or XML. A developer can then use that information to create applications.
Some APIs also require authentication before being able to access the data.

The Twitter API

Has a lots of API endoints.

You can see some of the public ones here:
https://dev.twitter.com/rest/public

How do we navigate?

For this session, we'll be focusing on the post Twitter API.

https://dev.twitter.com/rest/reference/post/statuses/update

That's a lot to take into account!

There's a Library!

Twython!

Twython makes all of these API calls and responses easy to navigate.


twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.update_status(status="Tweet String")
					

We need keys

To generate keys for your new bot, you'll need to create a new Twitter account first!
You can then go to https://apps.twitter.com/ to create a new application.

Why do we need keys?

Twitter needs to verify that the app builder (you) has permission to post tweets to a specific account.
The App Keys are related to your application and the Oauth Keys are specific to the user.

Since we're building an app to access and update our own bot's data, we can generate all the keys ourselves.

Testing Keys!

I've created a testing bot, @pyladiesatx_bot, for all of us to use during this workshop.
You are welcome to create your own, or you can use the keys for our testing bot here:
Twitter API Keys

NOTE: These keys are no longer valid. If you need help setting up your own, feel free to contact me!

Code to get us started

Setup your environment and project per the README file in the git repository and run the code once to see how it works.
https://github.com/bluflowr/twitterbot_workshop

Working on your bot

Change the make_tweet function.

Things you might want to try

  1. Mentioning a specific user in a tweet
  2. Creating randomized tweets from different words
  3. Adding a time variable. i.e. Good Morning, ... or Good Evening, ...

Scheduling your bot (Optional)

There are many ways to schedule your bot. Heroku has a free scheduler you can read about. For today's workshop we'll attempt to setup a cron job on our laptops. As long as they're running and internet connected, your computer can automatically run a script for you.


crontab -e
0 5,17 * * * cd /Path/to/your/code && source YOURenv/bin/activate && python tweet.py			
					

Resources

Thank You!

Irma Kramer
@bluflowr
irma@irmakramer.com