Skip to content

Commit

Permalink
Adds delete-webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
TiiFuchs committed Oct 6, 2022
1 parent 61512c2 commit 5f8ba6b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
45 changes: 45 additions & 0 deletions src/Commands/DeleteWebhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Telepath\Commands;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Telepath\Exceptions\TelegramException;

#[AsCommand(
name: 'delete-webhook', description: 'Remove webhook integration.'
)]
class DeleteWebhook extends BotCommand
{

protected function configure()
{
$this->configureBotArguments();

$this->addOption('drop-pending-updates', 'd', InputOption::VALUE_NONE, 'Drop all pending updates');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$bot = $this->makeBot($input, $output);

$dropPendingUpdates = $input->getOption('drop-pending-updates') ?: null;

try {
$bot->deleteWebhook(
drop_pending_updates: $dropPendingUpdates
);
} catch (TelegramException $e) {
\Termwind\render('<div class="error">' . $e->getMessage() . '</div>');
return self::FAILURE;
}

\Termwind\render('<div class="success">' . $bot->lastApiResult() . '</div>');
return self::SUCCESS;
}

}
10 changes: 3 additions & 7 deletions src/Commands/SetWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#[AsCommand(
name: 'set-webhook',
description: 'Registers a webhook with Telegram servers.'
description: 'Specify a URL an receive incoming updates via an outgoing webhook.'
)]
class SetWebhook extends BotCommand
{
Expand Down Expand Up @@ -64,15 +64,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
secret_token: $secretToken
);
} catch (TelegramException $e) {
render(<<<HTML
<div class="error">{$e->getMessage()}</div>
HTML);
render('<div class="error">' . $e->getMessage() . '</div>');
return Command::FAILURE;
}

render(<<<'HTML'
<div class="success">Webhook was set successfully.</div>
HTML);
render('<div class="success">' . $bot->lastApiResult() . '</div>');
return Command::SUCCESS;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Layers/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public function disableProxy(): static
return $this;
}

protected ?string $lastApiResult = null;

public function lastApiResult(): ?string
{
return $this->lastApiResult;
}

public function raw(string $method, $data = []): mixed
{
$parameters = array_map(
Expand All @@ -59,6 +66,8 @@ public function raw(string $method, $data = []): mixed

$json = json_decode($response->getBody()->getContents(), true);
if ($json['ok'] === true) {
$this->lastApiResult = $json['description'] ?? null;

$method = new \ReflectionMethod($this, $method);
preg_match('/@return (.+)\[]\n/u', $method->getDocComment(), $matches);

Expand Down
2 changes: 2 additions & 0 deletions telepathy
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Telepath\Commands\DeleteWebhook;
use Telepath\Commands\SetWebhook;

$application = new Application();

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

$application->run();

0 comments on commit 5f8ba6b

Please sign in to comment.