You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.1 KiB

1 year ago
  1. <?php
  2. /**
  3. * This file is part of the PHP Telegram Bot example-bot package.
  4. * https://github.com/php-telegram-bot/example-bot/
  5. *
  6. * (c) PHP Telegram Bot Team
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * User "/help" command
  13. *
  14. * Command that lists all available commands and displays them in User and Admin sections.
  15. */
  16. namespace Longman\TelegramBot\Commands\UserCommands;
  17. use Longman\TelegramBot\Commands\Command;
  18. use Longman\TelegramBot\Commands\UserCommand;
  19. use Longman\TelegramBot\Entities\ServerResponse;
  20. use Longman\TelegramBot\Exception\TelegramException;
  21. class HelpCommand extends UserCommand
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $name = 'help';
  27. /**
  28. * @var string
  29. */
  30. protected $description = 'Show bot commands help';
  31. /**
  32. * @var string
  33. */
  34. protected $usage = '/help or /help <command>';
  35. /**
  36. * @var string
  37. */
  38. protected $version = '1.4.0';
  39. /**
  40. * Main command execution
  41. *
  42. * @return ServerResponse
  43. * @throws TelegramException
  44. */
  45. public function execute(): ServerResponse
  46. {
  47. $message = $this->getMessage();
  48. $command_str = trim($message->getText(true));
  49. // Admin commands shouldn't be shown in group chats
  50. $safe_to_show = $message->getChat()->isPrivateChat();
  51. [$all_commands, $user_commands, $admin_commands] = $this->getUserAndAdminCommands();
  52. // If no command parameter is passed, show the list.
  53. if ($command_str === '') {
  54. $text = '*Commands List*:' . PHP_EOL;
  55. foreach ($user_commands as $user_command) {
  56. $text .= '/' . $user_command->getName() . ' - ' . $user_command->getDescription() . PHP_EOL;
  57. }
  58. if ($safe_to_show && count($admin_commands) > 0) {
  59. $text .= PHP_EOL . '*Admin Commands List*:' . PHP_EOL;
  60. foreach ($admin_commands as $admin_command) {
  61. $text .= '/' . $admin_command->getName() . ' - ' . $admin_command->getDescription() . PHP_EOL;
  62. }
  63. }
  64. $text .= PHP_EOL . 'For exact command help type: /help <command>';
  65. return $this->replyToChat($text, ['parse_mode' => 'markdown']);
  66. }
  67. $command_str = str_replace('/', '', $command_str);
  68. if (isset($all_commands[$command_str]) && ($safe_to_show || !$all_commands[$command_str]->isAdminCommand())) {
  69. $command = $all_commands[$command_str];
  70. return $this->replyToChat(sprintf(
  71. 'Command: %s (v%s)' . PHP_EOL .
  72. 'Description: %s' . PHP_EOL .
  73. 'Usage: %s',
  74. $command->getName(),
  75. $command->getVersion(),
  76. $command->getDescription(),
  77. $command->getUsage()
  78. ), ['parse_mode' => 'markdown']);
  79. }
  80. return $this->replyToChat('No help available: Command `/' . $command_str . '` not found', ['parse_mode' => 'markdown']);
  81. }
  82. /**
  83. * Get all available User and Admin commands to display in the help list.
  84. *
  85. * @return Command[][]
  86. * @throws TelegramException
  87. */
  88. protected function getUserAndAdminCommands(): array
  89. {
  90. /** @var Command[] $all_commands */
  91. $all_commands = $this->telegram->getCommandsList();
  92. // Only get enabled Admin and User commands that are allowed to be shown.
  93. $commands = array_filter($all_commands, function ($command): bool {
  94. return !$command->isSystemCommand() && $command->showInHelp() && $command->isEnabled();
  95. });
  96. // Filter out all User commands
  97. $user_commands = array_filter($commands, function ($command): bool {
  98. return $command->isUserCommand();
  99. });
  100. // Filter out all Admin commands
  101. $admin_commands = array_filter($commands, function ($command): bool {
  102. return $command->isAdminCommand();
  103. });
  104. ksort($commands);
  105. ksort($user_commands);
  106. ksort($admin_commands);
  107. return [$commands, $user_commands, $admin_commands];
  108. }
  109. }