StartCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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;
  7. use App\Utils\Telegram\TelegramTools;
  8. use RedisException;
  9. use Telegram\Bot\Actions;
  10. use Telegram\Bot\Commands\Command;
  11. use function strlen;
  12. /**
  13. * Class StratCommand.
  14. */
  15. final class StartCommand extends Command
  16. {
  17. /**
  18. * @var string Command Name
  19. */
  20. protected string $name = 'start';
  21. /**
  22. * @var string Command Description
  23. */
  24. protected string $description = '[群组/私聊] Bot 初始命令.';
  25. public function handle(): void
  26. {
  27. $Update = $this->getUpdate();
  28. $Message = $Update->getMessage();
  29. // 消息会话 ID
  30. $ChatID = $Message->getChat()->getId();
  31. if ($ChatID > 0) {
  32. // 私人会话
  33. // 发送 '输入中' 会话状态
  34. $this->replyWithChatAction(['action' => Actions::TYPING]);
  35. // 触发用户
  36. $SendUser = [
  37. 'id' => $Message->getFrom()->getId(),
  38. 'name' => $Message->getFrom()->getFirstName() . ' ' . $Message->getFrom()->getLastName(),
  39. 'username' => $Message->getFrom()->getUsername(),
  40. ];
  41. // 消息内容
  42. $MessageText = explode(' ', trim($Message->getText()));
  43. $MessageKey = array_splice($MessageText, -1)[0];
  44. if (
  45. $MessageKey !== ''
  46. && TelegramTools::getUser($SendUser['id']) === null
  47. && strlen($MessageKey) === 16
  48. ) {
  49. // 新用户绑定
  50. $this->bindingAccount($SendUser, $MessageKey);
  51. }
  52. // 回送信息
  53. $this->replyWithMessage(
  54. [
  55. 'text' => '发送 /help 获取帮助',
  56. 'parse_mode' => 'Markdown',
  57. ]
  58. );
  59. } else {
  60. if (! Setting::obtain('telegram_group_quiet')) {
  61. // 发送 '输入中' 会话状态
  62. $this->replyWithChatAction(['action' => Actions::TYPING]);
  63. // 回送信息
  64. $this->replyWithMessage(
  65. [
  66. 'text' => '喵喵喵.',
  67. 'parse_mode' => 'Markdown',
  68. 'reply_to_message_id' => $Message->getMessageId(),
  69. ]
  70. );
  71. }
  72. }
  73. }
  74. /**
  75. * @throws RedisException
  76. */
  77. public function bindingAccount($SendUser, $MessageText): void
  78. {
  79. $Uid = Telegram::verifyBindSession($MessageText);
  80. if ($Uid === 0) {
  81. $text = '绑定失败了呢,经检查发现:【' . $MessageText . '】的有效期为 10 分钟,你可以在我们网站上的 **资料编辑** 页面刷新后重试.';
  82. } else {
  83. $BinsUser = User::where('id', $Uid)->first();
  84. $BinsUser->telegram_id = $SendUser['id'];
  85. $BinsUser->im_type = 4;
  86. if ($SendUser['username'] === null) {
  87. $BinsUser->im_value = '用戶名未设置';
  88. } else {
  89. $BinsUser->im_value = $SendUser['username'];
  90. }
  91. $BinsUser->save();
  92. if ($BinsUser->is_admin === 1) {
  93. $text = '尊敬的 **管理员** 你好,恭喜绑定成功。' . PHP_EOL . '当前绑定邮箱为: ' . $BinsUser->email;
  94. } else {
  95. if ($BinsUser->class >= 1) {
  96. $text = '尊敬的 **VIP ' . $BinsUser->class . '** 用户你好.' . PHP_EOL . '恭喜你绑定成功,当前绑定邮箱为: ' . $BinsUser->email;
  97. } else {
  98. $text = '绑定成功了,你的邮箱为:' . $BinsUser->email;
  99. }
  100. }
  101. }
  102. // 回送信息
  103. $this->replyWithMessage(
  104. [
  105. 'text' => $text,
  106. 'parse_mode' => 'Markdown',
  107. ]
  108. );
  109. }
  110. }