Skip to content

Webhook command #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Laravel/Commands/WebhookCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace PhpTelegramBot\Laravel\Commands;

use Illuminate\Console\Command;
use Longman\TelegramBot\Exception\TelegramException;
use PhpTelegramBot\Laravel\PhpTelegramBotContract;

class WebhookCommand extends Command
{
protected $signature = 'telegram:webhook {webhook?}
{--delete : Delete webhook}';

protected $description = 'Set or delete webhook for Telegram bot';

/** @var \PhpTelegramBot\Laravel\PhpTelegramBotContract */
protected $telegramBot;

public function __construct(PhpTelegramBotContract $telegramBot)
{
parent::__construct();

$this->telegramBot = $telegramBot;
}

public function handle()
{
$webhook = $this->argument('webhook');
$delete = $this->option('delete');

if (! ($webhook || $delete)) {
$this->error('Not enough arguments!');
$this->error('php artisan telegram:webhook {webhook?} {--delete}');
return;
}

if ($delete) {
try {
$this->telegramBot->deleteWebhook();
$this->info('Webhook deleted succesfully!');
} catch (TelegramException $e) {
$this->error("Couldn't delete webhook");
$this->error($e->getMessage());
return;
}
}


if ($webhook) {
try {
$this->telegramBot->setWebhook($webhook);
$this->info('Webhook set succesfully!');
} catch (TelegramException $e) {
$this->error("Couldn't set webhook");
$this->error($e->getMessage());
return;
}
}

$this->info('All done!');
}
}
7 changes: 7 additions & 0 deletions src/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace PhpTelegramBot\Laravel;

use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
use PhpTelegramBot\Laravel\Commands\WebhookCommand;

class ServiceProvider extends LaravelServiceProvider
{
Expand Down Expand Up @@ -92,6 +93,12 @@ public function register()

return $bot;
});

if ($this->app->runningInConsole()) {
$this->commands([
WebhookCommand::class,
]);
}
}

/**
Expand Down