public function testBadColor() { $bad_color = 'fancy pink'; $hc = new HipChat('hipchat-php-test-token', $this->target); $this->setExpectedException('InvalidArgumentException'); $hc->message_room(1337, 'Hipchat', "Hi everybody.", false, $bad_color); }
/** * Post every new registration to HipChat. * * @param UserModel $sender * @param array $args */ public function userModel_afterInsertUser_handler($sender, $args) { // Determine how to link their name. $name = val('Name', $args['InsertFields']); if (c('Garden.Registration.Method') == 'Approval') { $user = anchor($name, '/user/applicants', '', array('WithDomain' => true)); $reason = sliceParagraph(val('DiscoveryText', $args['InsertFields'])); $message = sprintf(t('New member: %1$s (%2$s)'), $user, $reason); } else { // Use conservative name linking structure. $user = anchor($name, '/profile/' . $args['InsertUserID'] . '/' . $name, '', array('WithDomain' => true)); $message = sprintf(t('New member: %1$s'), $user); } // Say it. HipChat::say($message); }
/** * Webhook for Teamwork. * * POST data looks like this: * [ 'event' => 'TASK.COMPLETED', * 'objectId' => '000', * 'accountId' => '000', * 'userId' => '000', * ] * * @see http://developer.teamwork.com/todolistitems * * @param Gdn_Controller $sender * @param $secret * @throws Exception */ public function utilityController_teamworkTaskCompleted_create($sender, $secret) { if ($secret != c('SprintNotifier.Teamwork.Secret')) { throw new Exception('Invalid token.'); } // Get data $data = Gdn::request()->post(); // Sanity check we set up webhooks right. if (val('event', $data) != 'TASK.COMPLETED') { return; } // Cheat by storing some data in the config. $users = c('SprintNotifier.Teamwork.Users', []); $projects = c('SprintNotifier.Teamwork.Projects', []); // Get full task data via Teamwork's *ahem* "API". $task = self::teamworkTask(val('objectId', $data)); // DEBUG UserModel::setMeta(0, array('TaskAPI' => var_export($task, true)), 'SprintNotifier.Debug.'); // Respect project whitelist if we're using one. if (count($projects) && !in_array($task['project-name'], $projects)) { return; } // Build data for the chat message. $teamworkUserID = val('userId', $data); $userName = val($teamworkUserID, $users, 'User ' . val('userId', $data)); $taskUrl = sprintf('https://%1$s.teamwork.com/tasks/%2$s', c('Teamwork.Account'), val('objectId', $data)); $message = sprintf('%1$s completed %2$s task: <a href="%3$s">%4$s</a>', $userName, strtolower($task['project-name']), $taskUrl, $task['content']); // Override HipChat plugin's default token & room. saveToConfig('HipChat.Room', c('SprintNotifier.HipChat.RoomID'), false); saveToConfig('HipChat.Token', c('SprintNotifier.HipChat.Token'), false); // DEBUG UserModel::setMeta(0, array('Message' => var_export($message, true)), 'SprintNotifier.Debug.'); // Say it! Bust it! if (class_exists('HipChat')) { HipChat::say($message); } self::bustCache(); // 200 OK $sender->render('blank', 'utility', 'dashboard'); }
/** * Initializes HipChat API if credentials are valid. * * @access public * @return bool */ public function initialize_api() { if (!is_null($this->api)) { return true; } /* Load the API library. */ if (!class_exists('HipChat')) { require_once 'includes/class-hipchat.php'; } /* Get the OAuth token. */ $oauth_token = $this->get_plugin_setting('oauth_token'); /* If the OAuth token, do not run a validation check. */ if (rgblank($oauth_token)) { return null; } $this->log_debug(__METHOD__ . '(): Validating API Info.'); /* Setup a new HipChat object with the API credentials. */ /** * Enable or disable Verification of Hipchat SSL * * @param bool True or False to verify SSL */ $verify_ssl = apply_filters('gform_hipchat_verify_ssl', true); $hipchat = new HipChat($oauth_token, $verify_ssl); /* Run an authentication test. */ if ($hipchat->auth_test()) { $this->api = $hipchat; $this->log_debug(__METHOD__ . '(): API credentials are valid.'); return true; } else { $this->log_error(__METHOD__ . '(): API credentials are invalid.'); return false; } }
/** * @param $request * @return mixed */ private function processRequest($request) { if ($request['status'] == 200 or $request['status'] == 201 or $request['status'] == 204) { return $request; } $response_array = $this->objectToArray($request['body']); HipChat::throwException($response_array['error']['code'], $response_array['error']['message'], $request['meta']['uri']); }
#!/usr/bin/php <?php require 'HipChat.php'; if (!isset($argv[1])) { echo "Usage: {$argv['0']} <token> [target]\n"; die; } $token = $argv[1]; $target = isset($argv[2]) ? $argv[2] : 'http://api.hipchat.com'; $hc = new HipChat($token, $target); echo "Testing HipChat API.\nTarget: {$target}\nToken: {$token}\n\n"; // get rooms echo "Rooms:\n"; try { $rooms = $hc->get_rooms(); foreach ($rooms as $room) { echo "Room {$room->room_id}\n"; echo " - Name: {$room->name}\n"; $room_data = $hc->get_room($room->room_id); echo " - Participants: " . count($room_data->participants) . "\n"; } } catch (HipChat_Exception $e) { echo "Oops! Error: " . $e->getMessage(); } // get users echo "\nUsers:\n"; try { $users = $hc->get_users(); foreach ($users as $user) { echo "User {$user->user_id}\n"; echo " - Name: {$user->name}\n";