Expanding On TwitterBot – Keep Track of Used Tweets
Now that you know how to make a Twitter bot you may be looking to add some more functionality to it. One problem you may run into after some time is that you keep recycling or re-sending the same messages to Twitter. For example, you have 100 facts about your self and you don’t want any of them to appear twice but you want everything to be automated. How do you do this? Easy, just add a status field to your `tweets` table.
Step 1 – Add the field.
To add the field you can use the simple graphical interface in phpMyAdmin or use the SQL tab (or alternative) to run this code:
ALTER TABLE `tweets` ADD `status` VARCHAR(10) NOT NULL;
What this will do is create a new field in your tweets table with 10 characters of space for that messages status. We will be setting them all to “ready”, and later changing our PHP code to update them as we go along.
Step 2 – Set all tweets to ready.
After you have the new field added to your database you will need to set all of the tweets you want to use to ‘ready’ status. In this example, only tweets with the status set to ‘ready’ can be randomly selected.
While inside of phpMyAdmin (SQL tab) or what ever tool you use, go to the page that allows you to run MySQL commands on your tables and enter this code:
UPDATE `tweets` SET `status` = ‘ready’
Alternatively you can pick which ones you want to use if you already have some tweets that you wish not to recycle. This SQL command will update every tweet/message you have in your database so use it wisely.
Step 3 – Modify PHP to update tweet status in database.
Our third an final step is to modify the php script so it will change the status of every used tweet. To do this, we will make a few simple changes to our original code.
First off, we have to change the MySQL query so it will only pull random messages with “ready” status:
For our second change, we will need to get the ID number of the tweet from the database and pass it to the sendTweet() function we will modify in a moment:
$tweet = “$row[tweet]“;
$tweetID = “$row[id]“;
sendTweet($tweet, $tweetID);
}
Then we modify our sendTweet() function to handle the tweet’s ID, and then change its status if the message is successfully sent to Twitter:
$username = ‘TWITTER-USER-NAME’;
$password = ‘TWITTER-PASS’;
$url = ‘http://twitter.com/statuses/update.xml’;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, “$url”);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$msg”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)) {
echo ‘fail’;
} else {
echo ’success’;
mysql_query(“UPDATE `tweets` SET `status` = ‘used’ WHERE id = ‘$idoftweet’”);
}
Now it’s all ready to role. Now every time a message is pulled from the database and sent to Twitter (successfully) it will have it’s status changed to “used” taking it out of the possible messages to send. Just upload your new php file, re-activate your CRON job and have some fun.
