Advertisement
  1. Code
  2. Twitter

Using the Twitter API to Tweet Repetitive Content

Scroll to top
Read Time: 11 min
This post is part of a series called Building With the Twitter API.
Building With the Twitter API: Analyzing Your Followers
Final product imageFinal product imageFinal 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 AppRepeating Twitter API - Setting up your Twitter AppRepeating 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:

1
<?php
2
  use yii\db\Schema;
3
  use yii\db\Migration;
4
  class m170120_211944_create_table_norm_tweet extends Migration
5
  {
6
    public function up()
7
    {
8
        $tableOptions = null;
9
        if ($this->db->driverName === 'mysql') {
10
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
11
        }
12
13
        $this->createTable('{{%norm_tweet}}', [
14
            'id' => Schema::TYPE_PK,
15
            'tweet' => Schema::TYPE_TEXT.' NOT NULL',
16
            'media_id' => Schema::TYPE_STRING.' NOT NULL',
17
        ], $tableOptions);
18
19
    }
20
21
    public function down()
22
    {
23
24
      $this->dropTable('{{%norm_tweet}}');
25
    }
26
  }

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 GeneratorRepeating Twitter API - Yiis Gii Scaffolding Model GeneratorRepeating Twitter API - Yiis Gii Scaffolding Model Generator

And here's the CRUD generation:

Repeating Twitter API - Yiis Gii Scaffolding CRUD GeneratorRepeating Twitter API - Yiis Gii Scaffolding CRUD GeneratorRepeating 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 UXRepeating Twitter API - The Authoring Tweets UXRepeating 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:

1
<?php
2
  use yii\db\Schema;
3
  use yii\db\Migration;
4
  class m170120_212009_create_table_norm_hash extends Migration
5
  {
6
    public function up()
7
    {
8
        $tableOptions = null;
9
        if ($this->db->driverName === 'mysql') {
10
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
11
        }
12
13
        $this->createTable('{{%norm_hash}}', [
14
            'id' => Schema::TYPE_PK,
15
            'tag' => Schema::TYPE_STRING.' NOT NULL',            
16
        ], $tableOptions);
17
18
    }
19
20
    public function down()
21
    {
22
23
      $this->dropTable('{{%norm_hash}}');
24
    }
25
  }

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 UXRepeating Twitter API - The Authoring Hashtags UXRepeating 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:

1
<?php
2
  use yii\db\Schema;
3
  use yii\db\Migration;
4
  class m170120_211955_create_table_norm_url extends Migration
5
  {
6
    public function up()
7
    {
8
        $tableOptions = null;
9
        if ($this->db->driverName === 'mysql') {
10
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
11
        }
12
13
        $this->createTable('{{%norm_url}}', [
14
            'id' => Schema::TYPE_PK,
15
            'url' => Schema::TYPE_STRING.' NOT NULL',
16
            'last_used' => Schema::TYPE_INTEGER . ' NOT NULL',
17
            'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',
18
            'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',
19
        ], $tableOptions);
20
21
    }
22
23
    public function down()
24
    {
25
26
      $this->dropTable('{{%norm_url}}');
27
    }
28
  }

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 UXRepeating Twitter API - The Adding URLs UXRepeating 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 ChartRepeating Twitter API - Rate Limits ChartRepeating 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:

