Skip to content

Commit

Permalink
Merge pull request #58 from LuongTienThinh/feat/webhook-command
Browse files Browse the repository at this point in the history
feat: set webhook using cli
  • Loading branch information
tanhongit authored Jan 25, 2024
2 parents 4f1818c + 984c930 commit 9a2d9f7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ parameters:
path: src/Providers/TelegramGitNotifierServiceProvider.php

- message: '#Parameter \#1 \$token of method CSlant\\TelegramGitNotifier\\Webhook\:\:setToken\(\) expects string, mixed given\.#'
path: src/Http/Actions/WebhookAction.php
path: src/Services/WebhookService.php

- message: '#Parameter \#1 \$url of method CSlant\\TelegramGitNotifier\\Webhook\:\:setUrl\(\) expects string, mixed given\.#'
path: src/Http/Actions/WebhookAction.php
path: src/Services/WebhookService.php

- message: '#Parameter \#1 \$prefix of static method Illuminate\\Support\\Facades\\Route::prefix\(\) expects string, mixed given\.#'
path: routes/bot.php
Expand Down
40 changes: 40 additions & 0 deletions src/Commands/SetWebhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\Commands;

use CSlant\LaravelTelegramGitNotifier\Services\WebhookService;
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
use Illuminate\Console\Command;

class SetWebhook extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tg-notifier:webhook:set';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Set webhook';

/**
* Execute the console command.
*
* @return void
*/
public function handle(): void
{
try {
$webhookService = new WebhookService();

$this->info($webhookService->setWebhook());
} catch (WebhookException $e) {
$this->error($e->getMessage());
}
}
}
16 changes: 7 additions & 9 deletions src/Http/Actions/WebhookAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;

use CSlant\LaravelTelegramGitNotifier\Services\WebhookService;
use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
use CSlant\TelegramGitNotifier\Webhook;

class WebhookAction
{
protected Webhook $webhook;
protected WebhookService $webhookService;

public function __construct()
{
$this->webhook = new Webhook();
$this->webhook->setToken(config('telegram-git-notifier.bot.token'));
$this->webhook->setUrl(config('telegram-git-notifier.app.url'));
$this->webhookService = new WebhookService();
}

/**
Expand All @@ -25,7 +23,7 @@ public function __construct()
*/
public function set(): string
{
return $this->webhook->setWebhook();
return $this->webhookService->setWebhook();
}

/**
Expand All @@ -37,7 +35,7 @@ public function set(): string
*/
public function delete(): string
{
return $this->webhook->deleteWebHook();
return $this->webhookService->deleteWebHook();
}

/**
Expand All @@ -49,7 +47,7 @@ public function delete(): string
*/
public function getUpdates(): string
{
return $this->webhook->getUpdates();
return $this->webhookService->getUpdates();
}

/**
Expand All @@ -61,6 +59,6 @@ public function getUpdates(): string
*/
public function getWebHookInfo(): string
{
return $this->webhook->getWebHookInfo();
return $this->webhookService->getWebHookInfo();
}
}
2 changes: 2 additions & 0 deletions src/Providers/TelegramGitNotifierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CSlant\LaravelTelegramGitNotifier\Providers;

use CSlant\LaravelTelegramGitNotifier\Commands\ChangeOwnerConfigJson;
use CSlant\LaravelTelegramGitNotifier\Commands\SetWebhook;
use Illuminate\Support\ServiceProvider;

class TelegramGitNotifierServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -59,6 +60,7 @@ protected function registerCommands(): void
{
$this->commands([
ChangeOwnerConfigJson::class,
SetWebhook::class,
]);
}

Expand Down
66 changes: 66 additions & 0 deletions src/Services/WebhookService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\Services;

use CSlant\TelegramGitNotifier\Exceptions\WebhookException;
use CSlant\TelegramGitNotifier\Webhook;

class WebhookService
{
protected Webhook $webhook;

public function __construct(?Webhook $webhook = null)
{
$this->webhook = $webhook ?? new Webhook();
$this->webhook->setToken(config('telegram-git-notifier.bot.token'));
$this->webhook->setUrl(config('telegram-git-notifier.app.url'));
}

/**
* Set webhook for telegram bot.
*
* @return string
*
* @throws WebhookException
*/
public function setWebhook(): string
{
return $this->webhook->setWebhook();
}

/**
* Delete webhook for telegram bot.
*
* @return string
*
* @throws WebhookException
*/
public function deleteWebHook(): string
{
return $this->webhook->deleteWebHook();
}

/**
* Get webhook update.
*
* @return string
*
* @throws WebhookException
*/
public function getUpdates(): string
{
return $this->webhook->getUpdates();
}

/**
* Get webhook info.
*
* @return string
*
* @throws WebhookException
*/
public function getWebHookInfo(): string
{
return $this->webhook->getWebHookInfo();
}
}

0 comments on commit 9a2d9f7

Please sign in to comment.