Skip to content

Commit

Permalink
Added WebhookInfo Command
Browse files Browse the repository at this point in the history
  • Loading branch information
TiiFuchs committed Oct 6, 2022
1 parent 5f8ba6b commit 6811fa1
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Commands/DeleteWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
64 changes: 64 additions & 0 deletions src/Commands/GetWebhookInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Telepath\Commands;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'webhook:info',
description: 'Get current webhook status.'
)]
class GetWebhookInfo extends BotCommand
{
protected function configure()
{
$this->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;
}

}
2 changes: 1 addition & 1 deletion src/Commands/SetWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 17 additions & 8 deletions src/Layers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -40,8 +40,6 @@ public function disableProxy(): static
return $this;
}

protected ?string $lastApiResult = null;

public function lastApiResult(): ?string
{
return $this->lastApiResult;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions telepathy
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ 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();

$application->addCommands([
new SetWebhook(),
new DeleteWebhook(),
new GetWebhookInfo()
]);

$application->run();

0 comments on commit 6811fa1

Please sign in to comment.