commit ab157d3357ce59c4dfd2a25f78a8e92e311cc062 Author: Pavel Shevaev Date: Mon Oct 23 18:38:32 2023 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e92f57 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tags diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..edbdbe1 --- /dev/null +++ b/composer.json @@ -0,0 +1,11 @@ +{ + "name": "bit/mattermost", + "description": "Mattermost simple utils", + "homepage": "https://git.bit5.ru/bit/mattermost", + "require": { + "php": ">=7.4" + }, + "autoload": { + "classmap": ["mattermost.inc.php"] + } +} diff --git a/mattermost.inc.php b/mattermost.inc.php new file mode 100644 index 0000000..c8d397f --- /dev/null +++ b/mattermost.inc.php @@ -0,0 +1,88 @@ + $channel_id, + 'files' => $cfile + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_POSTFIELDS, $upload_fields); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_VERBOSE, true); + curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); + + $result = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + if($status != 201) + throw new Exception("File failed with status: " . $status .", result: " . var_export($result, true)); + + $result = json_decode($result, true); + $file_id = $result['file_infos'][0]['id']; + + $post_fields['file_ids'] = array($file_id); + return mattermost_send($server, $token, $channel_id, $title, $post_fields); +} + +function mattermost_send_thread_with_replies($server, $token, $channel_id, $msg, array $replies, $fields = []) +{ + $mm_orig_resp = mattermost_send($server, $token, $channel_id, $msg, $fields); + + $root_key = 'id'; + if(!isset($mm_orig_resp[$root_key])) + { + echo "Failed to post to Mattermost: response contains no '$root_key' value to post thread replies:\n".json_encode($mm_orig_resp, JSON_PRETTY_PRINT)."\n"; + return null; + } + + $root_id = $mm_orig_resp[$root_key]; + $fields['root_id'] = $root_id; + + foreach($replies as $reply) + mattermost_send($server, $token, $channel_id, $reply, $fields); + + return $root_id; +}