TelegramController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Ticket;
  4. use App\Models\User;
  5. use App\Models\UserOauth;
  6. use App\Services\TelegramService;
  7. use Exception;
  8. use Illuminate\Http\Request;
  9. use StdClass;
  10. class TelegramController extends Controller
  11. {
  12. protected $msg;
  13. public function __construct(Request $request)
  14. {
  15. if (hash_equals(sysConfig('telegram_token'), $request->input('access_token'))) {
  16. abort(500, 'authentication failed');
  17. }
  18. }
  19. public function webhook(Request $request)
  20. {
  21. $this->msg = $this->getMessage($request->input());
  22. if (! $this->msg) {
  23. return;
  24. }
  25. try {
  26. switch ($this->msg->message_type) {
  27. case 'send':
  28. $this->fromSend();
  29. break;
  30. case 'reply':
  31. $this->fromReply();
  32. break;
  33. }
  34. } catch (Exception $e) {
  35. $telegramService = new TelegramService();
  36. $telegramService->sendMessage($this->msg->chat_id, $e->getMessage());
  37. }
  38. }
  39. private function getMessage(array $data)
  40. {
  41. if (! isset($data['message'])) {
  42. return false;
  43. }
  44. $obj = new StdClass();
  45. $obj->is_private = $data['message']['chat']['type'] === 'private';
  46. if (! isset($data['message']['text'])) {
  47. return false;
  48. }
  49. $text = explode(' ', $data['message']['text']);
  50. $obj->command = $text[0];
  51. $obj->args = array_slice($text, 1);
  52. $obj->chat_id = $data['message']['chat']['id'];
  53. $obj->message_id = $data['message']['message_id'];
  54. $obj->message_type = ! isset($data['message']['reply_to_message']['text']) ? 'send' : 'reply';
  55. $obj->text = $data['message']['text'];
  56. if ($obj->message_type === 'reply') {
  57. $obj->reply_text = $data['message']['reply_to_message']['text'];
  58. }
  59. return $obj;
  60. }
  61. private function fromSend()
  62. {
  63. switch ($this->msg->command) {
  64. case '/bind':
  65. $this->bind();
  66. break;
  67. case '/traffic':
  68. $this->traffic();
  69. break;
  70. case '/getLatestUrl':
  71. $this->getLatestUrl();
  72. break;
  73. case '/unbind':
  74. $this->unbind();
  75. break;
  76. default:
  77. $this->help();
  78. }
  79. }
  80. private function bind()
  81. {
  82. $msg = $this->msg;
  83. if (! $msg->is_private) {
  84. return;
  85. }
  86. if (! isset($msg->args[0])) {
  87. abort(500, '参数有误,请携带邮箱地址发送');
  88. }
  89. $user = User::whereUsername($msg->args[0])->first();
  90. if (! $user) {
  91. abort(500, '用户不存在');
  92. }
  93. if ($user->telegram_user_id) {
  94. abort(500, '该账号已经绑定了Telegram账号');
  95. }
  96. if (! $user->userAuths()->create(['type' => 'telegram', 'identifier' => $msg->chat_id])) {
  97. abort(500, '设置失败');
  98. }
  99. $telegramService = new TelegramService();
  100. $telegramService->sendMessage($msg->chat_id, '绑定成功');
  101. }
  102. private function traffic()
  103. {
  104. $msg = $this->msg;
  105. if (! $msg->is_private) {
  106. return;
  107. }
  108. $telegramService = new TelegramService();
  109. if (! $oauth = UserOauth::query()->where([
  110. 'type' => 'telegram',
  111. 'identifier' => $msg->chat_id,
  112. ])->first()) {
  113. $this->help();
  114. $telegramService->sendMessage($msg->chat_id, '没有查询到您的用户信息,请先绑定账号', 'markdown');
  115. return;
  116. }
  117. $user = $oauth->user;
  118. $transferEnable = flowAutoShow($user->transfer_enable);
  119. $up = flowAutoShow($user->u);
  120. $down = flowAutoShow($user->d);
  121. $remaining = flowAutoShow($user->transfer_enable - ($user->u + $user->d));
  122. $text = "🚥流量查询\n———————————————\n计划流量:`{$transferEnable}`\n已用上行:`{$up}`\n已用下行:`{$down}`\n剩余流量:`{$remaining}`";
  123. $telegramService->sendMessage($msg->chat_id, $text, 'markdown');
  124. }
  125. private function help()
  126. {
  127. $msg = $this->msg;
  128. if (! $msg->is_private) {
  129. return;
  130. }
  131. $telegramService = new TelegramService();
  132. $commands = [
  133. '/bind 订阅地址 - 绑定你的'.sysConfig('website_name').'账号',
  134. '/traffic - 查询流量信息',
  135. '/getLatestUrl - 获取最新的'.sysConfig('website_name').'网址',
  136. '/unbind - 解除绑定',
  137. ];
  138. $text = implode(PHP_EOL, $commands);
  139. $telegramService->sendMessage($msg->chat_id, "你可以使用以下命令进行操作:\n\n$text", 'markdown');
  140. }
  141. private function getLatestUrl()
  142. {
  143. $msg = $this->msg;
  144. $telegramService = new TelegramService();
  145. $text = sprintf(
  146. '%s的最新网址是:%s',
  147. sysConfig('website_name'),
  148. sysConfig('website_url')
  149. );
  150. $telegramService->sendMessage($msg->chat_id, $text, 'markdown');
  151. }
  152. private function unbind()
  153. {
  154. $msg = $this->msg;
  155. if (! $msg->is_private) {
  156. return;
  157. }
  158. $user = User::with(['userAuths' => function ($query) use ($msg) {
  159. $query->whereType('telegram')->whereIdentifier($msg->chat_id);
  160. },
  161. ])->first();
  162. $telegramService = new TelegramService();
  163. if (! $user) {
  164. $this->help();
  165. $telegramService->sendMessage($msg->chat_id, '没有查询到您的用户信息,请先绑定账号', 'markdown');
  166. return;
  167. }
  168. if (! $user->userAuths()->whereType('telegram')->whereIdentifier($msg->chat_id)->delete()) {
  169. abort(500, '解绑失败');
  170. }
  171. $telegramService->sendMessage($msg->chat_id, '解绑成功', 'markdown');
  172. }
  173. private function fromReply()
  174. {
  175. // ticket
  176. if (preg_match('/[#](.*)/', $this->msg->reply_text, $match)) {
  177. $this->replayTicket($match[1]);
  178. }
  179. }
  180. private function replayTicket($ticketId)
  181. {
  182. $msg = $this->msg;
  183. if (! $msg->is_private) {
  184. return;
  185. }
  186. $user = User::with(['userAuths' => function ($query) use ($msg) {
  187. $query->whereType('telegram')->whereIdentifier($msg->chat_id);
  188. },
  189. ])->first();
  190. if (! $user) {
  191. abort(500, '用户不存在');
  192. }
  193. $admin = User::role('Super Admin')->whereId($user->id)->first();
  194. if ($admin) {
  195. $ticket = Ticket::whereId($ticketId)->first();
  196. if (! $ticket) {
  197. abort(500, '工单不存在');
  198. }
  199. if ($ticket->status) {
  200. abort(500, '工单已关闭,无法回复');
  201. }
  202. $ticket->reply()->create(['admin_id' => $admin->id, 'content' => $msg->text]);
  203. }
  204. $telegramService = new TelegramService();
  205. $telegramService->sendMessage($msg->chat_id, "#`{$ticketId}` 的工单已回复成功", 'markdown');
  206. }
  207. }