<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Konstantin Kovshenin &#187; robotics</title>
	<atom:link href="http://kovshenin.com/tag/robotics/feed/" rel="self" type="application/rss+xml" />
	<link>http://kovshenin.com</link>
	<description>WordPress, Automattic and Open Source</description>
	<lastBuildDate>Mon, 21 May 2012 15:59:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Robots Are Doing Better Than Humans on Twitter</title>
		<link>http://kovshenin.com/2010/robots-are-doing-better-than-humans-on-twitter/</link>
		<comments>http://kovshenin.com/2010/robots-are-doing-better-than-humans-on-twitter/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 07:10:48 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[twitter]]></category>
		<category><![CDATA[robotics]]></category>
		<category><![CDATA[social media]]></category>
		<category><![CDATA[twibots]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=2046</guid>
		<description><![CDATA[This is all about an experiment I started back in July 2009, called TwiBots. Initially it was supposed to be a simple 24/7 online tweep (Michael Davis) saying a bunch of stuff randomly. But then we (Michael and I hehe) started picking out certain topics, feeding content from certain RSS feeds, filtering all content by [...]]]></description>
			<content:encoded><![CDATA[<p>This is all about an experiment I started back in July 2009, called <a href="http://twibots.com">TwiBots</a>. Initially it was supposed to be a simple 24/7 online tweep (Michael Davis) saying a bunch of stuff randomly. But then we (Michael and I hehe) started picking out certain topics, feeding content from certain RSS feeds, filtering all content by keywords and regular expressions.</p>
<p>When the new Retweet API came along, I wrote an RT module which uses the Twitter Search API to find relevant tweets and users, then retweets those messages or just somehow interacts with a user. As soon as the Twitter Lists API was announced, I started working on the Lists module, which eventually became a simple &#8220;sorting-the-tweeps&#8221; based on their keywords &#8211; web design, design, wordpress, etc. Soon enough, I found out that Lists couldn&#8217;t contain more than 500 members (although some glitch made 501 possible). Web design 2, wordpress 2, etc wouldn&#8217;t be as fancy. I also tried building a conversation list of tweeps that by any means talked to the robot, but then again, the 500 limit broke all my hopes ;)</p>
<p>There were other modules which I worked on really hard, such as RThx module or Random Buzz, DM Control. Some of them worked, some of them were turned off after a few days (yes, you guessed it &#8211; Random Buzz, that really made some noize ;)</p>
<p>So, what did I achieve? Me &#8211; nothing. Michael did though, in 6 months he went up to 4500+ followers, while following a little more than 200 people himself, has been featured in ~ 250 lists, sent ~ 55,000 tweets and retweets all based on four keywords (or hashtags) &#8211; design, web design, wordpress and jquery. Built 4 lists based on these keywords, 500 members in each. Total list followers is a little less than 150 (which is quite good actually).</p>
<p>For comparison take a look at my account &#8211; (<a href="http://twitter.com/kovshenin">@kovshenin</a>), in a little bit more than a year I got ~ 1700 followers. It took Michael a couple of months to reach that. The chart below illustrates the followers growth during the last three months. Human (me &#8211; blue) vs Robot (Michael &#8211; red). Yeah, I added a new module in mid December ;)</p>
<p><a href="http://kovshenin.com/files/2010/02/twibots_chart.png"><img src="http://kovshenin.com/files/2010/02/twibots_chart.png" alt="Twibots Chart: Robot vs. Human" width="692" height="240" class="aligncenter size-full wp-image-2058" /></a></p>
<p>I manually logged into Michael&#8217;s account recently to check out how he&#8217;s doing, and I was kind of surprised to see that people really are talking to the guy, thanking him for retweets, asking him for further reads, wishing him a great day and handing over some coffee. Michael doesn&#8217;t usually reply to these and he&#8217;s a little bit shy sometimes, besides, he never drinks coffee ;)</p>
<p>A few days ago I decided to give Michael a rest, so tuned his backend to a new Twitter account with a few different settings, especially in timing. I&#8217;ll be switching to other keywords and feeds in the next few weeks. So let&#8217;s see if he&#8217;s as good as Michael, or perhaps better? ;)</p>
<p>I&#8217;m not sure what I&#8217;ll do with Michael&#8217;s account. TweetValue said it&#8217;s worth over $5k &#8230; anyone? ;) Or should I just throw it away.. Or run a contest for his password? &#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/robots-are-doing-better-than-humans-on-twitter/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Twitter Robot in PHP: Twibots Draft</title>
		<link>http://kovshenin.com/2010/twitter-robot-in-php-twibots-draft/</link>
		<comments>http://kovshenin.com/2010/twitter-robot-in-php-twibots-draft/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 19:19:49 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[twitter]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[robotics]]></category>
		<category><![CDATA[twibots]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=1965</guid>
		<description><![CDATA[As I promised quite some time ago, I&#8217;m putting out a draft of the Twitter Robot I wrote. Make sure you read Create Your Own Automated Twitter Robot in PHP before going on. The current functionality is as follows: Tweets around the clock Tweets from RSS feeds, supporting prefix and postfix text (for adding hashtags) [...]]]></description>
			<content:encoded><![CDATA[<p>As I promised quite some time ago, I&#8217;m putting out a draft of the Twitter Robot I wrote. Make sure you read <a href="http://kovshenin.com/2009/create-your-own-automated-twitter-robot-in-php/">Create Your Own Automated Twitter Robot in PHP</a> before going on. The current functionality is as follows:</p>
<ul>
<li>Tweets around the clock</li>
<li>Tweets from RSS feeds, supporting prefix and postfix text (for adding hashtags)</li>
<li>Retweet via the Twitter Search API and build conversation lists</li>
<li>Shoot random sentences at users who mention you, thank them for retweets</li>
<li>Control your robot via your own Twitter account by sending him direct messages</li>
<li>All this is Twitter OAuth powered, no password required</li>
<li>Such robots are called <a href="http://twibots.com">Twibots</a></li>
</ul>
<p>Now, before downloading the code, I have to warn you that it&#8217;s completely unorganized. The code is horrible, comments are awkward, the database being used is SQLite (just for the fun of it) and it&#8217;s very very glitchy. Be prepared for Twitter suspending your account for &#8216;strange activity&#8217; and use this at your own risk, don&#8217;t run here blaming me for that ;) I also suggest you&#8217;d contact Twitter to get your IP addresses and Twitter account white-listed before you start, especially if you plan to tweet very often (which I wouldn&#8217;t recommend). Use this at your own risk, and please keep my copyrights and preferably the OAuth application IDs.</p>
<p>Download: <a href="http://kovshenin.com/core/wp-content/uploads/2010/01/twibots-0.1.zip">here</a> (version 0.1)</p>
<p>Operation Instructions.. To say the truth it&#8217;s pretty tough, no web interface, not buttons, no config files. There are a bunch of files there, some of them useless. There&#8217;s the Snoopy class for reading and parsing RSS, there&#8217;s the Twitter OAuth class, and two core php files &#8211; cron.php and oauth.php. Open up cron.php, there are some comments and examples there. Make sure you get your own bit.ly API key and secret. Also make sure you get a connection with the twibots.sqlite database which has a couple of empty tables. Those will be used for tokens and dump data for unrepeated tweets.</p>
<p>Once you&#8217;re done configuring, use the command-line php in order to make it work. It goes something like this:</p>
<pre># php cron.php oauth register
# Please browse to https://twitter.com/...
# php cron.php oauth validate 123465
# Authentication successful, greetings @ev ;)

# php cron.php random
# tweeting a random RSS feed ...

# php cron.php reply
# sending replies...

# php cron.php dm
# reading direct messages

# php cron.php retweet
# retweeting...
</pre>
<p>You&#8217;ll have to put that in your crontab file and launch by schedule. Don&#8217;t run them too often though, as Twitter doesn&#8217;t like flooding, especially from newly created accounts. Any questions or suggestions are welcome in the comments below, but please, don&#8217;t tell me the code is horrible, I know it is, and I wouldn&#8217;t have posted it if you didn&#8217;t ask ;) Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2010/twitter-robot-in-php-twibots-draft/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Automated Twitter Bot in PHP: Remote Control</title>
		<link>http://kovshenin.com/2009/automated-twitter-bot-in-php-remote-control/</link>
		<comments>http://kovshenin.com/2009/automated-twitter-bot-in-php-remote-control/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 10:21:14 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[robotics]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[twitterapi]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=1423</guid>
		<description><![CDATA[Hope you&#8217;ve all read the first part of this series &#8211; Create Your Own Automated Twitter Robot in PHP and got your own prototype up and running. Today we&#8217;ll be adding a remote control feature to our robot. It&#8217;ll be working through direct messages and running in crontab every 5 minutes or so. You can [...]]]></description>
			<content:encoded><![CDATA[<p>Hope you&#8217;ve all read the first part of this series &#8211; <a href="http://kovshenin.com/2009/create-your-own-automated-twitter-robot-in-php/">Create Your Own Automated Twitter Robot in PHP</a> and got your own prototype up and running. Today we&#8217;ll be adding a remote control feature to our robot. It&#8217;ll be working through direct messages and running in crontab every 5 minutes or so. You can extend this as far as you want (adding retweet capabilities, follow/unfollow, direct messaging other people, etc) but we&#8217;ll stick to simple status updating in this post, might cover the others later on.</p>
<p>So, direct messaging the robot&#8217;s twitter account with the text &#8220;update status text&#8221; would make him tweet &#8220;status text&#8221; to the public timeline. Remember we had three branches of actions &#8211; feed, reply and rthx? Let&#8217;s add a fourth one and call it dm. This branch will simply scan through the account&#8217;s latest direct messages, find those sent by you and tweet them out loud. Again, as I said in the first tutorial, this is simply a prototype, just to get things up and running. You&#8217;ll have to polish this off for actual use and yeah, storing the access keys, dm_since_id, etc on disk is not such a good idea, you should probably use the database. Here&#8217;s the code:</p>
<pre>// The id of the latest read direct message will be stored in
// a file called dm_since_id, just like mentions_since_id
// in the previous examples
$since_id = @file_get_contents("dm_since_id", true);
if ($since_id &gt; 0) { }
else { $since_id = 1; }

// Retrieve the direct messages into $dms and parse the xml string
$dms = $oauth-&gt;OAuthRequest("http://twitter.com/direct_messages.xml" ,
	array("count" =&gt; 10, "since_id" =&gt;  $since_id), "GET");
$dms = simplexml_load_string($dms);

// If it's valid read the latest id and store into dm_since_id
if (count($dms))
{
	$last_id = ($dms-&gt;direct_message[0]-&gt;id &gt; $since_id) ?
		$dms-&gt;direct_message[0]-&gt;id : $since_id;
	file_put_contents("dm_since_id", (string)$last_id, FILE_USE_INCLUDE_PATH);
}

// Loop through the messages
foreach ($dms-&gt;direct_message as $dm)
{
	// Make sure you're the sender
	$sender = $dm-&gt;sender-&gt;screen_name;
	if ($sender == "kovshenin")
	{
		// What should we do
		if (strtolower(substr($dm-&gt;text, 0, 7)) == "update ")
		{
			// Construct the message, tweet and wait a few seconds
			$message = substr($dm-&gt;text, 7);
			$oauth-&gt;OAuthRequest('https://twitter.com/statuses/update.xml',
				array('status' =&gt; $message), 'POST');
			echo "Tweeting: " . $message;
			sleep(rand(5,30));
		}

		// Add more actions here
	}
}
</pre>
<p>Read through the comments in the code and you should be able to get the idea. The direct messages Twitter API method is documented <a href="http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-direct_messages">here</a>. Test it out a few times through your SSH client by sending a direct message to your robot with the text &#8220;update Updating my status&#8221; or whatever, then run:</p>
<pre># php robot.php dm
Tweeting: Updating my status
</pre>
<p>If everything works fine you might as well add the action to your cron, say 5 minutes:</p>
<pre>*/5 * * * * php /home/youruser/twibots/robot.php dm
</pre>
<p>And done! Your robot is now remote controlled. A few suggestions to more advanced remote controlled operations would be:</p>
<ul>
<li>Retweet capabilities by regular expression</li>
<li>Adding and removing feed sources, prefixes and postfixes</li>
<li>Turning on and off other operations (feeding, replying, rthxing)</li>
<li>Adding people to &#8220;reply ignore lists&#8221; (ones that talk to your robot too much)</li>
<li>Adding people to &#8220;rthx ignore lists&#8221; (ones that retweet too much, twitterfeed for instance)</li>
</ul>
<p>I think that&#8217;s enough for a start, oh and please don&#8217;t build dumb and annoying spammish robots. Stick to intelligent, smart <a href="http://twibots.com">twibots</a>! ;)</p>
<p>Upd. Continued: <a href="http://kovshenin.com/2010/twitter-robot-in-php-twibots-draft/">Twitter Robot in PHP: Twibots Draft</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2009/automated-twitter-bot-in-php-remote-control/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Create Your Own Automated Twitter Robot in PHP</title>
		<link>http://kovshenin.com/2009/create-your-own-automated-twitter-robot-in-php/</link>
		<comments>http://kovshenin.com/2009/create-your-own-automated-twitter-robot-in-php/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 16:39:42 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[robotics]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[twitterapi]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=1376</guid>
		<description><![CDATA[The ultimate guide to creating your own personalized twitterfeed clone! Kidding&#8230; Actualy this is just a mockup, a simple prototype, which is way too fresh for any actual use. We&#8217;ll take this forward step by step. I&#8217;m not going to give out all my sources but I&#8217;ll guide you through authentication, rss lookup, parsing, thanking [...]]]></description>
			<content:encoded><![CDATA[<p>The ultimate guide to creating your own personalized twitterfeed clone! Kidding&#8230; Actualy this is just a mockup, a simple prototype, which is way too fresh for any actual use. We&#8217;ll take this forward step by step. I&#8217;m not going to give out all my sources but I&#8217;ll guide you through authentication, rss lookup, parsing, thanking for retweets, and shooting random stuff at people that mention your robot.</p>
<p>Here&#8217;s a brief list of features we will implement:</p>
<ul>
<li>Runs in console, no HTTP access</li>
<li>Authentication via OAuth, tweeting via OAuth</li>
<li>RSS lookup, parsing, forming tweets in bound of 140 characters including a postfix (hashtag or RT)</li>
<li>Tweeting &#8216;thank you for retweeting&#8217; to users that retweet the robot</li>
<li>Following people that retweet the robot</li>
<li>Acting strange on users that mention the robot</li>
</ul>
<p>All this is going to be setup on a linux box running crond and acting every 15 minutes or so. Are you ready? Let&#8217;s do it!</p>
<p><span id="more-1376"></span></p>
<h2>Registering an App at Twitter OAuth Clients</h2>
<p>This is mandatory if we&#8217;re using OAuth (don&#8217;t go with basic authentication because Twitter will be closing that down sooner or later, plus we&#8217;re making a personalized robot, so the &#8216;from&#8217; string in the tweets is a must). Browse to the <a href="http://twitter.com/oauth_clients">Twitter OAuth Clients</a> page (assuming you&#8217;re logged into Twitter) and register a new application. Make sure you pick <strong>Read and Write access</strong> as our robot will use POST calls to update its status. Also note that we&#8217;re preventing all HTTP access to the robot control, thus we&#8217;ll be using PIN based OAuth, so make sure you pick <strong>Client application</strong> and not web.</p>
<p>Now, don&#8217;t lose your <strong>Consumer key</strong> and <strong>Consumer secret</strong> &#8211; we&#8217;ll be using those in our app for identity. Don&#8217;t give them out to anybody you don&#8217;t trust unless you&#8217;d like people tweeting &#8220;from YourApp&#8221;. Note that you can ask Twitter to reset the values for you in case somebody got unwanted access to them. The request and access URLs are not important, they&#8217;re the same for everyone.</p>
<h2>Setting up the Environment</h2>
<p>What environment? Umm, we&#8217;ll be using <a href="http://magpierss.sourceforge.net/">Magpie RSS</a> to parse RSS feeds, <a href="http://ru2.php.net/curl">Curl</a> to access the Twitter API and the <a href="http://github.com/abraham/twitteroauth">Twitter OAuth Class</a> by Abraham Williams (we used this a bunch of times in my earlier postings) which is dependent on the Curl functions. Make sure you install everything right &#8211; the magpie functions work, curl functions are available and the Twitter OAuth class is defined.</p>
<p>What you also need is a bit.ly login and API key, go get yours <a href="http://bit.ly/account/register">somewhere here</a>. Feel free to use a different URL shortener if you feel like, but we&#8217;ll stick to bit.ly.</p>
<h2>Authentication: Storing the Access Tokens</h2>
<p>We&#8217;ll divide our application in two parts, one will be running every 15 minutes, the second one will run once for authentication. Create a new PHP file, call it oauth.php or whatever and also make sure you got your consumer key and secret next to you and visible by oauth.php. It&#8217;s up to you where to keep them, I like storing them in a config.php which I include/require in auth.php and robot.php.</p>
<p><img src="http://kovshenin.com/files/2009/10/twitter_robot_chart.png" alt="twitter_robot_chart" width="680" height="500" class="aligncenter size-full wp-image-1399" /></p>
<p>If you haven&#8217;t read my previous articles about <a href="http://kovshenin.com/2009/automatic-tweet-oauth/">Automated Serverside Tweeting Using OAuth</a> and <a href="http://kovshenin.com/2009/twitter-api-pin-based-oauth-php/">PIN-based OAuth Using PHP</a> then make sure you do, because I wouldn&#8217;t like to go through the same code again. What&#8217;s important here is that oauth.php will run on its own and will accept parameters via command line. Like this:</p>
<pre># php oauth.php register
Request tokens aquired, proceed to this link: https://twitter.com/oauth/authorize?oauth_token=whatever

# php oauth.php validate 123456
Access tokens stored, identified as @twittername
</pre>
<p>So after you&#8217;ve validated your OAuth session, access tokens are stored somewhere on disk, I just call my files access_token and access_token_secret. If the files are present, then your robot will be able to tweet, otherwise ask for registration. I hope everything&#8217;s clear enough here. The main point of this file is to get your access tokens written to disk.</p>
<h2>Feeding Twitter from RSS</h2>
<p>We need to distinguish different actions which the robot is supposed to do, through an action parameter passed by the command line. I leave it up to you, but make sure your control flows similar to this:</p>
<pre>$action = $argv[1];
if ($action == "feed")
{
	$feed_name = $argv[2];
	// Read the feeds, tweet the feeds
}
elseif ($action == "rthx")
{
	// Read the names, tweet the names!
}
elseif ($action == "reply")
{
	// Read the names, umm.. Say something!
}
</pre>
<p>So the robot control will look something like this:</p>
<pre># php robot.php feed wordpress
Feeding my public timeline with wordpress articles
# php robot.php rthx
Thanking peeps for the retweets and following them all
# php robot.php reply
Writing strange stuff to peeps that mentioned me
</pre>
<p>Good, now let&#8217;s assume we&#8217;re in the feed action. Define an associative array of feeds:</p>
<pre>$feeds = array(
	"wordpress" =&gt; array("url" =&gt; "http://wordpress.org/development/feed/",
		"postfix" =&gt; "#wordpress"),
	"mashable" =&gt; array("url" =&gt; "http://feeds2.feedburner.com/Mashable",
		"postfix" =&gt; "via (@mashable)"),
);
</pre>
<p>Two is enough for starting. We&#8217;ll make our robot tweet #wordpress tweets and read out mashable&#8217;s website and tweet headlines via @mashable. Make sure that you&#8217;ve already written a check for access_token and access_token_secret and gave out an error message if they didn&#8217;t exist or were expired. We cannot tweet without those, remember? I&#8217;m assuming you&#8217;ve stored them into $access_token and $access_token_secret respectively and initialized the $oauth object:</p>
<pre>$oauth = new TwitterOAuth($oauth_consumer_key, $oauth_consumer_secret,
	$access_token, $access_token_secret);
</pre>
<p>The second parameter (&#8220;wordpress&#8221; in the control above example) would be stored into a $feed_name variable. From there on we&#8217;ll go with code:</p>
<pre>// Store the feed settings into $feed
$feed = $feeds[$feed_name];

// Fetch the feed and store the prefix
$rss = fetch_rss($feed["url"]);
$postfix = $feed["postfix"];

// Loop through the feed items
foreach ($rss-&gt;items as $item)
{
	// All simple enough here
	$title = trim($item["title"]);
	$url = $item["link"];

	// Let's make sure our feeds are in English, allow spaces and punctuation
	if (ereg('^[[:alnum:][:blank:][:punct:]]+$', $title))
	{
		// Escape the URL for bit.ly shortening and then shorten the link
		// This is the place where you have to use your bit.ly login
		// And the API key
		$url_escaped = urlencode($url);
		$bitly_url = "http://api.bit.ly/shorten?version=2.0.1";
		$bitly_url .= "&amp;longUrl=$url_escaped";
		$bitly_url .= "&amp;login=$bitly_login&amp;apiKey=$bitly_key";

		$shortened_url = json_decode(file_get_contents($bitly_url));

		// If everything went okay, go on
		if ($shortened_url-&gt;errorCode == 0)
		{
			// Retrieve the shortened url from the json object
			foreach ($shortened_url-&gt;results as $key =&gt; $value)
				$shorturl = $value-&gt;shortUrl;

			// Form a new message from the short URL and the postfix
			$message = " $shorturl $postfix";
			$length = strlen($message);

			// We should trim down the title if it's too long
			// So that our tweets are 120 characters or less
			if (strlen($title) &gt; 120-$length)
				$shorttitle = substr($title, 0, 117-$length) . "...";
			else
				$shorttitle = $title;

			// Add the title to the message
			$message = $shorttitle.$message;

			// Post the message to Twitter
			$oauth-&gt;OAuthRequest('https://twitter.com/statuses/update.xml',
				array('status' =&gt; $message), 'POST');

			// Wait a couple of mintes before the next tweet
			// Don't try and flood Twitter, remember, you have
			// Only 150 API calls per hour, use them wisely.
			sleep(rand(60,120));
		}
	}
}
</pre>
<p>Read carefully through the comments, you should be able to understand what we&#8217;re doing here. This is the RSS part, (almost) all clean and shiny. Proceed to the retweets part.</p>
<h2>Automatically Thank Your Retweeters and&#8230; Follow them!</h2>
<p>Be very very careful here as we don&#8217;t want to thank the retweeters too much. You&#8217;ll need to think of a mechanism to store the id of the latest retweet that you already thanked and use it in the since_id parameter when calling the Twitter API. I&#8217;ll leave this part up to you, but in general the code should look something like this:</p>
<pre>// Read the since_id from a file
$since_id = @file_get_contents("retweets_since_id");
if ($since_id &gt; 0) { }
else { $since_id = 1; }

// Send the Twitter API request for the latest 50 mentions
// That were posted after the since_id parameter we read earlier
$mentions = $oauth-&gt;OAuthRequest("http://twitter.com/statuses/mentions.xml" ,
	array("count" =&gt; 50, "since_id" =&gt;  $since_id), "GET");

// Make the XML an object
$mentions = simplexml_load_string($mentions);

// Setup an array which will contain users to retweet and follow
$users_to_rthx = array();

// Read the last tweet's id and store it into the retweets_since_id file
$last_id = ($mentions-&gt;status[0]-&gt;id &gt; $since_id) ? $mentions-&gt;status[0]-&gt;id : $since_id;
file_put_contents("retweets_since_id", (string)$last_id);

// Loop through the tweets
foreach ($mentions-&gt;status as $status)
{
	// Let's see if somebody retweeted you.
	// Err, remember to replace @yourname
	if (strpos(strtolower($status-&gt;text), "rt @yourname")
		|| strpos(strtolower($status-&gt;text), "via @yourname"))
	{
		// Add the guy to the retweeters array
		$users_to_rthx[] = $status-&gt;user-&gt;screen_name;
	}
}

// Remove duplicates (we don't thank somebody twice in a tweet)
$users_to_rthx = array_unique($users_to_rthx);

// Setup the tweet prefix and initialize the mentions variable
// The tweet_prefix is just in case ;)
$tweet_prefix = "Thanks for the retweets! ";
$tweet_mentions = "";
$tweet_prefix = "";

// Loop through the retweeters popping variables out of the array
while ($mention_this_guy = array_pop($users_to_rthx))
{
	// If the popped guy fits into our brand new tweet, add him
	if (strlen($tweet_prefix . $tweet_mentions .
		$tweet_postfix . "@".$mention_this_guy) &lt; 140)
	{
		$tweet_mentions .= "@".$mention_this_guy . " ";

		// Send the friendhips create (follow) request to the API
		echo "Following: " . $mention_this_guy;
		$oauth-&gt;oAuthRequest("https://twitter.com/friendships/create/" .
			$mention_this_guy . ".xml",	array(), "POST");
	}
	// If he doesn't push him back into the variable, tweet and reset
	// The $tweet_mentions variable for the other retweeters
	else
	{
		array_push($users_to_rthx, $mention_this_guy);

		// Format the message and output to screen (for debugging)
		$message = $tweet_prefix . trim($tweet_mentions) . $tweet_postfix;
		echo "Tweeting: " . $message . " (" . strlen($message) . ") n";

		// Send the status update request to the Twitter API
		$oauth-&gt;OAuthRequest('https://twitter.com/statuses/update.xml',
			array('status' =&gt; $message), 'POST');

		// Wait a few seconds before the next round
		sleep(rand(5,30));

		// Reset
		$tweet_mentions = "";
	}
}

// If we've got something left in the mentions, we need to tweet that
if (strlen($tweet_mentions) &gt; 0)
{
	$message = $tweet_prefix . trim($tweet_mentions) . $tweet_postfix;
	echo "Tweeting: " . $message . " (" . strlen($message) . ") n";

	$oauth-&gt;OAuthRequest('https://twitter.com/statuses/update.xml',
		array('status' =&gt; $message), 'POST');
	sleep(rand(5,30));

	$tweet_mentions = "";
}
</pre>
<p>The comments explain it all.</p>
<h2>Did Somebody Say Anything? Random Replies</h2>
<p>This is the fun part. Whenever somebody mentions your name (and it is not a retweet), we send them a strange message mentioning their name. The code looks exactly like the retweeters thank code above, except that we don&#8217;t search for &#8220;rt @yourname&#8221; and &#8220;via @yourname&#8221;, instead we work with ones that don&#8217;t contain &#8220;rt&#8221; and &#8220;via&#8221; at all.</p>
<p>Don&#8217;t forget to store the replies_since_id too as we don&#8217;t want to message users multiple times (infinite number of times actually) because they&#8217;ll block the frustrating robot. Here&#8217;s the while loop that actually tweets. In this part the names are stored in the $users_to_reply array and goes like this:</p>
<pre>// The loop
while ($mention_this_guy = array_pop($users_to_reply))
{
	// Some random quotes ;) You can add your own
	$random_quotes = array(
		"Wha?", "Interesting ...", "Affirmative sir!", "Hmm, makes me think ..",
		"So you really think I'm not human? Well.. Umm.. *sigh*"
	);

	// Format the message and tweet a random quote
	$message = "@".$mention_this_guy . " " .
		$random_quotes[array_rand($random_quotes)];

	echo "Tweeting: " . $message . " (" . strlen($message) . ") n";
	$oauth-&gt;OAuthRequest('https://twitter.com/statuses/update.xml',
		array('status' =&gt; $message), 'POST');

	// Wait a little
	sleep(rand(5,30));
}
</pre>
<p>That&#8217;s about it. The last part is adding the whole stuff to crontab for automated work. Open up /etc/crontab and add a few lines:</p>
<pre># Feed mashable and wordpress tweets once every 15-16 minutes
*/15 * * * * php /home/youruser/twibots/robot.php feed wordpress
*/16 * * * * php /home/youruser/twibots/robot.php feed mashable

# Thank and follow the retweeters once every half an hour
*/30 * * * * php /home/youruser/twibots/robot.php rthx

# Reply to people hourly
01 * * * * php /home/youruser/twibots/robot.php reply
</pre>
<p>Restart your cron daemon and voila! I&#8217;m used to having full control over my virtual private server so if you use simple shared hosting you should access the crontab (also called cron jobs) via your CPanel. Don&#8217;t forget to actually write the php command because .php files aren&#8217;t actually executable by linux. Also note that my files are stored in a non-accessible by apache part of the hard drive, so omit putting them into public_html, www or whatever. If you do though, make sure you define a &#8220;deny from all&#8221; rule in .htaccess. We don&#8217;t want other people messing with our newly born robot. Robots, robots, robots&#8230; What should we call them? <a href="http://twibots.com">Twibots</a>? ;)</p>
<p>Upd. Continued: <a href="http://kovshenin.com/2009/automated-twitter-bot-in-php-remote-control/">Automated Twitter Bot in PHP: Remote Control</a> and <a href="http://kovshenin.com/2010/twitter-robot-in-php-twibots-draft/">Twitter Robot in PHP: Twibots Draft</a></p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2009/create-your-own-automated-twitter-robot-in-php/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>More on Robotics: AR-600 Moscow (March 2009)</title>
		<link>http://kovshenin.com/2009/robotics-ar-600-moscow-march-2009/</link>
		<comments>http://kovshenin.com/2009/robotics-ar-600-moscow-march-2009/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 12:23:50 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[robotics]]></category>
		<category><![CDATA[ar-600]]></category>
		<category><![CDATA[exhibitions]]></category>
		<category><![CDATA[russia]]></category>
		<category><![CDATA[videos]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=655</guid>
		<description><![CDATA[Y&#8217;all know Russian, don&#8217;t you? Kidding.. The guys in the video talk about the development of robotics in Russia, why people are afraid of robots and why they shouldn&#8217;t be (the three laws of robotics by Asimov). This &#8220;thug&#8221; (AR-600) can now make some steps back and forth then side to side (though they don&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p><iframe width="580" height="326" src="http://www.youtube.com/embed/MghUpIbfc_U?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Y&#8217;all know Russian, don&#8217;t you? Kidding.. The guys in the video talk about the development of robotics in Russia, why people are afraid of robots and why they shouldn&#8217;t be (the three laws of robotics by Asimov). This &#8220;thug&#8221; (AR-600) can now make some steps back and forth then side to side (though they don&#8217;t show them on this exhibition, cause it requires a special surface). He can also recognize people&#8217;s faces and greet them individually. Oh, and almost forgot, he can read numbers from papers and add or subtract them! Smart ass! ;)</p>
<p>P.S. Available in HQ (click the play button, then HQ next to the full-screen button)</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2009/robotics-ar-600-moscow-march-2009/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AR-600 in Moscow</title>
		<link>http://kovshenin.com/2009/ar600-moscow/</link>
		<comments>http://kovshenin.com/2009/ar600-moscow/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 09:38:39 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[robotics]]></category>
		<category><![CDATA[12seconds.tv]]></category>
		<category><![CDATA[moscow]]></category>
		<category><![CDATA[photos]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=258</guid>
		<description><![CDATA[Okay, these are kind of old from an exhibition in Moscow in like December 2008. Previously hosted by Flickr, and today I decided to have duplicates here, &#8217;cause Flickr will not display more than 200 photos from my photostream, so here&#8217;s probably a safer place (unless my hosting provider messes up). This is an android [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, these are kind of old from an exhibition in Moscow in like December 2008. Previously hosted by Flickr, and today I decided to have duplicates here, &#8217;cause Flickr will not display more than 200 photos from my photostream, so here&#8217;s probably a safer place (unless my hosting provider messes up). This is an android robot, similar to Honda ASIMO. It&#8217;s called i-Van and the model name&#8217;s AR-600. It&#8217;s still being developed by Android Robotics Corp. in Russia, so I&#8217;ll get back to you with the improvements in a couple of months.</p>

<p>Sorry for the quality, I hadn&#8217;t had a camera back then so I caught those on my Nokia cellphone. It was fun though, our Russian president Dmitry Medvedev shook hands with the android, and then the newspapers here posted out some good photos of them both, stating that Dmitry&#8217;s standing on the right, in case somebody got confused LOL. BTW that&#8217;s me in the orange shirt. It was almost midnight and we had a bunch of technical problems, that&#8217;s why I looked so sad.</p>
<p>P.S. I&#8217;m on <a href="http://12seconds.tv/channel/kovshenin">12seconds.tv, follow me</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2009/ar600-moscow/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Robotics in Russia</title>
		<link>http://kovshenin.com/2008/robotics-in-russia/</link>
		<comments>http://kovshenin.com/2008/robotics-in-russia/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 10:03:42 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[robotics]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=128</guid>
		<description><![CDATA[Hey, just had another exhibition here in Moscow, and we&#8217;ve finally presented the new AR-600 android robot. It looks a lot like ASIMO, but it&#8217;s totally new. It doesn&#8217;t know how to walk yet, but that&#8217;s only a matter of time &#8211; the mechanics are strong enough to hold that 100 kg robot on one [...]]]></description>
			<content:encoded><![CDATA[<p>Hey, just had another exhibition here in Moscow, and we&#8217;ve finally presented the new AR-600 android robot. It looks a lot like ASIMO, but it&#8217;s totally new. It doesn&#8217;t know how to walk yet, but that&#8217;s only a matter of time &#8211; the mechanics are strong enough to hold that 100 kg robot on one leg!</p>
<p>If anybody&#8217;s interested, be sure to check out the photos and videos right here: <a href="http://androidov.net/">androidov.net</a>. The blog&#8217;s in russian, but you should get the most of it ;)</p>
<p>Regards.</p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2008/robotics-in-russia/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Robot Playing Ping Pong!</title>
		<link>http://kovshenin.com/2008/a-robot-playing-ping-pong/</link>
		<comments>http://kovshenin.com/2008/a-robot-playing-ping-pong/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 18:39:28 +0000</pubDate>
		<dc:creator>Konstantin Kovshenin</dc:creator>
				<category><![CDATA[robotics]]></category>
		<category><![CDATA[artificial intelligence]]></category>
		<category><![CDATA[games]]></category>

		<guid isPermaLink="false">http://kovshenin.com/?p=112</guid>
		<description><![CDATA[Woah. This is wicked! I wrote a ping pong game that had an AI player about two or three years ago in C++, the algorithm isn&#8217;t very difficult to figure out, but here&#8217;s one built-into a robot! RoboCup claims that they&#8217;ll win the international soccer championship by 2050, and I think table tennis will be [...]]]></description>
			<content:encoded><![CDATA[<p>Woah. This is wicked! I wrote a ping pong game that had an AI player about two or three years ago in C++, the algorithm isn&#8217;t very difficult to figure out, but here&#8217;s one built-into a robot! RoboCup claims that they&#8217;ll win the international soccer championship by 2050, and I think table tennis will be much easier (because japanese are the best in robotics AND table tennis ;)</p>
<p><iframe width="580" height="435" src="http://www.youtube.com/embed/eH2n-7b0UIs?fs=1&#038;feature=oembed" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://kovshenin.com/2008/a-robot-playing-ping-pong/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

