Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
This is just a mockup, a simple prototype, which is way too fresh for any actual use.
1) Authentication
2) RSS lookup
3) Parsing
Platform: - All this is going to be setup on a linux box running crond and acting every 15
minutes or so
* 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
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.
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.
http://kovshenin.com/archives/automatic-tweet-oauth/
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:
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!
}
$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:
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:
// 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));
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 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);
// Reset
$tweet_mentions = "";
}
}
$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*"
);
// 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: