76 lines
2.0 KiB
PHP
76 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Bots;
|
|
|
|
use App\Bots\BotContract;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\ClientException;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use GuzzleHttp\RequestOptions;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Mattermost implements BotContract
|
|
{
|
|
protected Client $client;
|
|
|
|
public function __construct(private ?array $config = [])
|
|
{
|
|
$this->client = new Client([
|
|
'base_uri' => rtrim(config('scheduler.mattermost.server_url'), '/') . '/api/v4/',
|
|
'headers' => [
|
|
'Authorization' => 'Bearer ' . config('scheduler.mattermost.access_token'),
|
|
'Accept' => 'application/json',
|
|
'Content-Type' => 'application/json',
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function run(): void
|
|
{
|
|
try {
|
|
$this->client->request(
|
|
$this->config['method'] ?? 'POST',
|
|
$this->config['endpoint'],
|
|
[
|
|
'json' => json_decode($this->config['body'] ?? '', true),
|
|
]
|
|
);
|
|
} catch (GuzzleException $e) {
|
|
Log::error("Call to Mattermost failed. " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public static function configSchema(): array
|
|
{
|
|
return [
|
|
'endpoint' => [
|
|
'type' => 'string',
|
|
'label' => 'API Endpoint',
|
|
'rules' => [
|
|
'required',
|
|
'string',
|
|
'max:255',
|
|
]
|
|
],
|
|
'method' => [
|
|
'type' => 'string',
|
|
'label' => 'HTTP Method',
|
|
'default' => 'POST',
|
|
'rules' => [
|
|
'required',
|
|
'in:GET,POST,PUT,DELETE,PATCH',
|
|
]
|
|
],
|
|
'body' => [
|
|
'type' => 'json',
|
|
'label' => 'Request Body (JSON)',
|
|
'rules' => [
|
|
'nullable',
|
|
'string',
|
|
'json',
|
|
]
|
|
],
|
|
];
|
|
}
|
|
}
|