1
<?php
2
3
namespace frontend\models;
4
5
use Yii;
6
use yii\db\Expression;
7
8
...
9
10
public static function build() {
11
  // pick a random tweet message (any one of them)

12
  $txt = NormTweet::find()
13
  ->orderBy(new Expression('rand()'))
14
  ->one();

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

1
// decide whether to use a hashtag

2
// pick a random # between 0 and 4

3
$useHash = rand(0,4);
4
// if rand# is not 0 but instead 1,2,3 or 4, include hashtag(s)

5
// for less freq use of hash tags, we can change this > 2 or >3

6
if ($useHash>3) {
7
  // so we're now going to decide which and how many hash tags to

8
  // Creator decided to only use one hashtag for right now

9
    $numHash = 1; // rand(1,3);

10
    // select this rand# $numHash randomly from our list

11
    $hash = NormHash::find()
12
    ->orderBy(new Expression('rand()'))
13
    ->one();
14
} else {
15
  // don't use any hashtags

16
  $hash=false;
17
}

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.

1
// only use a url if it's less than a week old

2
  $url_weekago = time()-7*24*3600;
3
  $url_3daysago = time()-3*24*3600;
4
  // only use a url if it's not been posted in the last 72 hrs

5
  $url= NormUrl::find()
6
    ->where('created_at>'.$url_weekago)
7
    ->andWhere('last_used<'.$url_3daysago) // handles unused zero case

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

1
$content = $txt->tweet;
2
  if ($hash!==false) {
3
    $content.=' #'.$hash->tag;
4
  }
5
  // only add URL 1/4 of the time

6
  if (!is_null($url) && rand(1,4) ==1) {
7
    $content.=' '.$url->url;
8
    $url->last_used = time();
9
    $url->update();
10
  }
11
  return $content;

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:

1
# m h  dom mon dow   command

2
*/3 * * * * /var/www/bot/yii daemon/frequent
3
*/15 * * * * /var/www/bot/yii daemon/quarter
4
0 * * * * /var/www/bot/yii daemon/hourly
5
15 1 * * * /var/www/bot/yii daemon/overnight
6
15 3 * * 5 /var/www/bot/yii daemon/weekly

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. 

1
public function actionHourly() {
2
  // every hour

3
  $current_hour = date('G');
4
  if ($current_hour%4) {
5
    // every four hours

6
    echo "Review tweeting plan...";
7
    $dayAgo = time()-24*3600;
8
    $nl= \frontend\models\NormLog::find()
9
      ->orderBy(['id' => SORT_DESC])
10
      ->one();
11
    echo 'created_at: '.$nl->id.' '.$nl->created_at.'...';
12
    echo 'dayago: '.$dayAgo.'...';
13
    if ($nl->created_at<$dayAgo) {
14
      // okay to maybe tweet

15
    ...

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

1
  // every four hours, if 6 is picked from 1-12, we tweet

2
      // 1 in 12 chance 12x in two days

3
      $r = rand(1,12);
4
      if ($r==6) {
5
        \frontend\models\NormTweet::deliver();
6
        echo 'tweet, 6 was picked...';
7
      } else {
8
        echo "do not tweet, 1 in 12 # not picked...";
9
      }
10
    } else {
11
      // never tweet twice in 24 hrs

12
      echo 'do not tweet, not yet 24 hrs...';
13
    }
14
    echo '..made it to end...!';
15
  }
16
      if ($current_hour%6) {
17
      // every six hours

18
    }
19
    }

Delivering the Tweet to the Twitter API

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

1
public static function deliver()
2
{
3
    // post an update

4
    // construct

5
    $content = NormTweet::build();
6
    // tweet it using params for norm acct

7
    $connection = new TwitterOAuth(
8
        Yii::$app->params['norm']['consumer_key'],
9
        Yii::$app->params['norm']['consumer_secret'],
10
        Yii::$app->params['norm']['access_token'],
11
        Yii::$app->params['norm']['access_token_secret']);
12
    $postit = $connection->post("statuses/update",
13
        ["status" => $content]);        
14
    // save it in the log

15
    $nl = new NormLog();
16
    $nl->tweet = $content;
17
    $nl->save();
18
  }
19
}

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

1
$ more params-local.php 
2
<?php
3
return [
4
'norm' => [
5
     'consumer_key' => $config['norm_consumer_key'],
6
     'consumer_secret' => $config['norm_consumer_secret'],
7
     'access_token' => $config['norm_access_token'],
8
     'access_token_secret' => $config['norm_access_token_secret'],
9
     'user_id' => $config['norm_user_id'],
10
   ],
11
12
];

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 GoddessRepeating Twitter API - Tom McFarlin Head Editorial GoddessRepeating 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 TutorialRepeating Twitter API - Bot Tweet Stream from TutorialRepeating 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

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.