HelpCommand.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils\Telegram\Commands;
  4. use App\Models\Setting;
  5. use Telegram\Bot\Actions;
  6. use Telegram\Bot\Commands\Command;
  7. /**
  8. * Class HelpCommand.
  9. */
  10. final class HelpCommand extends Command
  11. {
  12. /**
  13. * @var string Command Name
  14. */
  15. protected $name = 'help';
  16. /**
  17. * @var string Command Description
  18. */
  19. protected $description = '[群组/私聊] 系统中可用的所有命令.';
  20. public function handle(): void
  21. {
  22. $Update = $this->getUpdate();
  23. $Message = $Update->getMessage();
  24. if ($Message->getChat()->getId() < 0) {
  25. if (Setting::obtain('telegram_group_quiet') === true) {
  26. return;
  27. }
  28. }
  29. if (! preg_match('/^\/help\s?(@' . $_ENV['telegram_bot'] . ')?.*/i', $Message->getText())) {
  30. if (Setting::obtain('help_any_command') === false) {
  31. return;
  32. }
  33. }
  34. $this->replyWithChatAction(['action' => Actions::TYPING]);
  35. $commands = $this->telegram->getCommands();
  36. $text = '系统中可用的所有命令.';
  37. $text .= PHP_EOL . PHP_EOL;
  38. foreach ($commands as $name => $handler) {
  39. $text .= '/' . $name . PHP_EOL . '` - ' . $handler->getDescription() . '`' . PHP_EOL;
  40. }
  41. $this->replyWithMessage(
  42. [
  43. 'text' => $text,
  44. 'parse_mode' => 'Markdown',
  45. 'disable_web_page_preview' => false,
  46. 'reply_to_message_id' => $Message->getMessageId(),
  47. 'reply_markup' => null,
  48. ]
  49. );
  50. }
  51. }