Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Twitter

Using the Twitter API to Tweet Repetitive Content

This post is part of a series called Building With the Twitter API.
Building With the Twitter API: Analyzing Your Followers
Final product image
What You'll Be Creating

Welcome back to our coverage of the Twitter API. If you use Twitter, you may have come across a friend sharing tweets from the @infinite_scream bot (shown above). I know it's mostly a bot because it tweets at ten-minute intervals. But it varies the string length of its virtual screams to avoid being blocked by the Twitter's API's infamous undocumented restrictions. Tweet too frequently or repeat the same content and you'll find your bots hopelessly blocked.

Recently, an online friend asked me to help them write code for a bot that might repeat but provide some intelligent content variation. In today's tutorial, I'll write about how to do this with the Twitter API.

In today's tutorial, I'll describe how to build a bot that does the basics:

  • random tweet interval
  • random selection of status text
  • random selection of hashtags
  • random inclusion of URLs
  • avoid getting blocked by Twitter

However, if you want to use a Twitter API bot to effectively promote your product or service on your own account without annoying your followers, you need to write code that intelligently varies the topic, contents and frequency of your tweets in an organized way. I'll be writing about how to do that in future episodes. 

If you have any questions or ideas, please post them in the comments below. If you'd like to see some of my other Envato Tuts+ tutorials, please visit my instructor page, especially my startup series. Let's get started.

Getting Started

For the bot in episode one, I'm trying to generate fun tweets on a regular basis while avoiding upsetting the great Twitter God of Restricted Access in the Sky.

The tweet content is very simple and can be randomly created by combining previously written status text, hashtags, and URLs.

The bot runs in Yii, a popular PHP-based platform. I'll keep the guide below fairly simple for straight PHP developers. However, I encourage you to use frameworks. You can learn more in my Yii Series.

Building the Bot

Registering a Twitter App

Basically, the first thing I did was register an app to get my Twitter keys:

Repeating Twitter API - Setting up your Twitter App

If you aren't familiar with creating an app and authorizing API access with Twitter, please review some of our earlier tutorials:

Authoring Content for a Range of Tweets

I wanted to create a system where my friend (or any approved author) could write variations of tweets and place them in a database for ongoing use. First, I created a database migration to build the table for them.

All of my tables for this project have the prefix norm_. Here is the Tweet table or norm_tweet:

Next, I used Yii's Gii scaffolding system to create a model and CRUD files for me. Here's the model:

Repeating Twitter API - Yiis Gii Scaffolding Model Generator

And here's the CRUD generation:

Repeating Twitter API - Yiis Gii Scaffolding CRUD Generator

So all of this code automatically provides the grid view below and editing capabilities.

Repeating Twitter API - The Authoring Tweets UX

For now, I'm not using the Media ID, which is for images that are uploaded to be used within tweets. I'll likely address this in the next episode.

Pretty straightforward so far, right? 

Adding a Range of Hashtags

Then, I repeat this process for the other models. Here's the norm_hash migration for hashtags:

The idea is to randomly include a selected hashtag (or two) in the tweets to make it appear that the bot is human, varying its tweets. 

Here's the Hashtag UX:

Repeating Twitter API - The Authoring Hashtags UX

I won't repeat the Yii Gii steps from above, but I repeat them for norm_hash and norm_url as well.

Adding a Variety of URLs

Here's the database migration for adding URLs:

The bot master may add URLs at different times. It's probably best for this bot not to use old URLs or to repeat them. The last_used and created_at Unix timestamps allow the tweeting algorithm described below to decide when to use URLs.

Here's the URL UX page:

Repeating Twitter API - The Adding URLs UX

Now, let's look at the fun algorithm to combine all of these tables into interesting tweets that aren't annoying to Twitter's Master Overlord of Restrictions.

Randomizing the Content of Bot Tweets

It's fun to have a bot that tweets a variety of content, but the variety is also helpful at preventing it from getting blocked by Twitter. 

You can see the Twitter Rate Limits here, but some of the rules for content repetition don't appear to be documented:

Repeating Twitter API - Rate Limits Chart

I took directions from my friend as to how they wanted the algorithm to build tweets from the database of tweets, hashtags, and URLs.

Here's the build algorithm we decided on for now; it's easy to tweak. I'll describe it in parts.

In my opinion the algorithm below is low on its use of hashtags and URLs, and if you want a more robust variety of content, change it to your liking.

First, we use yii\db\Expression; to properly select a random single tweet text from the table:

Then we decide whether to use a hashtag (currently 1 in 5 or 20% of the time) and how many to use (currently fixed to just one):

Then, we decide if there is a URL available to use. URLs must be less than a week old and they can only be used once every 72 hours (3 days). So any new URL might only be available for use once, twice or possibly three times before expiring.

Finally, we build the tweet based on the selected data (available URLs are added only one in four times or 25% chance):

Choosing When to Tweet

Yii allows you to call console controllers from cron. So I add a call to my /console/DaemonController.php in crontab.

$ sudo crontab -l

Here's how my tasks are scheduled:

Every hour, daemon/hourly in /console/DaemonController.php is requested. Our bot app only decides whether to tweet or not once every four hours.

First, you'll see I have a NormLog table which I didn't describe above, but that tracks all the output and when tweets were made. So my friend didn't want to tweet more than once a day. 

We didn't want followers of our bot to get annoyed by high frequency tweeting.

Then, we pick a number, basically the six times a day (every four hours), and tweet if the number is 6 (or a one in 12 chance).

Delivering the Tweet to the Twitter API

Here's the NormTweet::deliver() method called by the Daemon to post the tweet:

The account's Twitter application keys are stored in /bot/frontend/config/params-local.php, configured from the bot.ini file I use:

Bots aren't simple, but they are fun!

Examining the Results

Here are the results of our bot:

Repeating Twitter API - Tom McFarlin Head Editorial Goddess

Just kidding! That's one of the editorial goddesses, Tom McFarlin. AI scripts aren't yet capable of replacing his "insights," but Envato Tuts+ has hired me to work on this. 

Here's the actual bot, meant to remind my friend and its followers that America's new politics aren't exactly normal. I imagine whatever your views you'd agree with that.

Repeating Twitter API - Bot Tweet Stream from Tutorial

I hope you've enjoyed this episode.

What's Next?

Next, I'm going to create a more marketing-driven platform to help you use the Twitter API to promote your startup, services, and business without getting labeled as a bot and blocked.

If you have any questions or suggestions about this tutorial, please post them in the comments. If you'd like to keep up on my future Envato Tuts+ tutorials and other series, please visit my instructor page or follow @reifman

Related Links

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.