Skip to content

Commit b591cdd

Browse files
authored
Merge pull request #33 from tanhongit/tools
Handle command and callback in the bot tool
2 parents d7cb783 + 378275a commit b591cdd

File tree

11 files changed

+334
-29
lines changed

11 files changed

+334
-29
lines changed

config/telegram-git-notifier.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
base_path('resources/views/vendor/tg-notifier')
1313
),
1414
],
15+
16+
// Set route prefix for telegram git notifier app
17+
'route_prefix' => env('TGN_DEFAULT_ROUTE_PREFIX', 'telegram-git-notifier'),
1518
],
1619

1720
'app' => [

lang/en/app.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
return [
44
'by' => 'by',
5+
'unknown_callback' => 'Unknown Callback. Something went wrong!',
6+
'invalid_request' => '🤨 Invalid Request!',
57
];

lang/en/tools/menu.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
return [
4+
'title' => 'BOT MENU',
5+
6+
'start' => 'Welcome to the bot',
7+
'menu' => 'Show menu of the bot',
8+
'token' => 'Show token of the bot',
9+
'id' => 'Show the ID of the current chat',
10+
'usage' => 'Show step by step usage',
11+
'server' => 'To get Server Information',
12+
'settings' => 'Go to settings of the bot',
13+
'back' => 'Back',
14+
15+
'discussion' => '🗨 Discussion',
16+
'source_code' => '💠 Source Code',
17+
];

phpstan-baseline.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ parameters:
88

99
- message: '#Parameter \#1 \$url of method CSlant\\TelegramGitNotifier\\Webhook\:\:setUrl\(\) expects string, mixed given\.#'
1010
path: src/Http/Actions/WebhookAction.php
11+
12+
- message: '#Parameter \#1 \$prefix of static method Illuminate\\Support\\Facades\\Route::prefix\(\) expects string, mixed given\.#'
13+
path: routes/bot.php
14+
15+
- message: '#Part \$routePrefix \(mixed\) of encapsed string cannot be cast to string\.#'
16+
path: routes/bot.php
17+
18+
- message: '#Property .+ does not accept mixed#'
19+
path: %currentWorkingDirectory%/src/Services/*.php

resources/views/events/github/workflow_run/completed.blade.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,17 @@
22
/**
33
* @var $payload object
44
*/
5-
?>
6-
7-
@switch($payload->workflow_run->conclusion)
8-
@case('success')
9-
{!! __('tg-notifier::events/github/workflow_run.completed.success.title', ['user' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
105
11-
{!! __('tg-notifier::events/github/workflow_run.completed.success.body', ['name' => $payload->workflow_run->name]) !!}
12-
@break
13-
@case('failure')
14-
{!! __('tg-notifier::events/github/workflow_run.completed.failure.title', ['user' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
15-
16-
{!! __('tg-notifier::events/github/workflow_run.completed.failure.body', ['name' => $payload->workflow_run->name]) !!}
17-
@break
18-
@case('cancelled')
19-
{!! __('tg-notifier::events/github/workflow_run.completed.cancelled.title', ['user' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
6+
match ($payload->workflow_run->conclusion) {
7+
'success' => $status = 'success',
8+
'failure' => $status = 'failure',
9+
'cancelled' => $status = 'cancelled',
10+
default => $status = 'default',
11+
};
12+
?>
2013

21-
{!! __('tg-notifier::events/github/workflow_run.completed.cancelled.body', ['name' => $payload->workflow_run->name]) !!}
22-
@break
23-
@default
24-
{!! __('tg-notifier::events/github/workflow_run.completed.default.title', ['user' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
14+
{!! __("tg-notifier::events/github/workflow_run.completed.$status.title", ['user' => "<a href='{$payload->repository->html_url}'>{$payload->repository->full_name}</a>"]) !!}
2515

26-
{!! __('tg-notifier::events/github/workflow_run.completed.default.body', ['name' => $payload->workflow_run->name]) !!}
27-
@break
28-
@endswitch
16+
{!! __("tg-notifier::events/github/workflow_run.completed.$status.body", ['name' => $payload->workflow_run->name]) !!}
2917

3018
{!! __('tg-notifier::events/github/workflow_run.link', ['link' => "<a href='{$payload->workflow_run->html_url}'>{$payload->workflow_run->event} - {$payload->workflow_run->name}</a>"]) !!}

resources/views/tools/menu.blade.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use CSlant\LaravelTelegramGitNotifier\Services\CommandService;
4+
5+
$menuCommands = CommandService::menuCommands() ?? [];
6+
?>
7+
8+
<b>{{ __('tg-notifier::tools/menu.title') }}</b> 🤖
9+
10+
<?php foreach ($menuCommands as $menuCommand) : ?>
11+
<b><?= $menuCommand['command'] ?></b> - <?= $menuCommand['description'] ?>
12+
13+
<?php endforeach; ?>

routes/bot.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
|
1616
*/
1717

18-
Route::prefix('telegram-git-notifier')->group(function () {
19-
Route::any('/', [IndexAction::class, 'index'])->name('telegram-git-notifier.index');
18+
$routePrefix = config('telegram-git-notifier.defaults.route_prefix');
2019

21-
Route::prefix('webhook')->group(function () {
22-
Route::get('/set', [WebhookAction::class, 'set'])->name('telegram-git-notifier.webhook.set');
23-
Route::get('/delete', [WebhookAction::class, 'delete'])->name('telegram-git-notifier.webhook.delete');
24-
Route::get('/info', [WebhookAction::class, 'getWebHookInfo'])->name('telegram-git-notifier.webhook.info');
25-
Route::get('/updates', [WebhookAction::class, 'getUpdates'])->name('telegram-git-notifier.webhook.updates');
20+
Route::prefix($routePrefix)->group(function () use ($routePrefix) {
21+
Route::match(['get', 'post'], '/', IndexAction::class)->name("$routePrefix.index");
22+
23+
Route::prefix('webhook')->group(function () use ($routePrefix) {
24+
Route::get('set', [WebhookAction::class, 'set'])->name("$routePrefix.webhook.set");
25+
Route::get('delete', [WebhookAction::class, 'delete'])->name("$routePrefix.webhook.delete");
26+
Route::get('info', [WebhookAction::class, 'getWebHookInfo'])->name("$routePrefix.webhook.info");
27+
Route::get('updates', [WebhookAction::class, 'getUpdates'])->name("$routePrefix.webhook.updates");
2628
});
2729
});

src/Http/Actions/IndexAction.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace CSlant\LaravelTelegramGitNotifier\Http\Actions;
44

5+
use CSlant\LaravelTelegramGitNotifier\Services\CallbackService;
6+
use CSlant\LaravelTelegramGitNotifier\Services\CommandService;
57
use CSlant\LaravelTelegramGitNotifier\Services\NotificationService;
68
use CSlant\TelegramGitNotifier\Bot;
9+
use CSlant\TelegramGitNotifier\Exceptions\BotException;
10+
use CSlant\TelegramGitNotifier\Exceptions\CallbackException;
711
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
12+
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
813
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
914
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
1015
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
@@ -43,9 +48,26 @@ public function __construct()
4348
* @throws InvalidViewTemplateException
4449
* @throws MessageIsEmptyException
4550
* @throws SendNotificationException
51+
* @throws BotException
52+
* @throws CallbackException
53+
* @throws EntryNotFoundException
4654
*/
47-
public function index(): void
55+
public function __invoke(): void
4856
{
57+
if ($this->bot->isCallback()) {
58+
$callbackAction = new CallbackService($this->bot);
59+
$callbackAction->handle();
60+
61+
return;
62+
}
63+
64+
if ($this->bot->isMessage() && $this->bot->isOwner()) {
65+
$commandAction = new CommandService($this->bot);
66+
$commandAction->handle();
67+
68+
return;
69+
}
70+
4971
$sendNotification = new NotificationService(
5072
$this->notifier,
5173
$this->bot->setting

src/Services/CallbackService.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Services;
4+
5+
use CSlant\LaravelTelegramGitNotifier\Traits\Markup;
6+
use CSlant\TelegramGitNotifier\Bot;
7+
use CSlant\TelegramGitNotifier\Constants\SettingConstant;
8+
use CSlant\TelegramGitNotifier\Exceptions\BotException;
9+
use CSlant\TelegramGitNotifier\Exceptions\CallbackException;
10+
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
11+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
12+
13+
class CallbackService
14+
{
15+
use Markup;
16+
17+
private Bot $bot;
18+
19+
protected string $viewNamespace = '';
20+
21+
public function __construct(Bot $bot)
22+
{
23+
$this->bot = $bot;
24+
$this->viewNamespace = config('telegram-git-notifier.view.namespace');
25+
}
26+
27+
/**
28+
* Answer the back button.
29+
*
30+
* @param string $callback
31+
* @return void
32+
*
33+
* @throws MessageIsEmptyException
34+
* @throws BotException
35+
* @throws CallbackException
36+
*/
37+
public function answerBackButton(string $callback): void
38+
{
39+
$callback = str_replace(SettingConstant::SETTING_BACK, '', $callback);
40+
switch ($callback) {
41+
case 'settings':
42+
$view = view("$this->viewNamespace::tools.settings");
43+
$markup = $this->bot->settingMarkup();
44+
45+
break;
46+
case 'settings.custom_events.github':
47+
$view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'github']);
48+
$markup = $this->bot->eventMarkup();
49+
50+
break;
51+
case 'settings.custom_events.gitlab':
52+
$view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'gitlab']);
53+
$markup = $this->bot->eventMarkup(null, 'gitlab');
54+
55+
break;
56+
case 'menu':
57+
$view = view("$this->viewNamespace::tools.menu");
58+
$markup = $this->menuMarkup($this->bot->telegram);
59+
60+
break;
61+
default:
62+
$this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback'));
63+
64+
return;
65+
}
66+
67+
$this->bot->editMessageText($view, [
68+
'reply_markup' => $markup,
69+
]);
70+
}
71+
72+
/**
73+
* @return void
74+
*
75+
* @throws MessageIsEmptyException
76+
* @throws InvalidViewTemplateException
77+
* @throws BotException|CallbackException
78+
*/
79+
public function handle(): void
80+
{
81+
$callback = $this->bot->telegram->Callback_Data();
82+
83+
if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) {
84+
$this->bot->eventHandle($callback);
85+
86+
return;
87+
}
88+
89+
if (str_contains($callback, SettingConstant::SETTING_BACK)) {
90+
$this->answerBackButton($callback);
91+
92+
return;
93+
}
94+
95+
$callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback);
96+
97+
$settings = $this->bot->setting->getSettings();
98+
if (array_key_exists($callback, $settings)
99+
&& $this->bot->setting->updateSetting(
100+
$callback,
101+
!$settings[$callback]
102+
)
103+
) {
104+
$this->bot->editMessageReplyMarkup([
105+
'reply_markup' => $this->bot->settingMarkup(),
106+
]);
107+
} else {
108+
$this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback'));
109+
}
110+
}
111+
}

src/Services/CommandService.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Services;
4+
5+
use CSlant\LaravelTelegramGitNotifier\Traits\Markup;
6+
use CSlant\TelegramGitNotifier\Bot;
7+
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
8+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
9+
10+
class CommandService
11+
{
12+
use Markup;
13+
14+
private Bot $bot;
15+
16+
protected string $viewNamespace = '';
17+
18+
public function __construct(Bot $bot)
19+
{
20+
$this->bot = $bot;
21+
$this->viewNamespace = config('telegram-git-notifier.view.namespace');
22+
}
23+
24+
/**
25+
* @param Bot $bot
26+
* @return void
27+
*
28+
* @throws EntryNotFoundException
29+
*/
30+
public function sendStartMessage(Bot $bot): void
31+
{
32+
$reply = view(
33+
"$this->viewNamespace::tools.start",
34+
['first_name' => $bot->telegram->FirstName()]
35+
);
36+
$bot->sendPhoto(
37+
__DIR__.'/../../resources/images/start.png',
38+
['caption' => $reply]
39+
);
40+
}
41+
42+
/**
43+
* @return void
44+
*
45+
* @throws EntryNotFoundException
46+
* @throws MessageIsEmptyException
47+
*/
48+
public function handle(): void
49+
{
50+
$text = $this->bot->telegram->Text();
51+
52+
switch ($text) {
53+
case '/start':
54+
$this->sendStartMessage($this->bot);
55+
56+
break;
57+
case '/menu':
58+
$this->bot->sendMessage(
59+
view("$this->viewNamespace::tools.menu"),
60+
['reply_markup' => $this->menuMarkup($this->bot->telegram)]
61+
);
62+
63+
break;
64+
case '/token':
65+
case '/id':
66+
case '/usage':
67+
case '/server':
68+
$this->bot->sendMessage(view("$this->viewNamespace::tools.".trim($text, '/')));
69+
70+
break;
71+
case '/settings':
72+
$this->bot->settingHandle();
73+
74+
break;
75+
case '/set_menu':
76+
$this->bot->setMyCommands(self::menuCommands());
77+
78+
break;
79+
default:
80+
$this->bot->sendMessage(__('tg-notifier::app.invalid_request'));
81+
}
82+
}
83+
84+
/**
85+
* @return array[]
86+
*/
87+
public static function menuCommands(): array
88+
{
89+
return [
90+
[
91+
'command' => '/start',
92+
'description' => __('tg-notifier::tools/menu.start'),
93+
], [
94+
'command' => '/menu',
95+
'description' => __('tg-notifier::tools/menu.menu'),
96+
], [
97+
'command' => '/token',
98+
'description' => __('tg-notifier::tools/menu.token'),
99+
], [
100+
'command' => '/id',
101+
'description' => __('tg-notifier::tools/menu.id'),
102+
], [
103+
'command' => '/usage',
104+
'description' => __('tg-notifier::tools/menu.usage'),
105+
], [
106+
'command' => '/server',
107+
'description' => __('tg-notifier::tools/menu.server'),
108+
], [
109+
'command' => '/settings',
110+
'description' => __('tg-notifier::tools/menu.settings'),
111+
],
112+
];
113+
}
114+
}

0 commit comments

Comments
 (0)