Sorry, this post is in english, as i got already some requests from twitter people to post this code.
I was playing a bit with the twitter api and it's really fun. I used to subscribe to my the comments in my blog via RSS when i still used Wordpress. I know there is a module for Drupal to do the same, but i use my RSS reader only once per day. So i was looking for a way to get instant notification on new comments in my blog. And as my twitter client runs all day i figured that would be a nice way.
I'm workin with Drupal 5, but i think it should work like this on Drupal 6 as well.
I decided to use direct tweets. These are the private mails in twitter and they can only bee seen by sender and receiver. I don't want to bother my followers with new comment notifications from my main twitter account. It would be too much if i write a popular post. For some reasons you can't send direct Tweets to yourself, although this applications shows that it would make sense. So you need to set up a new twitter account for your site. There are probably many more ways to set up the notification. Hope this post will inspire you.
One of the most fascinating Drupal Modules is workflow_ng. It lets you create custom actions on Drupal events. It's like a hook and filter system with a GUI. You need to install that module for my Twitter notifier. Besides lots of Drupal own actions it lets you just execute custom php code.
So after you installed and activated the Workflow-NG Module you need to go admin/workflow-ng/configurations and add a new rule configuration. The ecent you choose is Comment has been created nd you label it like you want, for example with Twitter Comment Notifier. At this point it would make sense to filter out your own comments. Thats easily done. Simply Add a condition with User has roles(s). Negate the rule, choose acting user and select admin.
After you safed the Condition you can Add an action. Choose the last option Execute custom PHP code.
You can just copy and paste my code, all you need to do is change you twitter username, password and receiver of the direct message. I just created a new twitter account for my website and it's only purpose is to send me messages with this script.
So here is the code:
function tinyURL($url) { $curl = curl_init(); curl_setopt($curl,CURLOPT_URL, "http://tinyurl.com/api-create.php?url=".urlencode($url)); curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,10); $tiny = curl_exec($curl); curl_close($curl); return($tiny); } function sendtwittermessage($username,$password,$receiver,$message){ $url = "http://twitter.com/direct_messages/new.xml"; $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,"user=$receiver&text=$message"); $results = curl_exec ($ch); curl_close ($ch); } //++++++++++++++++++++++++++++++++++++ // change to your own values $username = ''; $password = ''; $twitterreceiver = ''; //++++++++++++++++++++++++++++++++++++ $tinyurl = tinyURL($_SERVER['HTTP_REFERER']); if (!$tinyurl) { $tinyurl = $_SERVER['HTTP_REFERER']; } $commenttext = strip_tags('[comment:comment-body]'); $message = "[comment_author:user] said: ".$commenttext; if (strlen($tinyurl) + strlen($message) > 139) { $message = substr($message, 0, (140 - strlen($message) - strlen($tinyurl))); } $message .= ' '.$tinyurl; sendtwittermessage($username,$password,$twitterreceiver,$message);
When you play a little bit with the Workflow-NG mdoule, you can easily see new applications with this notifier. If you run Ubercart on Drupal, you can send yourself tweets when new orders come in or new user accounts are created.
Would love to hear your feedback what you did with workflow and twitter.


Post new comment