Sei sulla pagina 1di 10

Create Your Own Automated Twitter Robot in PHP

This is just a mockup, a simple prototype, which is way too fresh for any actual use.

Steps are as follows:

1) Authentication

2) RSS lookup

3) Parsing

4) Thanking for retweets

5) Shooting random stuff at people that mention your robot.

Create a Tweeter feed Clone in PHP

Platform: - All this is going to be setup on a linux box running crond and acting every 15
minutes or so

Here’s a brief list of features we will implement:

* Runs in console, no HTTP access

* Authentication via OAuth, tweeting via OAuth

* RSS lookup, parsing, forming tweets in bound of 140 characters including a postfix
(hashtag or RT)

* Tweeting ‘thank you for retweeting’ to users that retweet the robot

* Following people that retweet the robot

* Acting strange on users that mention the robot


Registering an App at Twitter OAuth Clients

This is mandatory if we’re using OAuth (don’t go with basic authentication because
Twitter will be closing that down sooner or later, plus we’re making a personalized
robot, so the ‘from’ string in the tweets is a must). Browse to the Twitter OAuth
Clients page (assuming you’re logged into Twitter) and register a new application.
Make sure you pick Read and Write access as our robot will use POST calls to
update its status. Also note that we’re preventing all HTTP access to the robot
control, thus we’ll be using PIN based OAuth, so make sure you pick Client
application and not web.

Now, don’t lose your Consumer key and Consumer secret – we’ll be using those in our
app for identity. Don’t give them out to anybody you don’t trust unless you’d like
people tweeting “from YourApp”. 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’re the same for everyone.

Setting up the Environment

We will be using Magpie RSS to parse RSS feeds, Curl to access the Twitter API and
the Twitter OAuth Class which is dependent on the Curl functions. Make sure you
install everything right – the magpie functions work, curl functions are available and
the Twitter OAuth class is defined.

What you also need is a bit.ly login and API key :-

Click here: - http://bit.ly/account/register

For Magpie RSS: - http://magpierss.sourceforge.net/

For Curl: - http://ru2.php.net/curl

For Twitter OAuth Class: - http://github.com/abraham/twitteroauth

Authentication: Storing the Access Tokens

We’ll divide our application in two parts,

1) One will be running every 15 minutes,

2) Second one will run once for authentication.

Create a new PHP file


Automated Serverside Tweeting Using OAuth

http://kovshenin.com/archives/automatic-tweet-oauth/

Twitter API: PIN-based OAuth Using PHP

http://kovshenin.com/archives/twitter-api-pin-based-oauth-php/

What’s important here is that oauth.php will run on its own and will accept parameters
via command line. Like this:

# 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


** So after you’ve validated your OAuth session, access tokens are stored somewhere on
disk, Say files as access_token and access_token_secret. If the files are present, then
your robot will be able to tweet, otherwise ask for registration. The main point of this
file is to get your access tokens written to disk.

Feeding Twitter from RSS

You need to distinguish different actions which the robot is supposed to do, through an
action parameter passed by the command line.Make sure your control flows similar
to this:

$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!
}

So the robot control will look something like this:

# php robot.php feed wordpress

Feeding your public timeline with wordpress articles

# php robot.php rthx

Thanking peoples for the retweets and following them all

# php robot.php reply

Writing strange stuff to peoples that mentioned you.


Define an associative array of feeds:

$feeds = array(
"wordpress" => array("url" =>
"http://wordpress.org/development/feed/",
"postfix" => "#wordpress"),
"mashable" => array("url" =>
"http://feeds2.feedburner.com/Mashable",
"postfix" => "via (@mashable)"),
);

We’ll make our robot tweet #wordpress tweets and read out mashable’s website and tweet
headlines via @mashable. Make sure that you’ve already written a check for
access_token and access_token_secret and gave out an error message if they didn’t
exist or were expired. We cannot tweet without those, remember? I’m assuming
you’ve stored them into $access_token and $access_token_secret respectively and
initialized the $oauth object:

$oauth = new TwitterOAuth($oauth_consumer_key, $oauth_consumer_secret,


$access_token, $access_token_secret);

The second parameter (“wordpress” in the control above example) would be stored
into a $feed_name variable. From there on we’ll go with code:

// 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->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 .= "&longUrl=$url_escaped";
$bitly_url .= "&login=$bitly_login&apiKey=$bitly_key";
$shortened_url =
json_decode(file_get_contents($bitly_url));

// If everything went okay, go on


if ($shortened_url->errorCode == 0)
{
// Retrieve the shortened url from the json
object
foreach ($shortened_url->results as $key =>
$value)
$shorturl = $value->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) > 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-
>OAuthRequest('https://twitter.com/statuses/update.xml',
array('status' => $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));
}
}
}
Automatically Thank Your Retweeters and… Follow them!

Be very very careful here as you don’t want to thank the retweeters too much. You’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. , but in general the
code should look something like this:

// Read the since_id from a file


$since_id = @file_get_contents("retweets_since_id");
if ($since_id > 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-
>OAuthRequest("http://twitter.com/statuses/mentions.xml" ,
array("count" => 50, "since_id" => $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->status[0]->id > $since_id) ? $mentions-
>status[0]->id : $since_id;
file_put_contents("retweets_since_id", (string)$last_id);

// Loop through the tweets


foreach ($mentions->status as $status)
{
// Let's see if somebody retweeted you.
// Err, remember to replace @yourname
if (strpos(strtolower($status->text), "rt @yourname")
|| strpos(strtolower($status->text), "via @yourname"))
{
// Add the guy to the retweeters array
$users_to_rthx[] = $status->user->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) < 140)
{
$tweet_mentions .= "@".$mention_this_guy . " ";

// Send the friendhips create (follow) request to the


API
echo "Following: " . $mention_this_guy;
$oauth-
>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-
>OAuthRequest('https://twitter.com/statuses/update.xml',
array('status' => $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) > 0)
{
$message = $tweet_prefix . trim($tweet_mentions) .
$tweet_postfix;
echo "Tweeting: " . $message . " (" . strlen($message) . ") \n";

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

$tweet_mentions = "";
}
Did Somebody Say Anything? Random Replies

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’t search for “rt @yourname” and “via
@yourname”, instead we work with ones that don’t contain “rt” and “via” at all.

Don’t forget to store the replies_since_id too as we don’t want to message users multiple
times (infinite number of times actually) because they’ll block the frustrating robot.
Here’s the while loop that actually tweets. In this part the names are stored in the
$users_to_reply array and goes like this:

// 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->OAuthRequest('https://twitter.com/statuses/update.xml',
array('status' => $message), 'POST');

// Wait a little
sleep(rand(5,30));
}
The last part is adding the whole stuff to crontab for automated work. Open up
/etc/crontab and add a few lines:

# 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

Potrebbero piacerti anche