fbpx
Select Page

If you’ve taken a look at my Twitter account recently, you’ve probably noticed an uptick in tweets about the games I’ve made. That’s because I set up a bot for Twitter a few weeks ago to automatically tweet promotional things for me. It’s been a game-changer, so I wanted to take some time and write up how I did it so that others can do the same.

Up top, let me say that I wouldn’t have been able to do this without the help of Chris Bisette. He took the time to DM and do a video call with me while I got the basics set up. Invaluable. Thank you, Chris!

A Note: This method requires your computer be on in order to work. That works for me. If it doesn’t work for you, this tutorial isn’t gonna help.

The Basics

First off, this walkthrough is for Windows. I’m running Windows 10. I assume that similar methods will work for other flavors of Windows, as well as Mac OS and Linux, but I don’t know the specifics of how to get stuff installed on those. Google will be your friend.

What You’ll Need:

If you’re at least passingly familiar with what code looks like and how to use the Windows Command Prompt, that’s helpful. If you’re not, you can probably still do this, but you’ll be learning a lot of new stuff.

Let’s get going!

Twitter Developer Account

Do this step early and first. Unless you get this done, there’s no way to make the bot.

Head to the Twitter Developer Portal, and request an account. The initial request should go through automatically. That puts you halfway there. What you need for this method to work is something called Elevated Access.

A screenshot of the Twitter Developer page that explains the different access levels.This process was the most painful part of things for me. When you request access, you’re asked why you want access to the API. The forms seem to be read by a bot or algorithm, so it’s looking for  specific keywords to confirm your access.

I filled out these forms six times and each time, the “intended purpose” field didn’t spot what it needed, so I couldn’t submit the request. I finally worded things correctly (making sure I said bot and promotion, I think) and the form submitted.

Then I had to email back and forth with Twitter Developer Support three times to further clarify my intended use. I basically said the same thing three different ways until they finally granted me Elevated access. It was a pain in the butt.

The Bot on Twitter’s End

While you’re waiting for your Elevated access, you can set things up in the Developer portal.

A screenshot of the Twitter Developer page showing where to set up OAuth

Stuff here is pretty straightforward. Give it a name and a purpose.

Screenshot of the Twitter Developer Page for Project Settings

Don’t worry about the OAuth stuff yet. You’ve got to have your Elevated Access before you can really activate it. Same goes for Keys and Tokens.

After Elevated Access

Once you’ve wrangled Elevated Access, you need to come back to your Developer Portal and head into your project.

First, go to the OAuth settings and enable 2.0 and 1.0 (just in case).

Twitter Developer OAuth Settings

In the “Callback URI/Redirect URL” I just put my own website. I have no idea if that’s good, bad, or unimportant. It worked, so I kept it.

Once OAuth is enabled, you need to go grab your keys. On the Project Settings page, click Keys and Tokens.

A Screenshot of the Twitter Developer Project page for Keys and Tokens,

IMPORTANT!!!!!!!!

This is the stuff you need to keep secret. Click “Regenerate” on each of these and copy the strings of letters and numbers to a document that you can keep secure. These strings give access to your Twitter account, so don’t share them.

The reason you’re regenerating them is because we enable OAuth in the step before. These new keys will have that access baked in. I didn’t do this on my first try out and it took me some fiddling to figure out.

You need:

  • Twitter API Key
  • Twitter API Secret Key
  • Twitter Access Token
  • Twitter Secret Access Token

You can also grab the Twitter Bearer Token but we won’t be using it in the Python code.

Speaking of Python…

Python and Tweepy Installation

Go to the Python website and download Python. I installed mine to the default location it picks. Wherever you choose to install, make a note of the folder. You’re going to need the file path to that folder.

Python installation is simple. Double-click and go. Once it’s installed, you need to make sure that python has the Tweepy library. To do that, you’ll need to open a Command Prompt.

If you’re not familiar, hit your Windows key (or open your start menu) and type ‘cmd’ (with no quotes). The command prompt will open up to your default user directory. A Note: When I tell you specific things to type, don’t include the quotation marks unless we’re in the code itself.

Here’s where you’ll need the file path for your Python installation.

First, type ‘cd FILEPATH TO PYTHON INSTALLATION.’ In my case, that looked like:
cd C:\Users\tbarn\AppData\Local\Programs\Python\Python310

When you hit Enter, it changes your active directory to the place Python was installed. It’s like looking at the folder, but just in text form. If you want to confirm you’re in the right spot, type ‘dir’ and hit enter and it should list out all the files and folders it sees. That should match what you see in the folder itself in Windows.

Next, we install Tweepy. The link there says to just type ‘pip install tweepy.’ I found that didn’t work. They leave out that you need to tell the Command Prompt to use Python to do this. Instead, type:
‘py -m pip install tweepy’

Hit Enter and a bunch of lines will scroll by, looking similar to the screenshot below. Mine says “Requirement already satisfied” because I already installed Tweepy. Don’t worry about the version of pip message.

A screenshot of the Windows Command Prompt, showing the directory change and theTweepy installation.

From here, it’s time to go to the actual code!

The Code

In your Start menu, there should be a Python folder. Open IDLE as an Administrator (it can cause problems depending on where you install if you don’t do this). Right-click and select ‘Run as Administrator.’ If you only want to do this once, you can right-click and go to properties and compatibility and check the box to always run as admin.

