The boring parts of standing up a twitter bot I always forget. This guide should get you from "I got absolutely nothing" to "I posted a thing to twitter with Python!"
Here's the deal. If you make a bunch of bot accounts, and all those accounts are tied to one cell phone number, you are in for a round of API access recovation musical chairs.
To get around this, you need to create your own central twitter app. This app will in turn then be used by all your bot accounts. This app will be tied to your main account, backed by your phone number.
- Go to https://apps.twitter.com using your primary twitter account (or designated twitter dev account). If you are not prompted to setup a dev account at this point, you already have on! Skip to 'Create Posting App' section.
- Click 'Create an app' button
- Fill out fields as follow
Who are you requesting access for?
I am requesting access for my own personal use
Account Name
My Strange Family Of Art Bots (or whatever)
What use case(s) are you interested in?
Other
Describe in your own words what you are building
- This account will post on behalf of a few art bots I have. They are a combination of text-based tracery grammar bots, and bots that generate visual art.
- I will only analyze tweets if they are @ replied to me account, in which case the @ replies may be used as input to my bot to modify the artistic output of my bots.
- Tweeting original art I create.
- I do not plan to display and user data off of twitter.
Will your product, service, or analysis make Twitter content or derived information available to a government entity?
No
--
Finally, verify your dev account via email if requested.
This twitter app will allow you to read and more importantly write to twitter. This is the app all your bots will use later. You'll only need to create this app once, not on a per-account basis.
- Go to: https://developer.twitter.com/en/apps
- Click 'Create an app'button
App Name
My ArtBot Poster (or whatever)
Application description
Artbot posts by me (this is theoretically public facing)
Wesbite URL
https://www.mypersonalhomepage.com
Enabled Sign in with Twitter
No
Callback URL
https://www.mypersonalhomepage.com
(Fine that this is not a valid callback URL)
Terms of Service, Privacy Policy, Organization
These look required, but can be left blank.
Tell us how this app will be used
This account will post on behalf of a few art bots I have. They are a combination of text-based tracery grammar bots, and bots that generate visual art.
- You created, the app! Click 'permissions' tab and confirm access permission says Read and write
- Click 'Keys and tokens' tab
- Make note of the Consumer API Keys. There is just the key and your secret key. You will need both.
- Sign out of twitter or open new browser window with different user profile
- Sign up for a twitter account, use phone number to do two-step verification. Should be okay even if you used this phone number for other accounts.
- You should now be signed in to twitter as your bot account
- Open terminal on your Mac
sudo gem install twurl
(if that doesn't work, try:sudo gem install -n /usr/local/bin twurl
)twurl authorize --consumer-key "Consumer API Key" --consumer-secret "Consumer API Key (Secret)"
- It will spit out a URL. Copy to clipboard.
- Open browser window that is signed in as your new Twitter Bot Account, and paste in URL from last step
- You should see something like Authorize My ArtBot Poster to use your account?
- Click 'Authorize App' button
- You will be given a PIN number. Copy to clipboard.
- Go back to terminal window, paste PIN number in and hit enter.
- If it worked you should see authroization successful
- Confirm your app is listed under Apps connected to your Twitter account here https://twitter.com/settings/sessions
Note that twurl will keep a record of all your authorized Twitter Bots in ~/.twurlrc
, which is a YAML file. This is great, because you don't have to stick your twitter credentials into your source code, and can keep it out of version control.
Finally done with the auth song and dance, phew. Now we can actually code something.
- Open a terminal on your Mac
sudo pip install tweepy
sudo pip install pyyaml
- Create a a file called
tweet.py
(wherever your project will go) with the following contents:
#!/usr/bin/env python
import tweepy, time, os, sys, yaml
# Load twitter credentials for this bot from config file
BOTCRED_FILE = '%s/.twurlrc' % os.path.expanduser('~')
with open(BOTCRED_FILE, 'r') as credfile:
full_config = yaml.load(credfile)
api_key = api_key = full_config['profiles']['myartbotname'].keys()[0]
bot_creds = full_config['profiles']['myartbotname'][api_key]
CONSUMER_KEY = bot_creds['consumer_key']
CONSUMER_SECRET = bot_creds['consumer_secret']
ACCESS_KEY = bot_creds['token']
ACCESS_SECRET = bot_creds['secret']
# Do actual authentication
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
# Post Tweet
tweet_text = 'Hello World'
# api.update_status(status=tweet_text)
# Post Tweet w/ Image
# tweet_image_filename = '<img filename goes here, should be in same directory as tweet.py>'
# api.update_with_media('%s/%s' % (sys.path[0], tweet_image_filename))
The only things you should need to change are both instance of myartbotname to the name of your actual bot. Additionally, if you want to upload an image, uncomment th last two lines of the file, and fill out the tweet_image_filename variable.
python tweet.py
You should now have tweeted!