Created
March 17, 2014 21:16
-
-
Save iNem0o/9608524 to your computer and use it in GitHub Desktop.
Simple PHP notifier for HipChat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace inem0o\Tools; | |
/** | |
* Class HipChatNotifier | |
* Allow you to notify a room on HipChat | |
* @package inem0o\Tools | |
*/ | |
class HipChatNotifier | |
{ | |
/** | |
* HipChat API authentification token | |
* @var string | |
*/ | |
protected $_authToken; | |
/** | |
* Username used to publish the notification | |
* @var | |
*/ | |
protected $_notifierUserName; | |
/** | |
* ID or name of the room. | |
* @var | |
*/ | |
protected $_roomId; | |
/** | |
* @var int | |
*/ | |
protected $_useNotification = 0; | |
/** | |
* Background color for message. One of "yellow", "red", "green", "purple", "gray", or "random". (default: yellow) | |
* @var string | |
*/ | |
protected $_backgroundColor = 'yellow'; | |
protected $_allowedBackgroundColors = array("yellow", "red", "green", "purple", "gray", "random"); | |
/** | |
* All messages to insert into the notification (one per line) | |
* @var array | |
*/ | |
protected $_messageBuffer = array(); | |
public function __construct($authToken, $channelId, $notifierUserName) | |
{ | |
$this ->_authToken = $authToken; | |
$this ->_roomId = $channelId; | |
$this ->_notifierUserName = $notifierUserName; | |
} | |
/** | |
* Add a timestamped message | |
* | |
* @param $text | |
*/ | |
public function addMessage($text) | |
{ | |
$this ->_messageBuffer[] = "[" . date('d-m-Y h:i:s') . "] " . nl2br($text); | |
return $this; | |
} | |
/** | |
* Clean the message buffer | |
*/ | |
public function clearBuffer() | |
{ | |
$this ->_messageBuffer = array(); | |
return $this; | |
} | |
/** | |
* Whether or not this message should trigger a notification for people in the room (change the tab color, play a sound, etc). | |
* Each recipient's notification preferences are taken into account. (default: false) | |
*/ | |
public function notifyUsers($useNotification = true) | |
{ | |
$this ->_useNotification = $useNotification ? 1 : 0; | |
return $this; | |
} | |
/** | |
* Background color for message. One of "yellow", "red", "green", "purple", "gray", or "random". (default: yellow) | |
* @var string | |
*/ | |
public function color($color) | |
{ | |
if (in_array($color, $this->_allowedBackgroundColors)) { | |
$this ->_backgroundColor = $color; | |
} | |
return $this; | |
} | |
/** | |
* Send notification to hipchat | |
* @return bool | |
*/ | |
public function send() | |
{ | |
$url = sprintf("https://api.hipchat.com/v1/rooms/message?format=json&auth_token=%s", $this->_authToken); | |
$data = array( | |
'room_id' => $this ->_roomId, | |
'from' => $this ->_notifierUserName, | |
'message' => implode("<br>", $this ->_messageBuffer), | |
'notify' => $this ->_useNotification, | |
'color' => $this ->_backgroundColor | |
); | |
$dataString = ''; | |
foreach ($data as $key => $value) { | |
$dataString .= $key . '=' . urlencode($value) . '&'; | |
} | |
rtrim($dataString, '&'); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_POST, count($data)); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataString); | |
$result = curl_exec($curl); | |
curl_close($curl); | |
$this ->clearBuffer(); | |
if (empty($result)) { | |
return false; | |
} | |
$result = json_decode($result); | |
if (!isset($result->status)) { | |
return false; | |
} | |
return $result->status == "sent"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment