blog

Using the Twitter API to Tweet Repetitive Content

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:

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:

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:

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.

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):

// decide whether to use a hashtag
// pick a random # between 0 and 4
$useHash = rand(0,4);
// if rand# is not 0 but instead 1,2,3 or 4, include hashtag(s)
// for less freq use of hash tags, we can change this > 2 or >3
if ($useHash>3) {
// so we're now going to decide which and how many hash tags to
// Creator decided to only use one hashtag for right now
$numHash = 1; // rand(1,3);
// select this rand# $numHash randomly from our list
$hash = NormHash::find()
->orderBy(new Expression('rand()'))
->one();
} else {
// don't use any hashtags
$hash=false;
}

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.

// only use a url if it's less than a week old
$url_weekago = time()-7*24*3600;
$url_3daysago = time()-3*24*3600;
// only use a url if it's not been posted in the last 72 hrs
$url= NormUrl::find()
->where('created_at>'.$url_weekago)
->andWhere('last_used<'.$url_3daysago) // handles unused zero case
->orderBy(['id' => SORT_DESC])->one();

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

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.