How to create a basic Twitterbot in Python

Requirements:

  • Python installed on your platform of choice
  • A registered Twitter account*
  • Ease with or at least knowledge of using the command-line tools for your operating system

*In order to create apps on your Twitter account, you need to verify it using a mobile phone number. For that, you can use your own phone number or, if you have a Google account, create one using Google Voice.

Naming your App

After logging into your Twitter account, proceed to apps.twitter.com. This is where you will register your app, get your API information, and control how your application works.

Twitter_NewApp

Click on the “Create New App” button to start the process of creating a new app and registering it with Twitter.

Twitter_CreateApp

For each required field, “Name”, “Description” and “Website”, make sure you fill out the information.

Note, too, that the “Name” will be how this application is known to other services. While you can using something simple like “TestingBot” for this example, it might be worthwhile to find a name you like and is not already in use for more advanced projects.

Twitter_Agree

 

Before you can finalize creating a Twitter application, you must agree to the Developer Agreement.

Take the time right now to read through it and become aware of how Twitter expects an application to act and what rights you have as a developer through this agreement.

Once done, click on “Create your Twitter application”.

Twitter_AppScreen

With your application now created, you can change the default permissions from “Read-only” (the application can only read your tweets) to “Read and Write” (the application can read and write tweets).

Changing Permissions

To do that, click on the “Permissions” tab.

Twitter_Permissions

 

From the choices, select “Read and Write” and then click on the “Update Settings” button.

Now that the permissions have been changed and your application will be allowed write tweets in the future, we need to find the API key information and create an access token.

Click on the “Keys and Access Tokens” tab.

Creating an Access Token

The “Keys and Access Tokens” tab contains your Consumer Key, Consumer Secret, and other private information. As noted in its description, none of those details should ever be public.

Twitter_CustomerScret

 

While Twitter will generate your Consumer Key and Secret pairs for you, it does not automatically create an Access Token for your application. To do that, scroll down the page to find that section.

Twitter_AccessToken

 

To generate an Access Token at your current permission levels (“Read and Write”) for your application, click on the “Create my access token” button.

Twitter_AccessTokenGranted

Once generated, you should now have a Consumer Key, Consumer Secret, Access Token, and Access Token Secret. All four will be needed to use the basic Twitter bot successfully.

Starting with Python

Before moving to the next section in this guide, I suggest you go ahead and create a new folder for testing the Twitter bot. That way, you won’t potentially disrupt any other processes or Python projects. (This is generally a best practice.)

There are two different ways to install the package.

Installing the Tweepy package via Pip

Open a command-line window and navigate to the folder you created for the project.

Once there, type the following and press Enter:

pip install tweepy

Once installed successfully, you are now ready to write code and one step closer to having a basic Twitter bot.

Installing the Tweepy package via Git

Open a command-line window and navigate to the folder you created for the project.

Once there, type the following per line:

git clone https://github.com/tweepy/tweepy.git
cd tweepy
python setup.py install

Once installed successfully, you are now ready to write code and one step closer to having a basic Twitter bot.

Testing your configuration

In the same folder your created moments ago, create a new file called “twitterBot.py” and copy the following code into it.

Within the configuration section of the code, be sure fill in each of the four items it needs: Consumer Key, Consumer Secret, Access Token, and Access Token Secret from your application settings.

Once done, save the changes.

To test the script and your configuration, run it with the following:

python twitterBot.py

If everything went successfully, you should now have a new tweet in your feed. It should look something like the following, but matching your own Twitter account.

Twitter_PostedTweet

Congratulations! You have now created a very basic Twitter bot capable of posting tweets from your Python code.

Where to go from here

Now, obviously, having a single tweet does not a Twitter bot make. It needs something more than the ability to post just a single string of “I’m posting a tweet!” over and over.

For that, consider looking at the Tweepy GitHub page for details on its API.