Once it’s open, this is where we’ll put the code. In IDLE, go to File->New File. Name the new file something you’ll remember and save it to the Python directory. You can save it somewhere else, but keeping everything together was a help to me.

In the new script you just made, type or paste the following code:

import tweepy
import random

consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

# Authenticate to Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

# Create API Object
api = tweepy.API(auth, wait_on_rate_limit=True)

# Open the Tweets file and pick a random line into a string.
def publictweet():
lines = open('c:/Users/tbarn/AppData/Local/Programs/Python/Python310/AutoPromoTweets_TB.txt').read().splitlines()
tweettopublish = random.choice(lines)

api.update_status(tweettopublish)
print(tweettopublish)

# Run the function
publictweet()

Keep everything that’s here, pound signs, quotes and all.

Here’s what it looks like in the IDLE program

Screenshot of the Twitter bot code

All the colors should be the same. Orange things are Python commands (importing Tweepy and the Random function). with consumer_key, etc, we’re defining variables for the code to use. Anything with a # in front of it is a comment. You also need to make sure that the indents are the same. Python needs those to parse everything properly.

Once the API keys are inserted, save the file and open a text document. This is where you’ll write the tweets themselves. We’re doing this now do you can test if things are set up correctly.

Put a line in the text document and save it. I saved mine into the main Python folder, like everything else for this project. Once it’s saved, copy the file path and paste it into the code. IMPORTANT: After you paste the file path, change all of the backslashes to forward slashes. Python won’t be able to parse the filepath otherwise.

Once that’s done, hit save. That’s it!

Testing and the Tweets

Once you’ve saved, you can test out the script by double-clicking it. If all is set up right, your Twitter account should tweet the single line you put in the text file. If something’s not right, you’ll get errors that you can then look up to debug.

Once the script is working, you can take that text file and replace your test sentence with a bunch of tweets. Every time you hit Enter for a new line break, the code in the script views that as a different tweet.

Screenshot of tweets from my text file.

Sadly, that means that this method (as written) can’t do line breaks in the tweets themselves. It also can’t do images. So you have to be clever about how you structure things.

Also of note, I use this to boost other people I dig, too. I put a slash between the @ and their username when I do that so they don’t get pinged whenever the bot pulls one of those tweets.

The random function is truly random, too. It doesn’t check off a list and then only pull from what’s remaining. So put at least 30 tweets in your text file. In fact, the more the better, so you have some variety.

Now that’s set up, there’s one last step and that’s scheduling.

Scheduling the Tweets

This script picks a random tweet when the script is run. So we’re going to use Windows Task Scheduler to get that going. The setup I used is a result of trial and error. There may be a better way to do it, but it works, so I’m sticking with it.

Hit the Windows key and type Task Scheduler.

Windows Task Scheduler

Once you’re there, make a new folder in the Task Library. I called mine ‘My Tasks.’

Next, you want to create a task. Use Create Task on the right-hand side. A Basic Task doesn’t have everything we need, so we need to do it the long way.

First, you get the General window.

Screenshot of the Task Scheduler General tab for a new task

Name the task. Then Choose ‘Run where user is logged on or not’ and check the ‘Do not store password’ box.

Next, Triggers.

A screenshot of the Task Scheduler Triggers window

Hit ‘New’ and set up the Trigger.

It’s a ‘One Time’ trigger, that you can set to whatever time you want. For testing purposes, once it’s all finished, set it for a time not long from when you’re working so you can see if it goes off.

I set mine to delay the task for up to 15 minutes so it’s not tweeting things out at exactly the same time every day.

The key to this is how often the task repeats. You have to set longer intervals in minutes. I set mine to every 3.5 hours, or 210 minutes, and an indefinite duration.

Also, check the Enable box so it’s, y’know, enabled.

Actions!

Make a new action. In the dialog box that pops up, choose ‘Start a program’ and then navigate to the folder where your script is stored. Choose that, then hit OK.

Under Conditions, I unchecked the AC power box under Power, then checked ‘Wake the computer to run this task.’ Don’t know why, but the AC box was causing things to not work. It might be different if you’re on a laptop. I don’t rightly know.

For Settings, check ‘Allow task to be run on demand’ and ‘Run task as soon as possible after a scheduled start is missed.’ Everything else is unchecked.

Don’t worry about the History tab.

OKAY!

From here, change the start time to something soon to when you’re working and wait for the automated tweet to go off. If everything is set up correctly, you should see a tweet and then Task Scheduler will tell you when the next tweet is set to go.

If it doesn’t work, you’ll need to poke around in the settings and fiddle with things. That’s how I settled on this setup. YMMV.


This is a decently long process but it’s yielded good results for me. I get a lot more engagement on these tweets than I expected and it’s turned into some sales of older games, too.

It’s a lot of legwork up front but once it’s set up, you can just keep adding to the text file of tweets. If you’re promoting something specific, you can change the code to point to a different text file or duplicate the script with a different text file and set up Task Scheduler to tweet from both! There are a lot of possibilities.

If you’ve got questions, I’ll do my best to help but I can’t debug or troubleshoot for you. I hope it all works out for ya!