InfoCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils\Telegram\Commands;
  4. use App\Models\Setting;
  5. use App\Models\User;
  6. use App\Utils\Telegram\Reply;
  7. use App\Utils\Telegram\TelegramTools;
  8. use Telegram\Bot\Actions;
  9. use Telegram\Bot\Commands\Command;
  10. use function in_array;
  11. use function json_decode;
  12. /**
  13. * Class InfoCommand.
  14. */
  15. final class InfoCommand extends Command
  16. {
  17. /**
  18. * @var string Command Name
  19. */
  20. protected string $name = 'info';
  21. /**
  22. * @var string Command Description
  23. */
  24. protected string $description = '[群组] 获取被回复消息的用户信息,管理员命令.';
  25. public function handle(): void
  26. {
  27. $Update = $this->getUpdate();
  28. $Message = $Update->getMessage();
  29. // 消息会话 ID
  30. $ChatID = $Message->getChat()->getId();
  31. // 消息 ID
  32. $MessageID = $Message->getMessageId();
  33. if ($ChatID < 0) {
  34. // 发送 '输入中' 会话状态
  35. $this->replyWithChatAction(['action' => Actions::TYPING]);
  36. // 触发用户
  37. $SendUser = [
  38. 'id' => $Message->getFrom()->getId(),
  39. ];
  40. if (! in_array($SendUser['id'], json_decode(Setting::obtain('telegram_admins')))) {
  41. $AdminUser = User::where('is_admin', 1)->where('telegram_id', $SendUser['id'])->first();
  42. if ($AdminUser === null) {
  43. // 非管理员回复消息
  44. if (Setting::obtain('enable_not_admin_reply') === true && Setting::obtain('not_admin_reply_msg') !== '') {
  45. $this->replyWithMessage(
  46. [
  47. 'text' => Setting::obtain('not_admin_reply_msg'),
  48. 'parse_mode' => 'HTML',
  49. 'reply_to_message_id' => $MessageID,
  50. ]
  51. );
  52. }
  53. return;
  54. }
  55. }
  56. if ($Message->getReplyToMessage() !== null) {
  57. // 回复源消息用户
  58. $FindUser = [
  59. 'id' => $Message->getReplyToMessage()->getFrom()->getId(),
  60. ];
  61. $User = TelegramTools::getUser($FindUser['id']);
  62. if ($User === null) {
  63. $this->replyWithMessage(
  64. [
  65. 'text' => Setting::obtain('no_user_found'),
  66. 'reply_to_message_id' => $MessageID,
  67. ]
  68. );
  69. } else {
  70. $this->replyWithMessage(
  71. [
  72. 'text' => Reply::getUserInfoFromAdmin($User, $ChatID),
  73. 'reply_to_message_id' => $MessageID,
  74. ]
  75. );
  76. }
  77. } else {
  78. $this->replyWithMessage(
  79. [
  80. 'text' => '请回复消息使用.',
  81. 'reply_to_message_id' => $MessageID,
  82. ]
  83. );
  84. }
  85. }
  86. }
  87. }