Skip to content

Instantly share code, notes, and snippets.

@ab5w
Last active January 15, 2018 15:06
Show Gist options
  • Save ab5w/067ccaf2a305cc5f6e3397b72e319562 to your computer and use it in GitHub Desktop.
Save ab5w/067ccaf2a305cc5f6e3397b72e319562 to your computer and use it in GitHub Desktop.
example mattermost bot using an outgoing webhook and incoming webhooks.
<?php
//Example Xerxes mattermost bot with incoming/outgoing webhook use.
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//function to send the message payload to mattermost.
function send_to_mattermost($message) {
global $channel;
$mattermost_server = '';
$icon_url = 'http://i.imgur.com/f16pg.jpg';
$payload = 'payload=' . json_encode(array('username' => 'BESTBOT', 'icon_url' => $icon_url, 'text' => $message));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://" . $mattermost_server . "/hooks/" . $channel);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
}
//get our posted json from mattermost
$post = file_get_contents("php://input");
$input = json_decode($post,true);
//grab the bits we need
$message = $input['text'];
$user = $input['user_name'];
$channel = $input['channel_name'];
//if statement to map the channel name to the incoming webhook to use.
if ($channel == 'ops-chat') {
$channel = 'sjk87hwt3fdfdxkuq5kkw3argc';
} elseif ($channel == 'off-topic') {
$channel = '7lamncptxqesr3joakfkbtxdrbi'
} else {
exit();
}
//match for commands starting with ?, else don't go any further.
if (!preg_match("/^\?/", $message)) { exit(0); }
//split the incoming text into the command and arguments.
$input = $message;
$nick = $user;
$toks = explode(" ",$input);
$command = str_replace("?","",$toks[0]);
$arg1 = $toks[1];
$arg2 = $toks[2];
$arg3 = $toks[3];
//functions for our commands.
function excuse() {
$msg = file_get_contents("http://pages.cs.wisc.edu/~ballard/bofh/excuses");
$msg = explode(PHP_EOL, $msg);
$msg = $msg[array_rand($msg, 1)];
return $msg;
}
function coding_love () {
$url = file_get_contents("http://thecodinglove.com/random");
preg_match_all('/<div class="centre"> <h.*/', $url, $matches);
if (isset($matches[0][0])) {
$matches = explode('<p', $matches[0][0]);
$title = explode('<h3>', $matches[0]);
$title = explode('</h3>', $title[1]);
$title = trim($title[0]);
$title = str_replace('&rsquo;', "'", $title);
$title = str_replace('&ldquo;', '"', $title);
$title = str_replace('&rdquo;', '"', $title);
$title = str_replace('&hellip;', '...', $title);
preg_match_all('/http.*.(gif|jpg)/', $matches[1], $matches2);
$img = trim($matches2[0][0]);
if (!preg_match('/minus/', $img)) {
if (preg_match('/.jpg$/', $img)) {
$img = preg_replace('/.jpg$/', '.gif', $img);
}
send_to_mattermost($title . PHP_EOL . $img);
}
}
}
function random_password($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";
$password = substr(str_shuffle($chars),0,$length);
return $password;
}
//action the relevant output per command.
switch(strtolower($command)){
case 'sleep':
send_to_mattermost("zzZzzZzzzZzzz!");
break;
case 'bark':
send_to_mattermost("WOOF!");
break;
case 'hello':
send_to_mattermost("Hello " . $nick . "!");
break;
case 'flashbang':
send_to_mattermost('Counter-Terrorists Win');
break;
case 'excuse':
send_to_mattermost(excuse());
break;
case 'code':
coding_love();
break;
case 'joke':
$joke = json_decode(file_get_contents('http://tambal.azurewebsites.net/joke/random'),1);
send_to_mattermost($joke['joke']);
break;
case 'password':
if (isset($arg1)) {
$length = intval($arg1);
send_to_mattermost(random_password($length));
} else {
send_to_mattermost(random_password());
}
break;
case 'xkcd':
$num = json_decode(file_get_contents("http://xkcd.com/info.0.json"),true);
$num = $num['num'];
$comic_num = mt_rand(0, $num);
$comic_array = json_decode(file_get_contents("http://xkcd.com/" . $comic_num . "/info.0.json"),true);
$comic_img = $comic_array['img'];
$comic_alt = $comic_array['alt'];
send_to_mattermost($comic_img . PHP_EOL . $comic_alt);
break;
default:
send_to_mattermost("I'm sorry, " . $nick . ". I'm afraid I can't " . $command . " because; " . excuse());
break;
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment