How to get dzone feeds as IM using JAXL? Add [email protected]

The most funny part of knowing XMPP protocol is that you can execute all your daily web needs using it. Be it playing anagram online or achieving your status messages. And your life gets easy if you are using an XMPP Client Library like JAXL.

Here I have tried to create a simple bot which allows you to read newest links posted on dzone category wise. Simply add [email protected] in your gtalk messenger and start by typing options.

Available options are:

  • frontpage: Will send you latest links on frontpage.
  • queue: Will send you latest links in queue.
  • tag: Send in a tag e.g. php,java,xml and you will get latest links for the particular tag

Here is modified eventMessage() method of JAXL library powering [email protected]:

    function eventMessage($fromJid, $content, $offline = FALSE) {

      // If the message is not an offliner
      if(!$offline) {
        if($this->restrictJid && !in_array($this->getBareJid($fromJid),$this->authorizedJid)) { // Block un-authorized users
          $this->logger->logger($fromJid." tried using this service by sending:n".$content);
          $message = 'You are not authorized to use this service';
          $this->sendMessage($fromJid,$message);
          return TRUE;
        }
        else {
          $content = strtolower(trim($content));

          // Build RSS Feed URL depending upon passed IM
          if($content == "options") {
            $message = "Available options are:n";
            $message .= "*1. frontpage:* Will send you latest links on frontpage.n";
            $message .= "*2. queue:* Will send you latest links in queue.n";
            $message .= "*3. tag:* Send in a tag e.g. php,java,xml and you will get latest links for the particular tagn";
            $this->sendMessage($fromJid,$message);
            return TRUE;
          }
          else if($content == "frontpage") $url = "http://www.dzone.com/links/feed/frontpage/rss.xml";
          else if($content == "queue") $url = "http://www.dzone.com/links/feed/queue/rss.xml";
          else $url = "http://www.dzone.com/links/feed/frontpage/".$content."/rss.xml";

          // Generate cache file location
          $cache_file = "cache/dzone/".md5($url)."-".$content.".rss";

          // If cache file is not older than 900 seconds (15 min)
          if(file_exists($cache_file) && (filemtime($cache_file) > time() - 900)) {
            $this->logger->logger("RSS already found cached...");
          }
          else { // else fetch a fresh copy of RSS
            $this->logger->logger("Fetching RSS for url:n".$url);
            $head = $this->fetchWebURL($url);
            if($head['errno'] == 0 && $head['http_code'] == 200) {
              $this->logger->logger("Fetched successfully...");
              $data = $head['content'];
              $fh = fopen($cache_file,"w");
              fwrite($fh,$data);
              fclose($fh);
            }
            else {
              // Send a message saying no such feed exists
              $this->logger->logger("Failed to fetch the RSS feed...");
              $message = 'No feed exists for the passed keyword: '.$content;
              $this->sendMessage($fromJid,$message);
              return TRUE;
            }
          }

          // Parse RSS Feed
          $r = &new XML_RSS($cache_file);
          $r->parse();
          $NumberOfFeeds = count($r->getItems());
          if($NumberOfFeeds > 0) {
            $message = '';
            foreach($r->getItems() as $key => $item) {
              if($this->count < $this->maxResults) {
                $message .= "*".$item["title"]."*n";
                $message .= $item["link"]."n";
                $message .= $item["description"]."nn";
                $this->count++;
              }
              else {
                break;
              }
            }
            $message .= "powered by *Jaxl* http://code.google.com/p/jaxl"."nn";
            $this->sendMessage($fromJid,$message);
            $this->count = 0;
            return TRUE;
          }
          else {
            // Send a message saying no feed found
            $message = 'No feed was found for passed keyword: '.$content;
            $this->sendMessage($fromJid,$message);
            return TRUE;
          }
        }
      }
      else {
        // Send an appropriate message
        $this->logger->logger($fromJid." sent an offliner saying:n".$content);
        return TRUE;
      }
    }

To run this sample application you need:

  • JAXL Library (http://code.google.com/p/jaxl)
  • XML_RSS Package (pear install XML_RSS)
  • Create a directory cache/dzone where this application will cache all rss files
  • Include jaxl4dzone.class.php in index.php, instead of jaxl.class.php and run ‘php index.php’ from command line to start this application

Checkout the complete code and JAXL library from http://code.google.com/p/jaxl.

Now browse new links even when dzone is down for maintenance 😛
Do let me know about your feedbacks 🙂

  • namespace

    Wow! This is great, and you are true. I am getting a lot of down for maintenance message from dzone since past few days. And if this works even if its down, you simply rock for me.

  • Very very cool. Just a note about the downtime, we moved to a new datacenter this past week and we’ve been experiencing some nasty DNS issues. We hope these are going to be resolved by the end of the week and that you’re going to see some great new speed and some new features coming up in the near future.

    -Matt
    DZone, Inc.

  • admin

    Thanks for the comment Matthew. I probably guessed that you guys are in middle of a server move which intermittently shows down for maintenance error 🙂

  • Gaurav

    Hello,

    I have some to doubts regarding creating my own bot using JAXL. The doubts are as follows:

    I have downloaded the code from http://code.google.com/p/jaxl
    I have done the changes in the config.php file. But where I should keep the files
    How can I get & link the bot id with the server pages?
    I am trying “prod” environment. I have uploaded files on server.
    How to proceed?
    Pls help me.

    Thanks in advance.

  • You need to do the following stuff:

    1. Checkout the whole code from code.google.com. Note that jaxl4dzone.class.php doesn’t comes by default with the .rar file under featured downloads. So u need to check out that code.
    2. You put the following files in a single directory: config.ini.php, xml.class.php, index.php, xmpp.class.php, jaxl4dzone.class.php, logger.class.php. Also create a directory named log, which have a file logger.log inside it.
    3. Open config.ini.php and set $env = “prod” and enter your gmail specifications under prod section.
    4. Open index.php and change the line include_once(“jaxl.class.php”) to include_once(“jaxl4dzone.class.php”)
    5. If you are on windows, open command line and point it to the directory where all these files are.
    6. Run from command line: php index.php
    7. If you are on linux, point to this directory and run the command in line (6) above.

    I hope i haven’t missed out anything. Run the above procedure and do mail me the log files, if it doesn’t work for you. That will help me identify the exact problem.

  • Pingback: Programatically control your google mails using JAXL v 1.0.4 « Abhi’s Weblog()

  • Pingback: 5 exciting (gaming) bots you can create using Jaxl (Jabber XMPP Library) in PHP | Abhi's Weblog()

  • Pingback: Get lyrics for any song using XMPP and PHP right into your IM – Add [email protected] | Abhi's Weblog()

  • Announcing Jaxl v3.x – asynchronous, non-blocking I/O, event based PHP client/server library – http://abhinavsingh.com/blog/2012/07/announcing-jaxl-v3-x-asynchronous-non-blocking-io-event-based-php-clientserver-library/