From 6811fa1c2767127089ce13246dd5162302b84582 Mon Sep 17 00:00:00 2001 From: Tii Date: Thu, 6 Oct 2022 21:04:55 +0200 Subject: [PATCH] Added WebhookInfo Command --- src/Commands/DeleteWebhook.php | 2 +- src/Commands/GetWebhookInfo.php | 64 +++++++++++++++++++++++++++++++++ src/Commands/SetWebhook.php | 2 +- src/Layers/Base.php | 25 ++++++++----- telepathy | 2 ++ 5 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 src/Commands/GetWebhookInfo.php diff --git a/src/Commands/DeleteWebhook.php b/src/Commands/DeleteWebhook.php index a620d6f..c049500 100644 --- a/src/Commands/DeleteWebhook.php +++ b/src/Commands/DeleteWebhook.php @@ -9,7 +9,7 @@ use Telepath\Exceptions\TelegramException; #[AsCommand( - name: 'delete-webhook', description: 'Remove webhook integration.' + name: 'webhook:delete', description: 'Remove webhook integration.' )] class DeleteWebhook extends BotCommand { diff --git a/src/Commands/GetWebhookInfo.php b/src/Commands/GetWebhookInfo.php new file mode 100644 index 0000000..121d512 --- /dev/null +++ b/src/Commands/GetWebhookInfo.php @@ -0,0 +1,64 @@ +configureBotArguments(); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + + $bot = $this->makeBot($input, $output); + + $info = $bot->getWebhookInfo(); + + $table = new Table($output); + $table->setHeaderTitle('WebhookInfo'); + + $table->setHeaders([ + 'url', + 'has_custom_certificate', + 'pending_update_count', + 'ip_address', + 'last_error_date', + 'last_error_message', + 'last_synchronization_error_date', + 'max_connections', + 'allowed_updates', + ]); + + $table->setRow(0, [ + $info->url, + $info->has_custom_certificate ? 'true' : 'false', + $info->pending_update_count, + $info->ip_address, + $info->last_error_date, + $info->last_error_message, + $info->last_synchronization_error_date, + $info->max_connections, + $info->allowed_updates ? implode(', ', $info->allowed_updates) : null, + ]); + + $table->setVertical(); + $table->setStyle('box'); + + $table->render(); + + return self::SUCCESS; + } + +} \ No newline at end of file diff --git a/src/Commands/SetWebhook.php b/src/Commands/SetWebhook.php index 5347cac..620d432 100644 --- a/src/Commands/SetWebhook.php +++ b/src/Commands/SetWebhook.php @@ -13,7 +13,7 @@ use function Termwind\{render}; #[AsCommand( - name: 'set-webhook', + name: 'webhook:set', description: 'Specify a URL an receive incoming updates via an outgoing webhook.' )] class SetWebhook extends BotCommand diff --git a/src/Layers/Base.php b/src/Layers/Base.php index 896dafd..6d534d0 100644 --- a/src/Layers/Base.php +++ b/src/Layers/Base.php @@ -3,18 +3,18 @@ namespace Telepath\Layers; use GuzzleHttp\Client; +use Psr\Http\Message\ResponseInterface; +use Telepath\Exceptions\TelegramException; use Telepath\Telegram\InputMedia; use Telepath\Types\CastsToTelegramTypes; -use Telepath\Exceptions\TelegramException; use Telepath\Types\InputFile; -use Telepath\Telegram\Update; -use Telepath\Telegram\User; abstract class Base { use CastsToTelegramTypes; protected Client $client; + protected ?string $lastApiResult = null; private ?string $proxy = null; public function __construct( @@ -40,8 +40,6 @@ public function disableProxy(): static return $this; } - protected ?string $lastApiResult = null; - public function lastApiResult(): ?string { return $this->lastApiResult; @@ -60,9 +58,11 @@ public function raw(string $method, $data = []): mixed $sendsFiles = $this->hasInputFiles($data); - $response = $sendsFiles - ? $this->sendAsMultipart($method, $data) - : $this->sendAsJson($method, $data); + $response = match (true) { + $sendsFiles => $this->sendAsMultipart($method, $data), + count($data) === 0 => $this->sendAsQuery($method, $data), + default => $this->sendAsJson($method, $data) + }; $json = json_decode($response->getBody()->getContents(), true); if ($json['ok'] === true) { @@ -156,6 +156,15 @@ protected function sendAsForm(string $method, array $data): \Psr\Http\Message\Re ]); } + protected function sendAsQuery(string $method, array $data): ResponseInterface + { + return $this->client->get($method, [ + 'query' => $data, + 'proxy' => $this->proxy, + 'http_errors' => false, + ]); + } + protected function hasInputFiles(array $data): bool { foreach ($data as $key => $value) { diff --git a/telepathy b/telepathy index 12338d3..7e63678 100755 --- a/telepathy +++ b/telepathy @@ -5,6 +5,7 @@ require __DIR__ . '/vendor/autoload.php'; use Symfony\Component\Console\Application; use Telepath\Commands\DeleteWebhook; +use Telepath\Commands\GetWebhookInfo; use Telepath\Commands\SetWebhook; $application = new Application(); @@ -12,6 +13,7 @@ $application = new Application(); $application->addCommands([ new SetWebhook(), new DeleteWebhook(), + new GetWebhookInfo() ]); $application->run(); \ No newline at end of file