Sending HipChat notifications in PHP

This morning I added a couple handlers to Notificator, one for RabbitMQ and another for HipChat: in this post I would like to show you how easy is to integrate HipChat within your systems.

The handler takes advantage of the PHP SDK that the HipChat team built, which is very, very good and available through packagist.

First thing you will need to do, is to create an instance of a notification manager and adding the handler to it, with an HipChat client and the API token you can generate from the HipChat admin interface:

1
2
3
4
5
6
7
8
9
10
11
<?php

use Namshi\Notificator\Manager;
use Namshi\Notificator\Notification\Handler\HipChat as HipChatHandler;
use HipChat\HipChat;
use Namshi\Notificator\Notification\HipChat\HipChatNotification;

$hipChatClient  = new HipChat('YOUR_API_TOKEN_HERE');
$hipChatHandler = new HipChatHandler(%hipChatClient);
$manager      = new Manager();
$manager->addHandler($hipChatHandler);

Then you only need to define a notification with a few, HipChat-specific, properties and trigger it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

$notification = new HipChatNotification(
  'YOLO!', // message
  'Alex',  // sender
  'room1', // name of the room you want this message to appear
  array(
      'hipchat_notify'            => true, // optional: should send notifications to everyone?
      'hipchat_color'             => HipChat::COLOR_GREEN, // optional: background color of the notification
      'hipchat_message_format'    => HipChat::FORMAT_TEXT, // optional: text or html
  )
);

$manager->trigger($notification);

The result is pretty self-explanatory:

Kind of the same code is also available, as an example, on the notificator repository, under the examples folder.

The greatness of Notificator are its handlers, so if you feel we should add another, useful handler, just shout out! Even better, you can contribute to the project by sending a pull request like Alessandro, Pascal and Luis already did!

Notes
  1. The sad truth is that capistrano has an hipchat gem/extension, but you cant really plug it the way you want (at least this happens to non-rubiers)

In the mood for some more reading?

...or check the archives.