TelegramController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 webhook(Request $request): void
  14. {
  15. $this->msg = $this->getMessage($request->input());
  16. if (! $this->msg) {
  17. return;
  18. }
  19. try {
  20. switch ($this->msg->message_type) {
  21. case 'send':
  22. $this->fromSend();
  23. break;
  24. case 'reply':
  25. $this->fromReply();
  26. break;
  27. }
  28. } catch (Exception $e) {
  29. $telegramService = new TelegramService;
  30. $telegramService->sendMessage($this->msg->chat_id, $e->getMessage());
  31. }
  32. }
  33. private function getMessage(array $data)
  34. {
  35. if (! isset($data['message'])) {
  36. return false;
  37. }
  38. $obj = new StdClass;
  39. $obj->is_private = $data['message']['chat']['type'] === 'private';
  40. if (! isset($data['message']['text'])) {
  41. return false;
  42. }
  43. $text = explode(' ', $data['message']['text']);
  44. $obj->command = $text[0];
  45. $obj->args = array_slice($text, 1);
  46. $obj->chat_id = $data['message']['chat']['id'];
  47. $obj->message_id = $data['message']['message_id'];
  48. $obj->message_type = ! isset($data['message']['reply_to_message']['text']) ? 'send' : 'reply';
  49. $obj->text = $data['message']['text'];
  50. if ($obj->message_type === 'reply') {
  51. $obj->reply_text = $data['message']['reply_to_message']['text'];
  52. }
  53. return $obj;
  54. }
  55. private function fromSend(): void
  56. {
  57. $commands = [
  58. '/bind' => 'bind',
  59. '/traffic' => 'traffic',
  60. '/url' => 'getUrl',
  61. '/unbind' => 'unbind',
  62. ];
  63. $command = $this->msg->command;
  64. if (isset($commands[$command])) {
  65. $this->{$commands[$command]}();
  66. } else {
  67. $this->help();
  68. }
  69. }
  70. private function help(): void
  71. {
  72. $msg = $this->msg;
  73. if (! $msg->is_private) {
  74. return;
  75. }
  76. $webName = sysConfig('website_name');
  77. $accountType = sysConfig('username_type') === 'email' || sysConfig('username_type') === null ? ucfirst(trans('validation.attributes.email')) : trans('model.user.username');
  78. $telegramService = new TelegramService;
  79. $commands = trans('user.telegram.command.intro').": \n\n/bind `$accountType` -[".trans('user.telegram.command.bind', ['web_name' => $webName])."]\n/traffic -[".trans('user.telegram.command.traffic')."]\n/url -[".trans('user.telegram.command.web_url', ['web_name' => $webName])."]\n/unbind -[".trans('user.telegram.command.unbind').']';
  80. $telegramService->sendMessage($msg->chat_id, $commands, 'markdown');
  81. }
  82. private function fromReply(): void
  83. {
  84. // ticket
  85. if (preg_match('/[#](.*)/', $this->msg->reply_text, $match)) {
  86. $this->replayTicket($match[1]);
  87. }
  88. }
  89. private function replayTicket($ticketId): void
  90. {
  91. $msg = $this->msg;
  92. if (! $msg->is_private) {
  93. return;
  94. }
  95. $user = User::with([
  96. 'userAuths' => function ($query) use ($msg) {
  97. $query->whereType('telegram')->whereIdentifier($msg->chat_id);
  98. },
  99. ])->first();
  100. if (! $user) {
  101. abort(500, trans('user.telegram.params_missing'));
  102. }
  103. $admin = User::role('Super Admin')->whereId($user->id)->first();
  104. if ($admin) {
  105. $ticket = Ticket::whereId($ticketId)->first();
  106. if (! $ticket) {
  107. abort(500, trans('user.telegram.ticket_missing'));
  108. }
  109. $ticket->reply()->create(['admin_id' => $admin->id, 'content' => $msg->text]);
  110. if ($ticket->status !== 1) {
  111. $ticket->update(['status' => 1]);
  112. }
  113. }
  114. (new TelegramService)->sendMessage($msg->chat_id, trans('user.telegram.ticket_reply', ['id' => $ticketId]), 'markdown');
  115. }
  116. private function bind(): void
  117. {
  118. $msg = $this->msg;
  119. if (! $msg->is_private) {
  120. return;
  121. }
  122. if (! isset($msg->args[0])) {
  123. abort(500, trans('user.telegram.params_missing'));
  124. }
  125. $user = User::whereUsername($msg->args[0])->first();
  126. if (! $user) {
  127. abort(500, trans('user.telegram.user_missing'));
  128. }
  129. if ($user->telegram_user_id) {
  130. abort(500, trans('user.telegram.bind_exists'));
  131. }
  132. if (! $user->userAuths()->create(['type' => 'telegram', 'identifier' => $msg->chat_id])) {
  133. abort(500, trans('common.failed_item', ['attribute' => trans('user.oauth.bind')]));
  134. }
  135. $telegramService = new TelegramService;
  136. $telegramService->sendMessage($msg->chat_id, trans('common.success_item', ['attribute' => trans('user.oauth.bind')]));
  137. }
  138. private function traffic(): void
  139. {
  140. $msg = $this->msg;
  141. if (! $msg->is_private) {
  142. return;
  143. }
  144. $telegramService = new TelegramService;
  145. if (! $oauth = UserOauth::query()->where(['type' => 'telegram', 'identifier' => $msg->chat_id])->first()) {
  146. $this->help();
  147. $telegramService->sendMessage($msg->chat_id, trans('user.telegram.bind_missing'), 'markdown');
  148. return;
  149. }
  150. $user = $oauth->user;
  151. $transferEnable = formatBytes($user->transfer_enable);
  152. $up = formatBytes($user->u);
  153. $down = formatBytes($user->d);
  154. $remaining = formatBytes($user->transfer_enable - ($user->u + $user->d));
  155. $text = '🚥'.trans('user.subscribe.info.title')."\n———————————————\n".trans('user.subscribe.info.total').": `$transferEnable`\n".trans('user.subscribe.info.upload').": `$up`\n".trans('user.subscribe.info.download').": `$down`\n".trans('user.account.remain').": `$remaining`";
  156. $telegramService->sendMessage($msg->chat_id, $text, 'markdown');
  157. }
  158. private function getUrl(): void
  159. {
  160. $msg = $this->msg;
  161. $telegramService = new TelegramService;
  162. $telegramService->sendMessage($msg->chat_id, trans('user.telegram.get_url', ['get_url' => sysConfig('website_name')]).': '.sysConfig('website_url'), 'markdown');
  163. }
  164. private function unbind(): void
  165. {
  166. $msg = $this->msg;
  167. if (! $msg->is_private) {
  168. return;
  169. }
  170. $user = User::with([
  171. 'userAuths' => function ($query) use ($msg) {
  172. $query->whereType('telegram')->whereIdentifier($msg->chat_id);
  173. },
  174. ])->first();
  175. $telegramService = new TelegramService;
  176. if (! $user) {
  177. $this->help();
  178. $telegramService->sendMessage($msg->chat_id, trans('user.telegram.bind_missing'), 'markdown');
  179. return;
  180. }
  181. if (! $user->userAuths()->whereType('telegram')->whereIdentifier($msg->chat_id)->delete()) {
  182. abort(500, trans('common.failed_item', ['attribute' => trans('user.oauth.unbind')]));
  183. }
  184. $telegramService->sendMessage($msg->chat_id, trans('common.success_item', ['attribute' => trans('user.oauth.unbind')]), 'markdown');
  185. }
  186. }