MyCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Telegram\Bot\Actions;
  8. use Telegram\Bot\Commands\Command;
  9. use function json_encode;
  10. /**
  11. * Class MyCommand.
  12. */
  13. final class MyCommand extends Command
  14. {
  15. /**
  16. * @var string Command Name
  17. */
  18. protected string $name = 'my';
  19. /**
  20. * @var string Command Description
  21. */
  22. protected string $description = '[群组/私聊] 我的个人信息.';
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function handle()
  27. {
  28. $Update = $this->getUpdate();
  29. $Message = $Update->getMessage();
  30. // 消息 ID
  31. $MessageID = $Message->getMessageId();
  32. // 消息会话 ID
  33. $ChatID = $Message->getChat()->getId();
  34. if ($ChatID < 0) {
  35. if (Setting::obtain('telegram_group_quiet') === true) {
  36. // 群组中不回应
  37. return null;
  38. }
  39. if ($ChatID !== $_ENV['telegram_chatid']) {
  40. // 非我方群组
  41. return null;
  42. }
  43. }
  44. // 发送 '输入中' 会话状态
  45. $this->replyWithChatAction(['action' => Actions::TYPING]);
  46. // 触发用户
  47. $SendUser = [
  48. 'id' => $Message->getFrom()->getId(),
  49. 'name' => $Message->getFrom()->getFirstName() . ' ' . $Message->getFrom()->getLastName(),
  50. 'username' => $Message->getFrom()->getUsername(),
  51. ];
  52. $User = User::where('telegram_id', $SendUser['id'])->first();
  53. if ($User === null) {
  54. // 回送信息
  55. $response = $this->replyWithMessage(
  56. [
  57. 'text' => Setting::obtain('user_not_bind_reply'),
  58. 'reply_to_message_id' => $MessageID,
  59. 'parse_mode' => 'Markdown',
  60. ]
  61. );
  62. } else {
  63. if ($ChatID > 0) {
  64. // 私人
  65. $response = $this->triggerCommand('menu');
  66. } else {
  67. // 群组
  68. $response = $this->group($User, $SendUser, $ChatID, $Message, $MessageID);
  69. }
  70. }
  71. return $response;
  72. }
  73. public function group($User, $SendUser, $ChatID, $Message, $MessageID)
  74. {
  75. $text = Reply::getUserTitle($User);
  76. $text .= PHP_EOL . PHP_EOL;
  77. $text .= Reply::getUserTrafficInfo($User);
  78. $text .= PHP_EOL;
  79. $text .= '流量重置时间:' . $User->validUseLoop();
  80. // 回送信息
  81. return $this->replyWithMessage(
  82. [
  83. 'text' => $text,
  84. 'parse_mode' => 'Markdown',
  85. 'reply_to_message_id' => $MessageID,
  86. 'reply_markup' => json_encode(
  87. [
  88. 'inline_keyboard' => [
  89. [
  90. [
  91. 'text' => (! $User->isAbleToCheckin() ? '已签到' : '签到'),
  92. 'callback_data' => 'user.checkin.' . $SendUser['id'],
  93. ],
  94. ],
  95. ],
  96. ]
  97. ),
  98. ]
  99. );
  100. }
  101. }