diff --git a/config/telegram-git-notifier.php b/config/telegram-git-notifier.php index 1920ebb..2621b01 100644 --- a/config/telegram-git-notifier.php +++ b/config/telegram-git-notifier.php @@ -12,6 +12,9 @@ base_path('resources/views/vendor/tg-notifier') ), ], + + // Set route prefix for telegram git notifier app + 'route_prefix' => env('TGN_DEFAULT_ROUTE_PREFIX', 'telegram-git-notifier'), ], 'app' => [ diff --git a/lang/en/app.php b/lang/en/app.php index 63a8747..0a75f3a 100644 --- a/lang/en/app.php +++ b/lang/en/app.php @@ -2,4 +2,6 @@ return [ 'by' => 'by', + 'unknown_callback' => 'Unknown Callback. Something went wrong!', + 'invalid_request' => '๐Ÿคจ Invalid Request!', ]; diff --git a/lang/en/tools/menu.php b/lang/en/tools/menu.php new file mode 100644 index 0000000..fa225a2 --- /dev/null +++ b/lang/en/tools/menu.php @@ -0,0 +1,17 @@ + 'BOT MENU', + + 'start' => 'Welcome to the bot', + 'menu' => 'Show menu of the bot', + 'token' => 'Show token of the bot', + 'id' => 'Show the ID of the current chat', + 'usage' => 'Show step by step usage', + 'server' => 'To get Server Information', + 'settings' => 'Go to settings of the bot', + 'back' => 'Back', + + 'discussion' => '๐Ÿ—จ Discussion', + 'source_code' => '๐Ÿ’  Source Code', +]; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 002d23b..2cfb5a2 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -8,3 +8,12 @@ parameters: - message: '#Parameter \#1 \$url of method CSlant\\TelegramGitNotifier\\Webhook\:\:setUrl\(\) expects string, mixed given\.#' path: src/Http/Actions/WebhookAction.php + + - message: '#Parameter \#1 \$prefix of static method Illuminate\\Support\\Facades\\Route::prefix\(\) expects string, mixed given\.#' + path: routes/bot.php + + - message: '#Part \$routePrefix \(mixed\) of encapsed string cannot be cast to string\.#' + path: routes/bot.php + + - message: '#Property .+ does not accept mixed#' + path: %currentWorkingDirectory%/src/Services/*.php diff --git a/resources/views/events/github/workflow_run/completed.blade.php b/resources/views/events/github/workflow_run/completed.blade.php index 2b564e1..079ed5d 100644 --- a/resources/views/events/github/workflow_run/completed.blade.php +++ b/resources/views/events/github/workflow_run/completed.blade.php @@ -2,29 +2,17 @@ /** * @var $payload object */ -?> - -@switch($payload->workflow_run->conclusion) - @case('success') -{!! __('tg-notifier::events/github/workflow_run.completed.success.title', ['user' => "{$payload->repository->full_name}"]) !!} -{!! __('tg-notifier::events/github/workflow_run.completed.success.body', ['name' => $payload->workflow_run->name]) !!} - @break - @case('failure') -{!! __('tg-notifier::events/github/workflow_run.completed.failure.title', ['user' => "{$payload->repository->full_name}"]) !!} - -{!! __('tg-notifier::events/github/workflow_run.completed.failure.body', ['name' => $payload->workflow_run->name]) !!} - @break - @case('cancelled') -{!! __('tg-notifier::events/github/workflow_run.completed.cancelled.title', ['user' => "{$payload->repository->full_name}"]) !!} +match ($payload->workflow_run->conclusion) { + 'success' => $status = 'success', + 'failure' => $status = 'failure', + 'cancelled' => $status = 'cancelled', + default => $status = 'default', +}; +?> -{!! __('tg-notifier::events/github/workflow_run.completed.cancelled.body', ['name' => $payload->workflow_run->name]) !!} - @break - @default -{!! __('tg-notifier::events/github/workflow_run.completed.default.title', ['user' => "{$payload->repository->full_name}"]) !!} +{!! __("tg-notifier::events/github/workflow_run.completed.$status.title", ['user' => "{$payload->repository->full_name}"]) !!} -{!! __('tg-notifier::events/github/workflow_run.completed.default.body', ['name' => $payload->workflow_run->name]) !!} - @break -@endswitch +{!! __("tg-notifier::events/github/workflow_run.completed.$status.body", ['name' => $payload->workflow_run->name]) !!} {!! __('tg-notifier::events/github/workflow_run.link', ['link' => "{$payload->workflow_run->event} - {$payload->workflow_run->name}"]) !!} diff --git a/resources/views/tools/menu.blade.php b/resources/views/tools/menu.blade.php new file mode 100644 index 0000000..b7648aa --- /dev/null +++ b/resources/views/tools/menu.blade.php @@ -0,0 +1,13 @@ + + +{{ __('tg-notifier::tools/menu.title') }} ๐Ÿค– + + + - + + diff --git a/routes/bot.php b/routes/bot.php index 81cf4a9..f216871 100644 --- a/routes/bot.php +++ b/routes/bot.php @@ -15,13 +15,15 @@ | */ -Route::prefix('telegram-git-notifier')->group(function () { - Route::any('/', [IndexAction::class, 'index'])->name('telegram-git-notifier.index'); +$routePrefix = config('telegram-git-notifier.defaults.route_prefix'); - Route::prefix('webhook')->group(function () { - Route::get('/set', [WebhookAction::class, 'set'])->name('telegram-git-notifier.webhook.set'); - Route::get('/delete', [WebhookAction::class, 'delete'])->name('telegram-git-notifier.webhook.delete'); - Route::get('/info', [WebhookAction::class, 'getWebHookInfo'])->name('telegram-git-notifier.webhook.info'); - Route::get('/updates', [WebhookAction::class, 'getUpdates'])->name('telegram-git-notifier.webhook.updates'); +Route::prefix($routePrefix)->group(function () use ($routePrefix) { + Route::match(['get', 'post'], '/', IndexAction::class)->name("$routePrefix.index"); + + Route::prefix('webhook')->group(function () use ($routePrefix) { + Route::get('set', [WebhookAction::class, 'set'])->name("$routePrefix.webhook.set"); + Route::get('delete', [WebhookAction::class, 'delete'])->name("$routePrefix.webhook.delete"); + Route::get('info', [WebhookAction::class, 'getWebHookInfo'])->name("$routePrefix.webhook.info"); + Route::get('updates', [WebhookAction::class, 'getUpdates'])->name("$routePrefix.webhook.updates"); }); }); diff --git a/src/Http/Actions/IndexAction.php b/src/Http/Actions/IndexAction.php index 1af94c9..d57e2f9 100644 --- a/src/Http/Actions/IndexAction.php +++ b/src/Http/Actions/IndexAction.php @@ -2,9 +2,14 @@ namespace CSlant\LaravelTelegramGitNotifier\Http\Actions; +use CSlant\LaravelTelegramGitNotifier\Services\CallbackService; +use CSlant\LaravelTelegramGitNotifier\Services\CommandService; use CSlant\LaravelTelegramGitNotifier\Services\NotificationService; use CSlant\TelegramGitNotifier\Bot; +use CSlant\TelegramGitNotifier\Exceptions\BotException; +use CSlant\TelegramGitNotifier\Exceptions\CallbackException; use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException; +use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException; use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException; @@ -43,9 +48,26 @@ public function __construct() * @throws InvalidViewTemplateException * @throws MessageIsEmptyException * @throws SendNotificationException + * @throws BotException + * @throws CallbackException + * @throws EntryNotFoundException */ - public function index(): void + public function __invoke(): void { + if ($this->bot->isCallback()) { + $callbackAction = new CallbackService($this->bot); + $callbackAction->handle(); + + return; + } + + if ($this->bot->isMessage() && $this->bot->isOwner()) { + $commandAction = new CommandService($this->bot); + $commandAction->handle(); + + return; + } + $sendNotification = new NotificationService( $this->notifier, $this->bot->setting diff --git a/src/Services/CallbackService.php b/src/Services/CallbackService.php new file mode 100644 index 0000000..d21c283 --- /dev/null +++ b/src/Services/CallbackService.php @@ -0,0 +1,111 @@ +bot = $bot; + $this->viewNamespace = config('telegram-git-notifier.view.namespace'); + } + + /** + * Answer the back button. + * + * @param string $callback + * @return void + * + * @throws MessageIsEmptyException + * @throws BotException + * @throws CallbackException + */ + public function answerBackButton(string $callback): void + { + $callback = str_replace(SettingConstant::SETTING_BACK, '', $callback); + switch ($callback) { + case 'settings': + $view = view("$this->viewNamespace::tools.settings"); + $markup = $this->bot->settingMarkup(); + + break; + case 'settings.custom_events.github': + $view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'github']); + $markup = $this->bot->eventMarkup(); + + break; + case 'settings.custom_events.gitlab': + $view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'gitlab']); + $markup = $this->bot->eventMarkup(null, 'gitlab'); + + break; + case 'menu': + $view = view("$this->viewNamespace::tools.menu"); + $markup = $this->menuMarkup($this->bot->telegram); + + break; + default: + $this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback')); + + return; + } + + $this->bot->editMessageText($view, [ + 'reply_markup' => $markup, + ]); + } + + /** + * @return void + * + * @throws MessageIsEmptyException + * @throws InvalidViewTemplateException + * @throws BotException|CallbackException + */ + public function handle(): void + { + $callback = $this->bot->telegram->Callback_Data(); + + if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) { + $this->bot->eventHandle($callback); + + return; + } + + if (str_contains($callback, SettingConstant::SETTING_BACK)) { + $this->answerBackButton($callback); + + return; + } + + $callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback); + + $settings = $this->bot->setting->getSettings(); + if (array_key_exists($callback, $settings) + && $this->bot->setting->updateSetting( + $callback, + !$settings[$callback] + ) + ) { + $this->bot->editMessageReplyMarkup([ + 'reply_markup' => $this->bot->settingMarkup(), + ]); + } else { + $this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback')); + } + } +} diff --git a/src/Services/CommandService.php b/src/Services/CommandService.php new file mode 100644 index 0000000..a08fd69 --- /dev/null +++ b/src/Services/CommandService.php @@ -0,0 +1,114 @@ +bot = $bot; + $this->viewNamespace = config('telegram-git-notifier.view.namespace'); + } + + /** + * @param Bot $bot + * @return void + * + * @throws EntryNotFoundException + */ + public function sendStartMessage(Bot $bot): void + { + $reply = view( + "$this->viewNamespace::tools.start", + ['first_name' => $bot->telegram->FirstName()] + ); + $bot->sendPhoto( + __DIR__.'/../../resources/images/start.png', + ['caption' => $reply] + ); + } + + /** + * @return void + * + * @throws EntryNotFoundException + * @throws MessageIsEmptyException + */ + public function handle(): void + { + $text = $this->bot->telegram->Text(); + + switch ($text) { + case '/start': + $this->sendStartMessage($this->bot); + + break; + case '/menu': + $this->bot->sendMessage( + view("$this->viewNamespace::tools.menu"), + ['reply_markup' => $this->menuMarkup($this->bot->telegram)] + ); + + break; + case '/token': + case '/id': + case '/usage': + case '/server': + $this->bot->sendMessage(view("$this->viewNamespace::tools.".trim($text, '/'))); + + break; + case '/settings': + $this->bot->settingHandle(); + + break; + case '/set_menu': + $this->bot->setMyCommands(self::menuCommands()); + + break; + default: + $this->bot->sendMessage(__('tg-notifier::app.invalid_request')); + } + } + + /** + * @return array[] + */ + public static function menuCommands(): array + { + return [ + [ + 'command' => '/start', + 'description' => __('tg-notifier::tools/menu.start'), + ], [ + 'command' => '/menu', + 'description' => __('tg-notifier::tools/menu.menu'), + ], [ + 'command' => '/token', + 'description' => __('tg-notifier::tools/menu.token'), + ], [ + 'command' => '/id', + 'description' => __('tg-notifier::tools/menu.id'), + ], [ + 'command' => '/usage', + 'description' => __('tg-notifier::tools/menu.usage'), + ], [ + 'command' => '/server', + 'description' => __('tg-notifier::tools/menu.server'), + ], [ + 'command' => '/settings', + 'description' => __('tg-notifier::tools/menu.settings'), + ], + ]; + } +} diff --git a/src/Traits/Markup.php b/src/Traits/Markup.php new file mode 100644 index 0000000..0cd09d1 --- /dev/null +++ b/src/Traits/Markup.php @@ -0,0 +1,24 @@ +buildInlineKeyBoardButton(__('tg-notifier::tools/menu.discussion'), config('telegram-git-notifier.author.discussion')), + ], [ + $telegram->buildInlineKeyBoardButton(__('tg-notifier::tools/menu.source_code'), config('telegram-git-notifier.author.source_code')), + ], + ]; + } +}