|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace CSlant\LaravelTelegramGitNotifier\Services; |
|
6
|
|
|
|
|
7
|
|
|
use CSlant\LaravelTelegramGitNotifier\Traits\Markup; |
|
8
|
|
|
use CSlant\TelegramGitNotifier\Bot; |
|
9
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException; |
|
10
|
|
|
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; |
|
11
|
|
|
use Illuminate\Support\Facades\Config; |
|
12
|
|
|
|
|
13
|
|
|
class CommandService |
|
14
|
|
|
{ |
|
15
|
|
|
use Markup; |
|
16
|
|
|
|
|
17
|
|
|
private const CONFIG_VIEW_NAMESPACE = 'telegram-git-notifier.view.namespace'; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct( |
|
20
|
|
|
private Bot $bot, |
|
21
|
|
|
private string $viewNamespace |
|
22
|
|
|
) { |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function create(Bot $bot): self |
|
26
|
|
|
{ |
|
27
|
|
|
return new self( |
|
28
|
|
|
$bot, |
|
29
|
|
|
(string) Config::get(self::CONFIG_VIEW_NAMESPACE, '') |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Send the start message to the user. |
|
35
|
|
|
* |
|
36
|
|
|
* @throws EntryNotFoundException |
|
37
|
|
|
*/ |
|
38
|
|
|
private function sendStartMessage(): void |
|
39
|
|
|
{ |
|
40
|
|
|
$firstName = $this->bot->telegram->FirstName() ?: 'there'; |
|
41
|
|
|
$reply = view("$this->viewNamespace::tools.start", ['first_name' => $firstName]); |
|
42
|
|
|
$imagePath = __DIR__.'/../../resources/images/telegram-git-notifier-laravel.png'; |
|
43
|
|
|
|
|
44
|
|
|
$this->bot->sendPhoto($imagePath, ['caption' => $reply]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Handle the incoming command. |
|
49
|
|
|
* |
|
50
|
|
|
* @throws EntryNotFoundException |
|
51
|
|
|
* @throws MessageIsEmptyException |
|
52
|
|
|
*/ |
|
53
|
|
|
public function handle(): void |
|
54
|
|
|
{ |
|
55
|
|
|
$text = (string) $this->bot->telegram->Text(); |
|
56
|
|
|
$command = trim($text, '/'); |
|
57
|
|
|
|
|
58
|
|
|
$handlers = [ |
|
59
|
|
|
'start' => fn () => $this->handleStart(), |
|
60
|
|
|
'menu' => fn () => $this->handleMenu(), |
|
61
|
|
|
'settings' => fn () => $this->handleSettings(), |
|
62
|
|
|
'set_menu' => fn () => $this->handleSetMenu(), |
|
63
|
|
|
'default' => fn () => $this->handleDefault($command), |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
$handler = $handlers[$command] ?? $this->handleToolCommand($command) ?? $handlers['default']; |
|
67
|
|
|
$handler(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Handle the start command. |
|
72
|
|
|
* |
|
73
|
|
|
* @throws EntryNotFoundException |
|
74
|
|
|
*/ |
|
75
|
|
|
private function handleStart(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->sendStartMessage(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Handle the menu command. |
|
82
|
|
|
* |
|
83
|
|
|
* @throws EntryNotFoundException |
|
84
|
|
|
* @throws MessageIsEmptyException |
|
85
|
|
|
*/ |
|
86
|
|
|
private function handleMenu(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$this->bot->sendMessage( |
|
89
|
|
|
view("$this->viewNamespace::tools.menu"), |
|
90
|
|
|
['reply_markup' => $this->menuMarkup($this->bot->telegram)] |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Handle the settings command. |
|
96
|
|
|
*/ |
|
97
|
|
|
private function handleSettings(): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->bot->settingHandle(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Handle the set_menu command. |
|
104
|
|
|
* |
|
105
|
|
|
* @throws EntryNotFoundException |
|
106
|
|
|
* @throws MessageIsEmptyException |
|
107
|
|
|
*/ |
|
108
|
|
|
private function handleSetMenu(): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->bot->setMyCommands(self::menuCommands()); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Handle tool commands (token, id, usage, server). |
|
115
|
|
|
* |
|
116
|
|
|
* @return callable|null The handler function or null if not a tool command |
|
117
|
|
|
*/ |
|
118
|
|
|
private function handleToolCommand(string $command): ?callable |
|
119
|
|
|
{ |
|
120
|
|
|
$toolCommands = ['token', 'id', 'usage', 'server']; |
|
121
|
|
|
|
|
122
|
|
|
if (!in_array($command, $toolCommands, true)) { |
|
123
|
|
|
return null; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return function () use ($command): void { |
|
127
|
|
|
$this->bot->sendMessage(view("$this->viewNamespace::tools.{$command}")); |
|
128
|
|
|
}; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Handle unknown commands. |
|
133
|
|
|
* |
|
134
|
|
|
* @throws EntryNotFoundException |
|
135
|
|
|
* @throws MessageIsEmptyException |
|
136
|
|
|
*/ |
|
137
|
|
|
private function handleDefault(string $command): void |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
$this->bot->sendMessage('🤨 '.__('tg-notifier::app.invalid_request')); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the list of available menu commands. |
|
144
|
|
|
* |
|
145
|
|
|
* @return array<array{command: string, description: string}> |
|
146
|
|
|
*/ |
|
147
|
|
|
public static function menuCommands(): array |
|
148
|
|
|
{ |
|
149
|
|
|
return [ |
|
150
|
|
|
self::createCommand('start', 'start'), |
|
151
|
|
|
self::createCommand('menu', 'menu'), |
|
152
|
|
|
self::createCommand('token', 'token'), |
|
153
|
|
|
self::createCommand('id', 'id'), |
|
154
|
|
|
self::createCommand('usage', 'usage'), |
|
155
|
|
|
self::createCommand('server', 'server'), |
|
156
|
|
|
self::createCommand('settings', 'settings'), |
|
157
|
|
|
]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Create a command array with the given name and translation key. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $name The command name without the leading slash |
|
164
|
|
|
* @param string $translationKey The translation key suffix |
|
165
|
|
|
* @return array{command: string, description: string} |
|
166
|
|
|
*/ |
|
167
|
|
|
private static function createCommand(string $name, string $translationKey): array |
|
168
|
|
|
{ |
|
169
|
|
|
return [ |
|
170
|
|
|
'command' => "/$name", |
|
171
|
|
|
'description' => __("tg-notifier::tools/menu.$translationKey"), |
|
172
|
|
|
]; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.