Skip to content

Instantly share code, notes, and snippets.

@ianfitzpatrick
Last active November 10, 2021 09:59
Show Gist options
  • Save ianfitzpatrick/4769090f34dec176e2d918e75a9c2668 to your computer and use it in GitHub Desktop.
Save ianfitzpatrick/4769090f34dec176e2d918e75a9c2668 to your computer and use it in GitHub Desktop.

How To Start Twitter Botting -- The Boring Parts

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.

1. Create a Twitter Dev Account

  1. 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.
  2. Click 'Create an app' button
  3. 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

  1. 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.
  2. 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.
  3. Tweeting original art I create.
  4. 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.

2. Create Posting App

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.

  1. Go to: https://developer.twitter.com/en/apps
  2. 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.

  1. You created, the app! Click 'permissions' tab and confirm access permission says Read and write
  2. Click 'Keys and tokens' tab
  3. Make note of the Consumer API Keys. There is just the key and your secret key. You will need both.

3. Create New Twitter Account for Bot

  1. Sign out of twitter or open new browser window with different user profile
  2. 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.
  3. You should now be signed in to twitter as your bot account

4. Grant Posting App access to your new Twitter Bot Account

  1. Open terminal on your Mac
  2. sudo gem install twurl (if that doesn't work, try: sudo gem install -n /usr/local/bin twurl)
  3. twurl authorize --consumer-key "Consumer API Key" --consumer-secret "Consumer API Key (Secret)"
  4. It will spit out a URL. Copy to clipboard.
  5. Open browser window that is signed in as your new Twitter Bot Account, and paste in URL from last step
  6. You should see something like Authorize My ArtBot Poster to use your account?
  7. Click 'Authorize App' button
  8. You will be given a PIN number. Copy to clipboard.
  9. Go back to terminal window, paste PIN number in and hit enter.
  10. If it worked you should see authroization successful
  11. 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.

5. Okay, finally, post something to twitter in Python!

Finally done with the auth song and dance, phew. Now we can actually code something.

  1. Open a terminal on your Mac
  2. sudo pip install tweepy
  3. sudo pip install pyyaml
  4. 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.

  1. python tweet.py

You should now have tweeted!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment