Skip to content

Instantly share code, notes, and snippets.

@mcarpenterjr
Last active August 25, 2017 13:11
Show Gist options
  • Save mcarpenterjr/1c3ee704707d0c12fc4fc7eceab95a4c to your computer and use it in GitHub Desktop.
Save mcarpenterjr/1c3ee704707d0c12fc4fc7eceab95a4c to your computer and use it in GitHub Desktop.
PHP incoming matter-most web-hook handler.
<?php
/**8 MATTERMOST INCOMING WEBHOOK 8**/
/* */
/* NOTE: Use either standalone with URL parameters or include */
/* in a PHP script to naturally call the send_msg_mmst(). */
/* Returns true if the call is succesful, or whatever the */
/* error. */
/* Includes defaults for most parameters to simplify usage. */
/* */
/* @PARAMETER $message - Can be an Array or String. Required */
/* If is an array, must use the following keys: */
/* 'url' - The webhook url */
/* 'user' - Override for the mmst user */
/* 'icon' - URL to an image to override the user icon */
/* 'channel' - Specify a team channel to override Webhook */
/* default */
/* 'text' - Not required in this case. The message text. */
/* */
/* @PARAMETER $target - Not required, set the default value */
/* to your main webhook url. */
/* */
/* @PARAMETER $userName - Not required, this will override */
/* the channel username */
/* */
/* @PARAMETER $icon - Not required, overrides the user icon. */
/* */
/* @PARAMETER $channel - Not required, overrides the channel */
/* the webhook outputs to. */
/* */
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
function send_msg_mmst($message, $target = 'http://dev2:8065/hooks/ochkd3685fg5pnyff3wnjpaffw', $userName = 'Message Admin', $icon = 'http://linux3/img/mailAdmin.png', $channel = null) {
$message_text = $message;
$webhook = $target;
if (is_array($message)) {
$webhook = isset($message['url']) ? $message['url'] : $target;
$message_text = isset($message['text']) ? $message['text'] : 'No Message Content';
$userName = isset($message['user']) ? $message['user'] : $username;
$icon = isset($message['icon']) ? $message['icon'] : $icon;
$channel = isset($message['channel']) ? $message['channel'] : $channel;
}
$content['text'] = $message_text;
$content['username'] = $userName;
$content['icon_url'] = $icon;
if (!is_null($channel)) {
$content['channel'] = $channel;
}
$payload = 'payload='.json_encode($content);
$result = exec_mmst_msg($webhook, $payload);
if($result == 'ok') {
return true;
}
else {
return $result;
}
}
// Just does our curl stuff.
function exec_mmst_msg($webhook_url, $webhook_payload) {
$ch = curl_init();
// set cURL options
curl_setopt($ch, CURLOPT_URL, $webhook_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $webhook_payload);
// enable return and execute
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
// close connection
curl_close($ch);
// return the response from mattermost, either an error or "OK"
return $server_output;
}
if(count($_GET)) {
$mmst_msg = array(
'text' => isset($_GET['message']) ? $_GET['message'] : null,
'user' => isset($_GET['username']) ? $_GET['username'] : null,
'url' => isset($_GET['target']) ? $_GET['target'] : null,
'icon' => isset($_GET['icon']) ? $_GET['icon'] : null,
'channel' => isset($_GET['channel']) ? $_GET['channel'] : null
);
send_msg_mmst($mmst_msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment