1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpTelegramBot\Laravel\Commands; |
6
|
|
|
|
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use Longman\TelegramBot\Exception\TelegramException; |
9
|
|
|
use PhpTelegramBot\Laravel\PhpTelegramBotContract; |
10
|
|
|
|
11
|
|
|
class WebhookCommand extends Command |
12
|
|
|
{ |
13
|
|
|
protected $signature = 'telegram:webhook {webhook?} |
14
|
|
|
{--delete : Delete webhook}'; |
15
|
|
|
|
16
|
|
|
protected $description = 'Set or delete webhook for Telegram bot'; |
17
|
|
|
|
18
|
|
|
/** @var \PhpTelegramBot\Laravel\PhpTelegramBotContract */ |
19
|
|
|
protected $telegramBot; |
20
|
|
|
|
21
|
|
|
public function __construct(PhpTelegramBotContract $telegramBot) |
22
|
|
|
{ |
23
|
|
|
parent::__construct(); |
24
|
|
|
|
25
|
|
|
$this->telegramBot = $telegramBot; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function handle() |
29
|
|
|
{ |
30
|
|
|
$webhook = $this->argument('webhook'); |
31
|
|
|
$delete = $this->option('delete'); |
32
|
|
|
|
33
|
|
|
if (! ($webhook || $delete)) { |
34
|
|
|
$this->error('Not enough arguments!'); |
35
|
|
|
$this->error('php artisan telegram:webhook {webhook?} {--delete}'); |
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($delete) { |
40
|
|
|
try { |
41
|
|
|
$this->telegramBot->deleteWebhook(); |
42
|
|
|
$this->info('Webhook deleted succesfully!'); |
43
|
|
|
} catch (TelegramException $e) { |
44
|
|
|
$this->error("Couldn't delete webhook"); |
45
|
|
|
$this->error($e->getMessage()); |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
if ($webhook) { |
52
|
|
|
try { |
53
|
|
|
$this->telegramBot->setWebhook($webhook); |
54
|
|
|
$this->info('Webhook set succesfully!'); |
55
|
|
|
} catch (TelegramException $e) { |
56
|
|
|
$this->error("Couldn't set webhook"); |
57
|
|
|
$this->error($e->getMessage()); |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->info('All done!'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|