TelegramController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Http\Controllers\Guest;
  3. use App\Services\TelegramService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\User;
  7. use App\Utils\Helper;
  8. use App\Services\TicketService;
  9. class TelegramController extends Controller
  10. {
  11. protected $msg;
  12. public function __construct(Request $request)
  13. {
  14. if ($request->input('access_token') !== md5(config('v2board.telegram_bot_token'))) {
  15. abort(500, 'authentication failed');
  16. }
  17. }
  18. public function webhook(Request $request)
  19. {
  20. $this->msg = $this->getMessage($request->input());
  21. if (!$this->msg) return;
  22. try {
  23. switch($this->msg->message_type) {
  24. case 'send':
  25. $this->fromSend();
  26. break;
  27. case 'reply':
  28. $this->fromReply();
  29. break;
  30. }
  31. } catch (\Exception $e) {
  32. $telegramService = new TelegramService();
  33. $telegramService->sendMessage($this->msg->chat_id, $e->getMessage());
  34. }
  35. }
  36. private function fromSend()
  37. {
  38. switch($this->msg->command) {
  39. case '/bind': $this->bind();
  40. break;
  41. case '/traffic': $this->traffic();
  42. break;
  43. case '/getlatesturl': $this->getLatestUrl();
  44. break;
  45. default: $this->help();
  46. }
  47. }
  48. private function fromReply()
  49. {
  50. // ticket
  51. if (preg_match("/[#](.*)/", $this->msg->reply_text, $match)) {
  52. $this->replayTicket($match[1]);
  53. }
  54. }
  55. private function getMessage(array $data)
  56. {
  57. if (!isset($data['message'])) return false;
  58. $obj = new \StdClass();
  59. $obj->is_private = $data['message']['chat']['type'] === 'private' ? true : false;
  60. if (!isset($data['message']['text'])) return false;
  61. $text = explode(' ', $data['message']['text']);
  62. $obj->command = $text[0];
  63. $obj->args = array_slice($text, 1);
  64. $obj->chat_id = $data['message']['chat']['id'];
  65. $obj->message_id = $data['message']['message_id'];
  66. $obj->message_type = !isset($data['message']['reply_to_message']) ? 'send' : 'reply';
  67. $obj->text = $data['message']['text'];
  68. if ($obj->message_type === 'reply') {
  69. $obj->reply_text = $data['message']['reply_to_message']['text'];
  70. }
  71. return $obj;
  72. }
  73. private function bind()
  74. {
  75. $msg = $this->msg;
  76. if (!$msg->is_private) return;
  77. if (!isset($msg->args[0])) {
  78. abort(500, '参数有误,请携带订阅地址发送');
  79. }
  80. $subscribeUrl = $msg->args[0];
  81. $subscribeUrl = parse_url($subscribeUrl);
  82. parse_str($subscribeUrl['query'], $query);
  83. $token = $query['token'];
  84. if (!$token) {
  85. abort(500, '订阅地址无效');
  86. }
  87. $user = User::where('token', $token)->first();
  88. if (!$user) {
  89. abort(500, '用户不存在');
  90. }
  91. $user->telegram_id = $msg->chat_id;
  92. if (!$user->save()) {
  93. abort(500, '设置失败');
  94. }
  95. $telegramService = new TelegramService();
  96. $telegramService->sendMessage($msg->chat_id, '绑定成功');
  97. }
  98. private function help()
  99. {
  100. $msg = $this->msg;
  101. if (!$msg->is_private) return;
  102. $telegramService = new TelegramService();
  103. $commands = [
  104. '/bind 订阅地址 - 绑定你的' . config('v2board.app_name', 'V2Board') . '账号',
  105. '/traffic - 查询流量信息',
  106. '/getlatesturl - 获取最新的' . config('v2board.app_name', 'V2Board') . '网址'
  107. ];
  108. $text = implode(PHP_EOL, $commands);
  109. $telegramService->sendMessage($msg->chat_id, "你可以使用以下命令进行操作:\n\n$text", 'markdown');
  110. }
  111. private function traffic()
  112. {
  113. $msg = $this->msg;
  114. if (!$msg->is_private) return;
  115. $user = User::where('telegram_id', $msg->chat_id)->first();
  116. $telegramService = new TelegramService();
  117. if (!$user) {
  118. $this->help();
  119. $telegramService->sendMessage($msg->chat_id, '没有查询到您的用户信息,请先绑定账号', 'markdown');
  120. return;
  121. }
  122. $transferEnable = Helper::trafficConvert($user->transfer_enable);
  123. $up = Helper::trafficConvert($user->u);
  124. $down = Helper::trafficConvert($user->d);
  125. $remaining = Helper::trafficConvert($user->transfer_enable - ($user->u + $user->d));
  126. $text = "🚥流量查询\n———————————————\n计划流量:`{$transferEnable}`\n已用上行:`{$up}`\n已用下行:`{$down}`\n剩余流量:`{$remaining}`";
  127. $telegramService->sendMessage($msg->chat_id, $text, 'markdown');
  128. }
  129. private function getLatestUrl()
  130. {
  131. $msg = $this->msg;
  132. $user = User::where('telegram_id', $msg->chat_id)->first();
  133. $telegramService = new TelegramService();
  134. $text = sprintf(
  135. "%s的最新网址是:%s",
  136. config('v2board.app_name', 'V2Board'),
  137. config('v2board.app_url')
  138. );
  139. $telegramService->sendMessage($msg->chat_id, $text, 'markdown');
  140. }
  141. private function replayTicket($ticketId)
  142. {
  143. $msg = $this->msg;
  144. $user = User::where('telegram_id', $msg->chat_id)->first();
  145. if (!$user) {
  146. abort(500, '用户不存在');
  147. }
  148. $ticketService = new TicketService();
  149. if ($user->is_admin) {
  150. $ticketService->replyByAdmin(
  151. $ticketId,
  152. $msg->text,
  153. $user->id
  154. );
  155. }
  156. $telegramService = new TelegramService();
  157. $telegramService->sendMessage($msg->chat_id, "#`{$ticketId}` 的工单已回复成功", 'markdown');
  158. }
  159. }