CheckinCommand.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Services\Bot\Telegram\Commands;
  4. use App\Models\Config;
  5. use App\Services\Bot\Telegram\Message;
  6. use App\Services\Reward;
  7. use Telegram\Bot\Actions;
  8. use Telegram\Bot\Commands\Command;
  9. /**
  10. * Class CheckinCommand.
  11. */
  12. final class CheckinCommand extends Command
  13. {
  14. /**
  15. * @var string Command Name
  16. */
  17. protected string $name = 'checkin';
  18. /**
  19. * @var string Command Description
  20. */
  21. protected string $description = '[群组/私聊] 每日签到.';
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function handle()
  26. {
  27. $update = $this->getUpdate();
  28. $message = $update->getMessage();
  29. // 消息会话 ID
  30. $chat_id = $message->getChat()->getId();
  31. if ($chat_id < 0) {
  32. if (Config::obtain('telegram_group_quiet')) {
  33. // 群组中不回应
  34. return null;
  35. }
  36. if ($chat_id !== Config::obtain('telegram_chatid')) {
  37. // 非我方群组
  38. return null;
  39. }
  40. }
  41. // 发送 '输入中' 会话状态
  42. $this->replyWithChatAction(['action' => Actions::TYPING]);
  43. // 触发用户
  44. $send_user = [
  45. 'id' => $message->getFrom()->getId(),
  46. ];
  47. $user = Message::getUser($send_user['id']);
  48. if ($user === null) {
  49. // 回送信息
  50. $response = $this->replyWithMessage(
  51. [
  52. 'text' => Config::obtain('user_not_bind_reply'),
  53. 'parse_mode' => 'Markdown',
  54. 'reply_to_message_id' => $message->getMessageId(),
  55. ]
  56. );
  57. } else {
  58. if ($user->isAbleToCheckin()) {
  59. $traffic = Reward::issueCheckinReward($this->user->id);
  60. if (! $traffic) {
  61. $msg = '签到失败';
  62. } else {
  63. $msg = '获得了 ' . $traffic . 'MB 流量.';
  64. }
  65. } else {
  66. $msg = '你今天已经签到过了';
  67. }
  68. // 回送信息
  69. $response = $this->replyWithMessage(
  70. [
  71. 'text' => $msg,
  72. 'parse_mode' => 'Markdown',
  73. 'reply_to_message_id' => $message->getMessageId(),
  74. ]
  75. );
  76. }
  77. return $response;
  78. }
  79. }