📜 ⬆️ ⬇️

Mattermost. Integration with external services

Choosing the replacement used by our messaging system, I came across the description of the Mattermost, and decided to try. One of the advantages of the described system is simple integration with third-party services, the so-called "hooks" (outgoing and incoming hooks). That's about setting up interaction through hooks with external systems and this article will be (in our particular case, this is zabbix and glpi).


Part one. GLPI Integration


Since we, in our work, use GLPI to account for equipment, software, connections, registration of calls to technical support, it would be logical to organize the possibility for users to send applications to TP from the mattermost.


API


For integration with external services, GLPI has http rest api. Documentation on it is available in the installed system via the link http: //glpi/apirest.php/#glossary (where "glpi" is the address of your server).


Slightly thinking about the task, it was decided to implement the exchange algorithm in php, this fact is supported by the fact that php is already installed in the system and the script was organically entered in glpi and is available at http: //glpi/mm.php . It turned out, a kind of "proxy", which receives the request from mattermost, converts it into the desired format and sends it to GLPI. All http requests are transmitted in JSON format.


The work procedure consists of 5 parts:


  1. Getting a request from mattermost
  2. Session initialization in glpi
  3. Retrieving data from a request
  4. Sending data to glpi
  5. Closing session

Before proceeding to the description of the script code, we will carry out the preparatory work both in the mattermost and in glpi.


GLPI


  1. We will create a helpdesk user, on whose behalf requests will be created, and going into the settings of this user, we will generate tokens:
    image
    The one circled in red will be user_token.
  2. In the system settings you need to add a client to interact with the API. To do this, go to "Settings" -> "General" -> "API" and click the "Add client" button, add a record and generate a token (app_token)

image


  1. To identify the source of requests in the system, add an entry to the Directory "Query sources" and going to the newly added entry will remember its id (circled in red)
    image

This completes the setup of the API in GLPI.


Mattermost


In the client menu Mattermost go to "Inegration" -> "Outgoing Webhooks" click "Add" and add a record. On the screen, I highlighted significant fields. There should be a digression: the mattermost "trigger" to start the procedure for sending a request is a word or phrase, which, being indicated at the beginning of the message, actually starts the process. In our case, the word trigger is "112" (here is a direct association with the Ministry of Emergency Situations).


image


You can add a default username and a link to the avatar (or you can not add it), since these parameters will be passed in the request. But in order for mattermost to be able to process these parameters, in the server settings you need to change a couple of options in the /opt/mattermost/config/config.json file


"EnablePostUsernameOverride": true, "EnablePostIconOverride": true, 

At this setting is over. It is time to move on to writing code. The script is copied to the root directory with glpi files, in my case it is /var/www/html/glpi/mm.php


  <?php # GLPI     $app_token = '7uizyyildM71x84j1UxeABXTuCHdPoLRW45Tx2wG'; $user_app_token ='dZdCqc10Xhb1TxCT4OsXp8qqDSEqILASf2wZot0w'; #    (  ,    ) $requesttypes_id = '7'; #    GLPI (1 - , 2 - ) $type = '1'; #      $postData = file_get_contents('php://input'); $data = json_decode($postData, true); #  json  MatterMost.       #         #$message_text = $data["text"]; #$user_name = $data["user_name"]; #$user_id = $data["user_id"]; #$channel_name = $data["channel_name"]; #$channel_id = $data["channel_id"]; #$team_domain = $data["team_domain"]; #$team_id = $data["team_id"]; #$post_id = $data["post_id"]; #  "112"  ,    4 . $message_text = substr($data["text"],4); #   POST ,      mattermost #HTTP/1.1 200 OK header('Content-Type: application/json'); $reply = array( 'response_type' => 'comment', 'text'=> '      ' ); echo json_encode($reply); #     glpi #        #         if( $curl = curl_init() ) { curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: user_token '.$user_app_token, 'App-Token: '.$app_token)); curl_setopt($curl, CURLOPT_URL, 'http://glpi/apirest.php/initSession'); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $out = curl_exec($curl); $session = json_decode($out, true); $session_token = $session["session_token"]; #echo $session_token; curl_close($curl); } #       $json = array( 'input'=> array( 'name'=>'  '.$data["user_name"], 'requesttypes_id'=>$requesttypes_id, 'content'=>$message_text, 'type'=>$type ) ); #   if( $curl = curl_init() ) { curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'App-Token: '.$app_token, 'Session-token: '.$session_token)); curl_setopt($curl, CURLOPT_URL, 'http://glpi/apirest.php/Ticket'); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($json)); $out = curl_exec($curl); curl_close($curl); } #   if( $curl = curl_init() ) { curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'App-Token: '.$app_token, 'Session-token: '.$session_token)); curl_setopt($curl, CURLOPT_URL, 'http://glpi/apirest.php/killSession'); curl_setopt($curl, CURLOPT_POST, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $out = curl_exec($curl); curl_close($curl); } ?> 

The result of this script will be an added query in the incident recording system in GLPI. In pictures it looks like this:


We write the message to mattermost:


image


Go to GLPI "Support" -> "Requests" and a new message should appear in the list:


image


By clicking on the message header, more detailed information will open (the red outlined fields, the values ​​of which are transmitted in the script)


image


At this, the setting for sending messages to GLPI from the Mattermost can be considered complete. Having worked a little on the code, nothing can prevent a change in the type of request (incident or request).


')

Source: https://habr.com/ru/post/442402/


All Articles