-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathHelpCommand.php
145 lines (120 loc) · 4.44 KB
/
HelpCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/**
* This file is part of the PHP Telegram Support Bot.
*
* (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace PhpTelegramBot\Core\Commands\UserCommands;
use PhpTelegramBot\Core\Commands\Command;
use PhpTelegramBot\Core\Commands\UserCommand;
use PhpTelegramBot\Core\Entities\ServerResponse;
use PhpTelegramBot\Core\Exception\TelegramException;
/**
* User "/help" command
*
* Command that lists all available commands and displays them in User and Admin sections.
*/
class HelpCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'help';
/**
* @var string
*/
protected $description = 'Show bot commands help';
/**
* @var string
*/
protected $usage = '/help or /help <command>';
/**
* @var string
*/
protected $version = '0.1.0';
/**
* @var bool
*/
protected $private_only = true;
/**
* @inheritdoc
*/
public function execute(): ServerResponse
{
$message = $this->getMessage();
$command_str = trim($message->getText(true));
$text = <<<EOT
Hello {$message->getChat()->tryMention(true)}!
Please feel free to ask your questions about the PHP Telegram Bot library.
Keep in mind that the support group is English only.
(Go to group: @PHP\_Telegram\_Bot\_Support)
Below you can see the available commands of this bot.
EOT;
// Admin commands shouldn't be shown in group chats
$safe_to_show = $message->getChat()->isPrivateChat();
$extra_data = ['parse_mode' => 'markdown'];
[$all_commands, $user_commands, $admin_commands] = $this->getUserAdminCommands();
// If no command parameter is passed, show the list.
if ($command_str === '') {
$text .= '*Commands List*:' . PHP_EOL;
foreach ($user_commands as $user_command) {
$text .= '/' . $user_command->getName() . ' - ' . $user_command->getDescription() . PHP_EOL;
}
if ($safe_to_show && count($admin_commands) > 0) {
$text .= PHP_EOL . '*Admin Commands List*:' . PHP_EOL;
foreach ($admin_commands as $admin_command) {
$text .= '/' . $admin_command->getName() . ' - ' . $admin_command->getDescription() . PHP_EOL;
}
}
$text .= PHP_EOL . 'For exact command help, type: /help <command>';
return $this->replyToChat($text, $extra_data);
}
$command_str = str_replace('/', '', $command_str);
if (isset($all_commands[$command_str]) && ($safe_to_show || !$all_commands[$command_str]->isAdminCommand())) {
$command = $all_commands[$command_str];
$text = sprintf(
'Command: %s (v%s)' . PHP_EOL .
'Description: %s' . PHP_EOL .
'Usage: %s',
$command->getName(),
$command->getVersion(),
$command->getDescription(),
$command->getUsage()
);
return $this->replyToChat($text, $extra_data);
}
$text = "No help available: Command /{$command_str} not found";
return $this->replyToChat($text, $extra_data);
}
/**
* Get all available User and Admin commands to display in the help list.
*
* @return Command[][]
* @throws TelegramException
*/
protected function getUserAdminCommands(): array
{
// Only get enabled Admin and User commands that are allowed to be shown.
/** @var Command[] $commands */
$commands = array_filter($this->telegram->getCommandsList(), static function ($command) {
/** @var Command $command */
return !$command->isSystemCommand() && $command->showInHelp() && $command->isEnabled();
});
ksort($commands);
$user_commands = array_filter($commands, static function ($command) {
/** @var Command $command */
return $command->isUserCommand();
});
ksort($user_commands);
$admin_commands = array_filter($commands, static function ($command) {
/** @var Command $command */
return $command->isAdminCommand();
});
ksort($admin_commands);
return [$commands, $user_commands, $admin_commands];
}
}