Add generic api schema and scripts model
This commit is contained in:
82
app/Bots/GenericApi.php
Normal file
82
app/Bots/GenericApi.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bots;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class GenericApi implements BotContract
|
||||
{
|
||||
protected array $config;
|
||||
|
||||
protected Client $client;
|
||||
|
||||
public function __construct(array $config = [])
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
try {
|
||||
$this->client = new Client();
|
||||
|
||||
$options = [];
|
||||
|
||||
if (!empty($this->config['headers'])) {
|
||||
$options['headers'] = json_decode($this->config['headers'], true);
|
||||
}
|
||||
|
||||
if (!empty($this->config['body'])) {
|
||||
$options['json'] = json_decode($this->config['body'], true);
|
||||
}
|
||||
|
||||
$this->client->request(
|
||||
$this->config['method'] ?? 'GET',
|
||||
$this->config['url'],
|
||||
$options
|
||||
);
|
||||
} catch (GuzzleException $e) {
|
||||
Log::error("Call to {$this->config['url']} failed" . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function configSchema(): array
|
||||
{
|
||||
return [
|
||||
'url' => [
|
||||
'type' => 'string',
|
||||
'label' => 'API URL',
|
||||
'rules' => [
|
||||
'required',
|
||||
'url',
|
||||
]
|
||||
],
|
||||
'method' => [
|
||||
'type' => 'string',
|
||||
'label' => 'HTTP Method',
|
||||
'rules' => [
|
||||
'required',
|
||||
'in:GET,POST,PUT,DELETE,PATCH',
|
||||
]
|
||||
],
|
||||
'headers' => [
|
||||
'type' => 'json',
|
||||
'label' => 'Request Headers (JSON)',
|
||||
'rules' => [
|
||||
'nullable',
|
||||
'json',
|
||||
]
|
||||
],
|
||||
'body' => [
|
||||
'type' => 'json',
|
||||
'label' => 'Request Body (JSON)',
|
||||
'rules' => [
|
||||
'nullable',
|
||||
'json',
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,9 @@ 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
|
||||
@@ -25,49 +28,48 @@ class Mattermost implements BotContract
|
||||
public function run(): void
|
||||
{
|
||||
try {
|
||||
$request = new \GuzzleHttp\Psr7\Request(
|
||||
$this->client->request(
|
||||
$this->config['method'] ?? 'POST',
|
||||
$this->config['endpoint'],
|
||||
['Content-Type' => 'application/json'],
|
||||
json_encode($this->config['body'] ?? [])
|
||||
[
|
||||
'json' => json_decode($this->config['body'] ?? '', true),
|
||||
]
|
||||
);
|
||||
|
||||
$res = $this->client->send($request);
|
||||
|
||||
// $res = $this->client->put('users/me/status/custom', [
|
||||
// 'json' => [
|
||||
// 'emoji' => 'house',
|
||||
// 'text' => 'Working Home',
|
||||
// 'status' => 'online',
|
||||
// ]
|
||||
// ]);
|
||||
|
||||
if ($res->getStatusCode() === 200) {
|
||||
Log::info("Mattermost home status updated successfully.");
|
||||
} else {
|
||||
Log::error("Failed to update Mattermost status. HTTP Status: " . $res->getStatusCode());
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error updating Mattermost status: " . $e->getMessage());
|
||||
} 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' => 'array', 'label' => 'Request Body (JSON)', 'rules' => [
|
||||
'nullable',
|
||||
'array',
|
||||
]],
|
||||
'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',
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bots\Mattermost;
|
||||
|
||||
use App\Bots\BotContract;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class RemoveStatus 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'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
try {
|
||||
$res = $this->client->delete('users/me/status/custom');
|
||||
|
||||
if ($res->getStatusCode() === 200) {
|
||||
Log::info("Mattermost status cleared");
|
||||
} else {
|
||||
Log::error("Failed to update Mattermost status. HTTP Status: " . $res->getStatusCode());
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error updating Mattermost status: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function configSchema(): array
|
||||
{
|
||||
return [
|
||||
'endpoint' => ['type' => 'string', 'label' => 'API Endpoint'],
|
||||
'method' => ['type' => 'string', 'label' => 'HTTP Method', 'default' => 'POST'],
|
||||
'body' => ['type' => 'array', 'label' => 'Request Body (JSON)'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Bots\Mattermost;
|
||||
|
||||
use App\Bots\BotContract;
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WednesdayHomeStatus 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 {
|
||||
$res = $this->client->put('users/me/status/custom', [
|
||||
'json' => [
|
||||
'emoji' => 'house',
|
||||
'text' => 'Working Home',
|
||||
'status' => 'online',
|
||||
]
|
||||
]);
|
||||
|
||||
if ($res->getStatusCode() === 200) {
|
||||
Log::info("Mattermost home status updated successfully.");
|
||||
} else {
|
||||
Log::error("Failed to update Mattermost status. HTTP Status: " . $res->getStatusCode());
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error updating Mattermost status: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static function configSchema(): array
|
||||
{
|
||||
return [
|
||||
'endpoint' => ['type' => 'string', 'label' => 'API Endpoint'],
|
||||
'method' => ['type' => 'string', 'label' => 'HTTP Method', 'default' => 'POST'],
|
||||
'body' => ['type' => 'array', 'label' => 'Request Body (JSON)'],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user