Skip to content

Commit

Permalink
Merge pull request #33 from tanhongit/tools
Browse files Browse the repository at this point in the history
Handle command and callback in the bot tool
  • Loading branch information
tanhongit authored Nov 23, 2023
2 parents d7cb783 + 378275a commit b591cdd
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 29 deletions.
3 changes: 3 additions & 0 deletions config/telegram-git-notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
2 changes: 2 additions & 0 deletions lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

return [
'by' => 'by',
'unknown_callback' => 'Unknown Callback. Something went wrong!',
'invalid_request' => '🤨 Invalid Request!',
];
17 changes: 17 additions & 0 deletions lang/en/tools/menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'title' => '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',
];
9 changes: 9 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 9 additions & 21 deletions resources/views/events/github/workflow_run/completed.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,17 @@
/**
* @var $payload object
*/
?>

@switch($payload->workflow_run->conclusion)
@case('success')
{!! __('tg-notifier::events/github/workflow_run.completed.success.title', ['user' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
{!! __('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' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}

{!! __('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' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
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' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
{!! __("tg-notifier::events/github/workflow_run.completed.$status.title", ['user' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}

{!! __('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' => "<a href='{$payload->workflow_run->html_url}'>{$payload->workflow_run->event} - {$payload->workflow_run->name}</a>"]) !!}
13 changes: 13 additions & 0 deletions resources/views/tools/menu.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
use CSlant\LaravelTelegramGitNotifier\Services\CommandService;
$menuCommands = CommandService::menuCommands() ?? [];
?>

<b>{{ __('tg-notifier::tools/menu.title') }}</b> 🤖

<?php foreach ($menuCommands as $menuCommand) : ?>
<b><?= $menuCommand['command'] ?></b> - <?= $menuCommand['description'] ?>

<?php endforeach; ?>
16 changes: 9 additions & 7 deletions routes/bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
24 changes: 23 additions & 1 deletion src/Http/Actions/IndexAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
111 changes: 111 additions & 0 deletions src/Services/CallbackService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\Services;

use CSlant\LaravelTelegramGitNotifier\Traits\Markup;
use CSlant\TelegramGitNotifier\Bot;
use CSlant\TelegramGitNotifier\Constants\SettingConstant;
use CSlant\TelegramGitNotifier\Exceptions\BotException;
use CSlant\TelegramGitNotifier\Exceptions\CallbackException;
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;

class CallbackService
{
use Markup;

private Bot $bot;

protected string $viewNamespace = '';

public function __construct(Bot $bot)
{
$this->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'));
}
}
}
114 changes: 114 additions & 0 deletions src/Services/CommandService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace CSlant\LaravelTelegramGitNotifier\Services;

use CSlant\LaravelTelegramGitNotifier\Traits\Markup;
use CSlant\TelegramGitNotifier\Bot;
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;

class CommandService
{
use Markup;

private Bot $bot;

protected string $viewNamespace = '';

public function __construct(Bot $bot)
{
$this->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'),
],
];
}
}
Loading

0 comments on commit b591cdd

Please sign in to comment.