A little context

The Twitter API is a playground, if you’re inclined toward social networks and information. Building a basic bot to interact with the platform via the backend functionality is a fun way to learn some rudimentary capabilities. There are a few different ways to go about triggering bots and timeline activities but I’m going to give examples that rely on the Python language to make things happen.

In early 2018 I acquired a Raspberry Pi to start prototyping a few projects and began initial experiments to learn to write code in Python. This included deploying packages such as Tweepy, which facilitates interfacing with the Twitter API via account credentials.

My first exercise was to create image collages that Wild West Analogy, an old account that had been sitting dormant for a few years, would post.

Wild West Analogy Twitter bot

The current iteration of the script randomly selects one of my photos from a folder, it also randomly selects a phrase from an array to both tweet as text and write over top of the image in a Western-style font. It’s a very simple process but was a nice endeavor into generative art. I crowd-sourced the phrases on social media and from folks offline.

Wild West phrases crowdsourcing

Having confirmed my ability to post via the API to the account, I decided to branch into engagement. When the account was initially created it was focused on instances of the phrase “like the Wild West” which is a commonly used idiom. So the next script pinged the search function to find 5 tweets that included those words then retweeted them. Again, very simple - it had been a number of years since I had worked with the Twitter API and didn't want to rely on domains or major data management.

Watching how language usage evolves over time is fascinating and I thought I saw a few trends emerging through the collection of retweets made by the account. Do you think this observation is accurate:

Wild West idiom context

My RPI isn’t mobile so all of my work had to be done at home which limited when I could let inspiration strike or trigger the bot without jumping through additional hoops. So to transition to mobile and more readily trigger activity or dabble with code tweaks, I started using Pythonista on iOS. This also required switching from the Tweepy Python package to Twython. Without getting too technical here, this required a few additional processes, trial and error, as well as a tip from a friend that had already encountered the same issues.

My writing style is probably best described as “run and gun”. Frequently, I’m jotting down random thoughts or phrases into my Notes app, fleshing out concepts into full sentences, or attempting to shape up paragraphs in Dropbox Paper while in the middle of an unrelated activity. Nonlinear thinking and editing is your friend. It was this transition to mobile that made me begin thinking about unleashing a small squadron of bots to explore more language usage on Twitter.

The next project required another account (because I didn’t want to cross streams) to delve into new themes. Roman Idioms was launched to explore the use of the following phrases:

  • “Rome wasn’t built in a day”
  • “When in Rome”
  • “All roads lead to Rome”

Roman Idioms Twitter bot

The script is basic, just three queries for 5 instances of each phrase to retweet. I haven’t spent nearly as much time assessing how people are using those phrases because there are three times as many daily RTs on the account. My general sense is that the first two are used as an allowance,  a way to permit ourself to pace ourself or behave in a foreign way. The third expression I think references concentrated power.

I launched one last language-focused bot earlier this year when I started thinking about writing this essay. It is called Trendy Cliche and retweeted posts that use the phrase “vibe check” at launch. I believe the medical coding incident we experienced this Spring has quietly laid this cliche to rest - though we still see iterations of “vibes” in use.

Having started a considerable number of Twitter accounts over the years for different projects or interests, I became eager to deploy scripts across a number of them to increase my exposure to particular information or increase engagement around a topic. This includes Rustbelt Innovators (searching for technology/business/entrepreneurship posts around particular metropolitan regions), SpeakerCone (searching for posts regarding various art styles), Victory Garden (searching for usage of the phrase “victory garden” of course), and my own profile to engage with posts about the 1893 Columbian Exposition as well as architecture in Chicago.

Building the bot

In this segment I intend to show you how to spin up a Twitter bot with the developer API. The process is fairly straight forward if you have any development experience. If not, don’t worry because this is a great way to dip a toe in and give it a try. This walkthrough is going to assume you already have Python available on your computer because I don’t want to overload the lesson.

A bot script can be setup on any Twitter account but for this tutorial I’ve launched a brand new Twitter profile and will illustrate how to apply for a Twitter Developer account as well. Trendy Cliche will target popular phrases. Once you have a Twitter account selected head to the Twitter Developer URL at Developer.Twitter.com in your browser.

Twitter Developer application

The application process is fairly simple. Click into the “Apply” tab on the top right menu to begin.

“Making a bot” is the first option under the “Hobbyist” column. Select it to enable the “Next” button on the bottom right.

Twitter Developer application

You will need to connect your account to a phone number that can receive a text message to confirm your account.

Twitter Developer application

There are a few sections that will require you to describe your intentions for the bot. Keep them clean and plain, don’t be cute or esoteric.

Twitter Developer application

The text areas have minimum character counts and I’ve always attempted to keep my descriptions close to that. I have never had a developer account or bot denied.

Twitter Developer application

Twitter Developer application

Once you have completed the application, submit it and wait for an email that confirms your account has been approved. Approval typically occurs within 24 hour after submission. Head back to the Developer site once you have received it to proceed with the next steps.

Twitter Developer application

Click into “Create an app” to begin the form.

Twitter Developer application

Twitter bot development

Again, keep your descriptions short and to the point. No need to write an essay here. I usually title my app functionally because you may want to use other functions or features of the Developer platform in the future and this will help to keep things clear.

Twitter bot development

Twitter bot development

Twitter bot development

Once your bot has been created you will need to generate a few API tokens. Click into the tab titled “Keys and tokens” to generate the pair you’ll need to input into your code. You will only be able to view this set of token one time so be sure to copy and paste them to a notepad app or write them down. If you don’t have them correctly documented it will require regeneration, which isn’t difficult but can be an annoying snag in the process.

Twitter bot development

Next comes the actual Python code to get your script triggering activity on the Twitter account via the API. In this example I am using the iOS app Pythonista which allows this bot to be activated on your iPhone. It costs $9.99 and is very convenient but not required to build Twitter bots. This tutorial demonstrates how to activate a bot using the Twython Twitter package but there are others that are available. You could do this on any machine that will run Python such as a Raspberry Pi which comes preloaded with it.

Twitter bot development

Paste your Keys and Tokens into their respective places in the code for app_key, app_secret, oauth_token, and oauth_token_secret. This is required to “speak” with the Twitter API.

The section below that indicates this bot is looking for the phrase “vibe check” to retweet. It is set to search for and retweet 5 posts that include that exact phrase.  If the script has successfully found a post your log will say “Tweet Retweeted”, if you have already retweeted a post it will show a 403 error instead.

Twitter bot development

After that head to the account to see what posts were retweeted.

Twitter bot development

This is a fun way to experiment with the Twitter API and also to discover accounts, media, and ideas on Twitter that you may not already be aware of presently. Of course, it has a high probability of engaging with themes you may not find interesting or even agree with so you may want to monitor what it’s doing if you have a particular agenda. For some accounts I continually tweak the script and for others I just hop into the account and disengage with a post.

Hopefully this walkthrough has helped you to get a bot launched and to consider how it can be used. There are many different ways to apply them and your imagination is the limit.

Look forward to seeing what you create!