initialized = true; parent::__construct($config + array('custom_text' => '')); } /** * This creates the listener that listens for new * threads to send out mattermost notifications */ public function run() { $dispatcher = EventDispatcher::getInstance(); $dispatcher->attachListener(Events::THREAD_CREATE, $this, 'sendMattermostNotification'); } /** * Sends notification to Mattermost. * * @return boolean */ public function sendMattermostNotification(&$args) { // Convert to json $data = array( 'username' => $this->config['username'], 'channel' => strtolower($this->config['channel']), 'text' => getlocal('You have a new user {0} waiting for response.', array($args['thread']->userName)) . ( ($this->config['custom_text'] != '') ? ' ' . $this->config['custom_text'] : '' ) ); $json = json_encode($data); // Do the request $curl = curl_init($this->config['mattermost_url']); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json')); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $json); $json_response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ( $status != 201 ) { trigger_error( 'Unable to send notification to Mattermost. Request failed with status ' . $status, E_USER_WARNING ); return false; } curl_close($curl); return true; } /** * Returns plugin's version. * * @return string */ public static function getVersion() { return '1.1.0'; } /** * Returns plugin's dependencies. * * @return type */ public static function getDependencies() { return array(); } /** * {@inheritdoc} */ public static function install() { // Initialize localization constants $constants = array( 'You have a new user {0} waiting for response.' ); foreach ($constants as $constant) { getlocal($constant); } return true; } }