error_reporting(E_ALL); ini_set('display_errors', 1); require_once 'SlackRequest.php'; require_once 'SlackResponse.php'; require_once 'BotHooks.php'; // initialize and validate the incoming request $request = SlackRequest::getInstance(); if (!$request->isValidRequest()) { SlackResponse::getInstance()->invalidRequest(); } // get our beloved token, trigger and text $token = $request->getParam(SlackRequest::REQUEST_TOKEN); $trigger = $request->getParam(SlackRequest::REQUEST_TRIGGER); $text = $request->getParam(SlackRequest::REQUEST_TEXT); // load any registered hooks for this trigger $tmpHooks = BotHooks::getInstance()->getHooksByTrigger($trigger); if (empty($tmpHooks)) { SlackResponse::getInstance()->invalidRequest(); } // find which of the loaded hooks are valid for the request token $hooks = array(); foreach ($tmpHooks as $hook) { if ($hook->isValidToken($token)) { $hooks[] = $hook; } } unset($tmpHooks); // no need to proceed if we don't have any hooks if (empty($hooks)) { SlackResponse::getInstance()->unauthorizedRequest(); }
$config = parse_ini_file(self::CONFIG_FILE, true); foreach ($config as $sectionName => $settings) { if (isset($settings['token'])) { foreach ($this->hooks as $hookName => $hook) { if ($hookName === $sectionName) { $hook->addTokens((array) $settings['token']); break; } } } } } /** * Generate a trigger-to-hook map for the given hook and each of it's supported triggers. * * @param \Slackbot\Hooks\Hook $hook */ private function loadHookTriggers(\Slackbot\Hooks\Hook $hook) { $triggers = $hook->getTriggers(); foreach ($triggers as $trigger) { if (!isset($this->triggers[$trigger])) { $this->triggers[$trigger] = array(); } $this->triggers[$trigger][] = $hook; } } } // auto-initialize this class upon being included =] BotHooks::getInstance();