TelegramWebHook.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Constants\StepStatus;
  4. use App\Constants\Util;
  5. use App\Exceptions\MessageException;
  6. use App\Http\Controllers\Controller;
  7. use App\Models\Message;
  8. use App\Models\User;
  9. use App\Services\OnLineService;
  10. use App\Services\RecordService;
  11. use App\Services\RoomService;
  12. use App\Services\SettlementService;
  13. use App\Services\TopUpService;
  14. use App\Services\TutorialService;
  15. use App\Services\UserService;
  16. use App\Services\WalletService;
  17. use App\Services\WithdrawService;
  18. use App\Services\BalanceLogService;
  19. use Illuminate\Http\Request;
  20. use Illuminate\Support\Facades\Cache;
  21. use Illuminate\Support\Facades\DB;
  22. use Telegram\Bot\Api;
  23. use Telegram\Bot\Exceptions\TelegramSDKException;
  24. use Illuminate\Support\Facades\Log;
  25. use App\Models\Config;
  26. use App\Services\GameplayRuleService;
  27. use App\Services\BetService;
  28. class TelegramWebHook extends Controller
  29. {
  30. protected $telegram;
  31. public function __construct(Api $telegram)
  32. {
  33. $this->telegram = $telegram;
  34. }
  35. public function handle(Request $request)
  36. {
  37. Log::error('Telegram 日志写入测试: ' . json_encode([$request->ip()], JSON_UNESCAPED_UNICODE));
  38. // try {
  39. $m = new Message();
  40. $m->json = $request->ip();
  41. $m->save();
  42. $telegram = new Api(config('services.telegram.token'));
  43. // } catch (TelegramSDKException $e) {
  44. // return response()->json(['status' => 'ok']);
  45. // }
  46. $update = $telegram->getWebhookUpdate(); // 获取更新数据
  47. $update->callbackQuery;
  48. if ($update->has('callback_query')) {
  49. $json['type'] = 'callback_query';
  50. $callbackQuery = $update->callbackQuery;
  51. $json['update'] = $callbackQuery;
  52. $message = $callbackQuery->message;
  53. $from = $callbackQuery->from;
  54. $data = $callbackQuery->data; // 获取 callback_data
  55. $m = new Message();
  56. $m->json = $data;
  57. $m->save();
  58. Util::delCache($message->chat->id);
  59. Log::error('Telegram 回调数据(JSON): ' . json_encode($update, JSON_UNESCAPED_UNICODE));
  60. }else {
  61. $update = $request->all();
  62. if (isset($update['message'])) {
  63. $message = $update['message'];
  64. $chatId = $message['chat']['id'];
  65. $messageId = $message['message_id'];
  66. DB::beginTransaction();
  67. try {
  68. $m = new Message();
  69. $m->json = json_encode($update);
  70. $m->save();
  71. $this->processChatMessage($chatId, $messageId, $message, $message['from']);
  72. }
  73. catch (MessageException $e) {
  74. DB::rollBack();
  75. $msg = $e->getMessage();
  76. $msg = json_decode($msg, true);
  77. $telegram->sendMessage($msg);
  78. } //
  79. catch (TelegramSDKException $e) {
  80. DB::rollBack();
  81. $m = new Message();
  82. $m->json = $e->getMessage();
  83. $m->save();
  84. $telegram->sendMessage([
  85. 'chat_id' => $chatId,
  86. 'text' => '‼️‼️系统发生了错误,请联系客服'
  87. ]);
  88. }//
  89. catch (\Exception $e) {
  90. DB::rollBack();
  91. $m = new Message();
  92. $m->json = json_encode([
  93. 'line' => $e->getLine(),
  94. 'message' => $e->getMessage()
  95. ]);
  96. $m->save();
  97. Log::error('Telegram 处理消息异常: ' . $e->getMessage());
  98. $telegram->sendMessage([
  99. 'chat_id' => $chatId,
  100. 'text' => '‼️‼️系统发生了错误,请联系客服'
  101. ]);
  102. }
  103. }
  104. }
  105. return response()->json(['status' => 'ok']);
  106. }
  107. /**
  108. * @description: 处理聊天消息
  109. * @param {*} $chatId
  110. * @param {*} $messageId
  111. * @param {*} $message
  112. * @param {*} $from
  113. * @return {*}
  114. */
  115. public function processChatMessage($chatId, $messageId, $message, $from)
  116. {
  117. //用户发送图片,结算截图
  118. if (isset($message['photo'])) {
  119. $stepStatus = Cache::get(get_step_key($chatId), -1);
  120. $stepStatus = intval($stepStatus);
  121. // //结算截图
  122. // if ($stepStatus === StepStatus::INPUT_IMAGE) {
  123. // $photo = $message['photo'][count($message['photo']) - 1];
  124. // $res = (new SettlementService())->photo($photo, $chatId);
  125. // if ($res) $telegram->sendMessage($res);
  126. // }//
  127. // //充值截图
  128. // else if ($stepStatus === StepStatus::INPUT_TOP_UP_IMAGE) {
  129. // $photo = $message['photo'][count($message['photo']) - 1];
  130. // $res = TopUpService::photo($chatId, $photo);
  131. // if (isset($res['message_id'])) $telegram->editMessageText($res);
  132. // else $telegram->sendMessage($res);
  133. // }
  134. } //用户发送了消息
  135. else if (isset($message['text'])) {
  136. $text = $message['text'];
  137. if ($message['chat']['type'] === 'private') {
  138. switch ($text) {
  139. case "/start":
  140. Util::delCache($chatId);
  141. $user = User::where('member_id', $chatId)->first();
  142. if (!$user) {
  143. $user = new User();
  144. $user->member_id = $chatId;
  145. }
  146. $user->first_name = $message['chat']['first_name'];
  147. $user->save();
  148. //给每个用户生成一个专属的USDT钱包
  149. WalletService::getUserWallet($chatId);
  150. $username = config('services.telegram.username');
  151. $this->setReplyKeyboard($chatId);
  152. break;
  153. case "近期注单":
  154. Util::delCache($chatId);
  155. $this->telegram->sendMessage([
  156. 'chat_id' => $chatId,
  157. 'text' => $text
  158. ]);
  159. break;
  160. case "今日流水":
  161. Util::delCache($chatId);
  162. $result = BalanceLogService::getTodayFlowing($chatId);
  163. $this->telegram->sendMessage($result);
  164. break;
  165. case "联系客服":
  166. Util::delCache($chatId);
  167. $receivingAddress = Config::where('field', 'receiving_address')->first()->val;
  168. $serviceAccount = Config::where('field', 'service_account')->first()->val;
  169. $text = "官方唯一上分地址:\n";
  170. $text .= "USDT: \n";
  171. $text .= "{$receivingAddress}\n\n\n";
  172. $text .= "‼️特别注意‼️特别注意‼️\n";
  173. $text .= "‼️近期骗子肆意妄为,猖狂狡诈。经常冒充我方财务,管理人员以各种手段行使诈骗之术。多数为要求转账/提供验证码等。任何主动私聊您的都是非奸即盗。望各位老板谨记,预防受骗!\n";
  174. $text .= "‼️本群管理以及财务绝不会主动私聊客户。请各位老板务必认准群内带标识管理账号,转账前务必核对是否官方账号以及地址。任何一切不是官方行为的受骗上当,本群概不负责,谢谢!\n";
  175. $text .= "‼️为了您的资金安全,下分更换地址请提前告知财务,并使用原地址进行转账或录屏进行身份核实,核实为本人操作后才可进行更换地址下分\n";
  176. $text .= "‼️如查分金额与实际转账金额不同,请重新喊查。玩家重复查3次以上错误金额则视为恶意捣乱,将不退还转账金额!\n";
  177. $text .= "‼️如因为人为失误问题,到账金额与玩家实际转账金额不符,到账后的一切下注视为无效。\n";
  178. $text .= "‼️本群不接待以任何形式刷水的水军,一经发现玩家存在刷水行为将扣除历史所有返水、彩金。并冻结分数一个月,每天仅可下分账户余额10%。不纠结,不解释,请各大水军自行绕道。\n";
  179. $keyboard = [];
  180. $keyboard[] = [['text' => '✅ 唯一客服', 'callback_data' => "", 'url' => "https://t.me/{$serviceAccount}"]];
  181. $this->telegram->sendMessage([
  182. 'chat_id' => $chatId,
  183. 'text' => $text,
  184. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  185. ]);
  186. break;
  187. case "开奖历史":
  188. Util::delCache($chatId);
  189. $this->telegram->sendMessage([
  190. 'chat_id' => $chatId,
  191. 'text' => $text
  192. ]);
  193. break;
  194. case "当期下注":
  195. Util::delCache($chatId);
  196. $this->telegram->sendMessage([
  197. 'chat_id' => $chatId,
  198. 'text' => $text
  199. ]);
  200. break;
  201. case "查看余额":
  202. Util::delCache($chatId);
  203. $result = WalletService::getBalance($chatId);
  204. $this->telegram->sendMessage($result);
  205. break;
  206. case "投注大群":
  207. Util::delCache($chatId);
  208. $bettingGroup = Config::where('field', 'betting_group')->first()->val;
  209. $text = "👇👇👇点击下方按钮进群投注👇👇👇\n";
  210. $keyboard = [];
  211. $keyboard[] = [['text' => '✅ 投注群', 'callback_data' => "", 'url' => "https://t.me/{$bettingGroup}"]];
  212. $this->telegram->sendMessage([
  213. 'chat_id' => $chatId,
  214. 'text' => $text,
  215. 'reply_markup' => json_encode(['inline_keyboard' => $keyboard])
  216. ]);
  217. break;
  218. default:
  219. $returnMsg = BetService::bet($chatId,$text);
  220. $this->telegram->sendMessage($returnMsg);
  221. }
  222. }
  223. }
  224. }
  225. /**
  226. * @description: 设置 start 回复菜单
  227. * @param {*} $chatId
  228. * @return {*}
  229. */
  230. public function setReplyKeyboard($chatId)
  231. {
  232. $telegram = new Api(config('services.telegram.token'));
  233. $keyboard = [
  234. ['近期注单', '今日流水', '联系客服'], // 第一排按钮
  235. ['开奖历史', '当期下注', '查看余额'], // 第二排按钮
  236. ['投注大群']
  237. ];
  238. $replyMarkup = [
  239. 'keyboard' => $keyboard,
  240. 'resize_keyboard' => true, // 自适应大小
  241. 'one_time_keyboard' => false, // 保持显示,不会点击后收起
  242. ];
  243. $telegram->sendMessage([
  244. 'chat_id' => $chatId,
  245. 'text' => '你好,请选择功能菜单',
  246. 'reply_markup' => json_encode($replyMarkup),
  247. ]);
  248. }
  249. public function handleMessage(Request $request)
  250. {
  251. $telegram = new Api(config('services.telegram.token'));
  252. $update = $telegram->getWebhookUpdate(); // 获取更新数据
  253. $update->callbackQuery;
  254. if ($update->has('callback_query')) {
  255. $json['type'] = 'callback_query';
  256. $callbackQuery = $update->callbackQuery;
  257. $json['update'] = $callbackQuery;
  258. $message = $callbackQuery->message;
  259. $from = $callbackQuery->from;
  260. $data = $callbackQuery->data; // 获取 callback_data
  261. $m = new Message();
  262. $m->json = $data;
  263. $m->save();
  264. Util::delCache($message->chat->id);
  265. DB::beginTransaction();
  266. try {
  267. $chatId = $message->chat->id;
  268. $firstName = $message->chat->firstName;
  269. $messageId = $message->messageId;
  270. if (!$from->isBot) {
  271. $chatId = $from->id;
  272. $firstName = $from->firstName;
  273. }
  274. $user = User::where('member_id', $chatId)->first();
  275. if (!$user) {
  276. $user = new User();
  277. $user->member_id = $chatId;
  278. }
  279. $user->first_name = $firstName;
  280. $user->save();
  281. //给每个用户生成一个专属的USDT钱包
  282. WalletService::getUserWallet($chatId);
  283. //取消创建游戏
  284. if ($data === "games@@cancel") {
  285. $roomService = new RoomService($telegram);
  286. $res = $roomService->cancel($chatId, $messageId);
  287. $telegram->editMessageText($res);
  288. }
  289. if ($data === 'withdrawAddress@@done') {
  290. $res = WithdrawService::done($chatId, $messageId, $firstName);
  291. $telegram->editMessageText($res);
  292. }
  293. //选择提现地址
  294. $pattern = "/^withdrawAddress@@choose\d+$/";
  295. if (preg_match($pattern, $data)) {
  296. $id = preg_replace('/^withdrawAddress@@choose/', '', $data);
  297. $res = WithdrawService::chooseAddress($chatId, $firstName, $messageId, $id);
  298. $telegram->editMessageText($res);
  299. }
  300. //删除地址
  301. $pattern = "/^withdrawAddress@@del\d+$/";
  302. if (preg_match($pattern, $data)) {
  303. $id = preg_replace('/^withdrawAddress@@del/', '', $data);
  304. $res = WithdrawService::delAddress($chatId, $id, $messageId);
  305. $telegram->editMessageText($res);
  306. }
  307. //地址详情
  308. $pattern = "/^withdrawAddress@@detail\d+$/";
  309. if (preg_match($pattern, $data)) {
  310. $id = preg_replace('/^withdrawAddress@@detail/', '', $data);
  311. $res = WithdrawService::addressDetails($chatId, $messageId, $id);
  312. $telegram->editMessageText($res);
  313. }
  314. //教程帮助
  315. $pattern = "/^Tutorial@@\d+$/";
  316. if (preg_match($pattern, $data)) {
  317. $index = preg_replace('/^Tutorial@@/', '', $data);
  318. $res = TutorialService::help($chatId, $index, $messageId);
  319. $telegram->editMessageText($res);
  320. }
  321. if ($data === 'Tutorial@@home') {
  322. $res = (new TutorialService())->index($chatId);
  323. $res['message_id'] = $messageId;
  324. $telegram->editMessageText($res);
  325. }
  326. //游戏房间首页
  327. $pattern = "/^games@@home\d+$/";
  328. if (preg_match($pattern, $data)) {
  329. $roomId = preg_replace('/^games@@home/', '', $data);
  330. $res = (new RoomService($telegram))->gameHome($roomId, $chatId, $messageId);
  331. $m = new Message();
  332. $m->json = json_encode($res);
  333. $m->save();
  334. $telegram->editMessageText($res);
  335. }
  336. //选择游戏
  337. $pattern = "/^inputGameName@@.+$/";
  338. if (preg_match($pattern, $data)) {
  339. $gameName = preg_replace('/^inputGameName@@/', '', $data);
  340. $roomService = new RoomService($telegram);
  341. $res = $roomService->setGameName($chatId, $gameName, $messageId);
  342. if (isset($res['message_id'])) $telegram->editMessageText($res);
  343. else $telegram->sendMessage($res);
  344. }
  345. //选择中途加入
  346. $pattern = "/^setGameMidway@@.+$/";
  347. if (preg_match($pattern, $data)) {
  348. $midway = preg_replace('/^setGameMidway@@/', '', $data);
  349. $roomService = new RoomService($telegram);
  350. $res = $roomService->setGameMidway($chatId, $midway, $messageId);
  351. if (isset($res['message_id'])) $telegram->editMessageText($res);
  352. else $telegram->sendMessage($res);
  353. }
  354. //确认游戏ID
  355. $pattern = "/^inputGameID@@\d+$/";
  356. if (preg_match($pattern, $data)) {
  357. $gameId = preg_replace('/^inputGameID@@/', '', $data);
  358. $rs = new RoomService($telegram);
  359. $res = $rs->setGameID($chatId, $gameId, $messageId);
  360. foreach ($res as $r) {
  361. if (isset($r['message_id'])) {
  362. $telegram->editMessageText($r);
  363. } else {
  364. $telegram->sendMessage($r);
  365. }
  366. }
  367. }
  368. //房间低分
  369. $pattern = '/^roomMin@@.*/';
  370. if (preg_match($pattern, $data)) {
  371. $roomService = new RoomService($telegram);
  372. $res = $roomService->setMin($data, $chatId, $messageId);
  373. if (isset($res['message_id'])) {
  374. $telegram->editMessageText($res);
  375. } else {
  376. $telegram->sendMessage($res);
  377. }
  378. }
  379. //删除本条数据
  380. if ($data === 'del@@message') {
  381. $telegram->deleteMessage([
  382. 'chat_id' => $chatId,
  383. 'message_id' => $messageId,
  384. ]);
  385. }
  386. //确认创建房间
  387. if ($data === 'room@@done') {
  388. $roomService = new RoomService($telegram);
  389. $res = $roomService->done($chatId, $messageId);
  390. foreach ($res as $r) {
  391. if (isset($r['message_id'])) {
  392. $telegram->editMessageText($r);
  393. } else {
  394. $telegram->sendMessage($r);
  395. }
  396. }
  397. }
  398. //客服帮助
  399. if ($data === "/tutorial") {
  400. $res = (new TutorialService())->index($chatId);
  401. $telegram->sendMessage($res);
  402. }
  403. //点击提现按钮
  404. if ($data === "withdraw@@apply") {
  405. $res = (new WithdrawService())->apply($chatId, $messageId);
  406. $telegram->editMessageText($res);
  407. }
  408. //地址管理
  409. if ($data === 'withdraw@@address') {
  410. $res = WithdrawService::getAddress($chatId, $messageId);
  411. $telegram->editMessageText($res);
  412. }
  413. //点击充值按钮
  414. if ($data === 'topup@@topup') {
  415. $telegram->deleteMessage([
  416. 'chat_id' => $chatId,
  417. 'message_id' => $messageId,
  418. ]);
  419. $topService = new TopUpService();
  420. $res = $topService->scan($chatId, $messageId);
  421. $telegram->sendPhoto($res);
  422. }
  423. //点击充值的账单按钮
  424. if ($data === 'topup@@bill') {
  425. $res = (new TopUpService())->bill($chatId, $firstName, $messageId);
  426. $telegram->editMessageText($res);
  427. // $text = "📅 请输入查询日期\n";
  428. // $date = date('Y-m-d');
  429. // $text .= "例如您要查询的日期 {$date}\n";
  430. // $text .= "那么请发送:【充值账单】{$date}\n";
  431. // $telegram->sendMessage([
  432. // 'chat_id' => $chatId,
  433. // 'text' => $text
  434. // ]);
  435. }
  436. //关闭本条消息
  437. if ($data === 'message@@close') {
  438. $telegram->deleteMessage([
  439. 'chat_id' => $chatId,
  440. 'message_id' => $messageId
  441. ]);
  442. }
  443. if ($data === 'withdrawAddress@@add') {
  444. $res = WithdrawService::addAddress($chatId, $messageId);
  445. $telegram->editMessageText($res);
  446. }
  447. //提现管理
  448. if ($data === "withdraw@@home") {
  449. $res = WithdrawService::index($chatId, $firstName, $messageId);
  450. $telegram->editMessageText($res);
  451. }
  452. //点击提现的账单按钮
  453. if ($data === "withdraw@@bill") {
  454. $res = (new WithdrawService())->bill($chatId, $firstName, $messageId);
  455. $telegram->editMessageText($res);
  456. // $telegram->sendMessage($res);
  457. // $text = "📅 请输入查询日期\n";
  458. // $date = date('Y-m-d');
  459. // $text .= "例如您要查询的日期 {$date}\n";
  460. // $text .= "那么请发送:【提现账单】{$date}\n";
  461. // $telegram->sendMessage([
  462. // 'chat_id' => $chatId,
  463. // 'text' => $text
  464. // ]);
  465. }
  466. //充值首页
  467. if ($data === "topUp@@home" || $data === "topUp@@home1") {
  468. $res = (new TopUpService())->index($chatId, $firstName, $messageId);
  469. if ($data === "topUp@@home1") {
  470. $telegram->deleteMessage([
  471. 'chat_id' => $chatId,
  472. 'message_id' => $messageId
  473. ]);
  474. $telegram->sendMessage($res);
  475. } else {
  476. $telegram->editMessageText($res);
  477. }
  478. }
  479. //点击我已付款按钮
  480. //手动充值(后台审核后到账)
  481. if ($data === 'topUp@@pay2') {
  482. $telegram->deleteMessage([
  483. 'chat_id' => $chatId,
  484. 'message_id' => $messageId
  485. ]);
  486. $res = TopUpService::pay2($chatId);
  487. $telegram->sendMessage($res);
  488. } //
  489. //自动充值
  490. elseif ($data === 'topUp@@pay') {
  491. $telegram->deleteMessage([
  492. 'chat_id' => $chatId,
  493. 'message_id' => $messageId
  494. ]);
  495. $topService = new TopUpService();
  496. $res = $topService->done($chatId);
  497. $telegram->sendMessage($res);
  498. }
  499. //充值账单,下一页
  500. $pattern = "/^topUpBillNextPage@@\d+$/";
  501. if (preg_match($pattern, $data)) {
  502. $page = preg_replace('/^topUpBillNextPage@@/', '', $data);
  503. $page = intval($page);
  504. $res = (new TopUpService())->bill($chatId, $firstName, $messageId, $page);
  505. $telegram->editMessageText($res);
  506. }
  507. //提现账单,下一页
  508. $pattern = "/^withdrawBillNextPage@@\d+$/";
  509. if (preg_match($pattern, $data)) {
  510. $page = preg_replace('/^withdrawBillNextPage@@/', '', $data);
  511. $page = intval($page);
  512. $res = (new WithdrawService())->bill($chatId, $firstName, $messageId, $page);
  513. $telegram->editMessageText($res);
  514. }
  515. //提前开始游戏
  516. $pattern = "/^games@@start\d+$/";
  517. if (preg_match($pattern, $data)) {
  518. $roomId = preg_replace('/^games@@start/', '', $data);
  519. $roomService = new RoomService($telegram);
  520. $res = $roomService->start($chatId, $roomId, $messageId);
  521. foreach ($res as $r) {
  522. if (isset($r['message_id'])) {
  523. $telegram->editMessageText($r);
  524. } else {
  525. $telegram->sendMessage($r);
  526. }
  527. }
  528. }
  529. //解散房间
  530. $pattern = "/^games@@Disband\d+$/";
  531. if (preg_match($pattern, $data)) {
  532. $roomService = new RoomService($telegram);
  533. $roomId = preg_replace('/^games@@Disband/', '', $data);
  534. $res = $roomService->disband($roomId, $chatId, $messageId);
  535. foreach ($res as $r) {
  536. if (isset($r['message_id'])) {
  537. $telegram->editMessageText($r);
  538. } else {
  539. $telegram->sendMessage($r);
  540. }
  541. }
  542. }
  543. //退出房间
  544. $pattern = "/^games@@exit\d+$/";
  545. if (preg_match($pattern, $data)) {
  546. $roomService = new RoomService($telegram);
  547. $res = $roomService->exit($data, $chatId, $messageId);
  548. if (isset($res['message_id'])) $telegram->editMessageText($res);
  549. else $telegram->sendMessage($res);
  550. }
  551. //人员/状态
  552. $pattern = "/^games@@status\d+$/";
  553. if (preg_match($pattern, $data)) {
  554. $roomId = preg_replace('/^games@@status/', '', $data);
  555. $roomService = new RoomService($telegram);
  556. $res = $roomService->userStatus($roomId, $chatId, $messageId);
  557. if (isset($res['message_id'])) $telegram->editMessageText($res);
  558. else $telegram->sendMessage($res);
  559. }
  560. //准备
  561. $pattern = "/^games@@ready\d+$/";
  562. if (preg_match($pattern, $data)) {
  563. $roomId = preg_replace('/^games@@ready/', '', $data);
  564. $res = (new RoomService($telegram))->ready($roomId, $chatId, $messageId);
  565. foreach ($res as $r) {
  566. if (isset($r['message_id'])) {
  567. $telegram->editMessageText($r);
  568. } else {
  569. $telegram->sendMessage($r);
  570. }
  571. }
  572. }
  573. //结算游戏
  574. $pattern = "/^games@@settle\d+$/";
  575. if (preg_match($pattern, $data)) {
  576. $roomId = preg_replace('/^games@@settle/', '', $data);
  577. $res = (new SettlementService())->settle($roomId, $chatId, $messageId);
  578. if (isset($res['message_id'])) $telegram->editMessageText($res);
  579. else $telegram->sendMessage($res);
  580. }
  581. //在线房间,下一页
  582. $pattern = "/^onlineNextPage@@\d+$/";
  583. if (preg_match($pattern, $data)) {
  584. $page = preg_replace('/^onlineNextPage@@/', '', $data);
  585. $page = intval($page);
  586. $res = (new OnLineService())->getList($chatId, $messageId, $page);
  587. $telegram->editMessageText($res);
  588. }
  589. //我的战绩,下一页
  590. $pattern = "/^recordNextPage@@\d+$/";
  591. if (preg_match($pattern, $data)) {
  592. $page = preg_replace('/^recordNextPage@@/', '', $data);
  593. $page = intval($page);
  594. $res = (new RecordService())->getList($chatId, $firstName, $messageId, $page);
  595. $telegram->editMessageText($res);
  596. }
  597. //加入房间
  598. $pattern = "/^join@@\d+$/";
  599. if (preg_match($pattern, $data)) {
  600. $roomId = preg_replace('/^join@@/', '', $data);
  601. $res = (new RoomService($telegram))->join($chatId, $roomId, $messageId);
  602. foreach ($res as $r) {
  603. if (isset($r['message_id'])) {
  604. $telegram->editMessageText($r);
  605. } else {
  606. $telegram->sendMessage($r);
  607. }
  608. }
  609. }
  610. //输入房间号加入房间
  611. if ($data === 'join@@room') {
  612. $res = OnLineService::joinRoom($chatId, $messageId);
  613. $telegram->editMessageText($res);
  614. }
  615. //通过频道加入房间
  616. $pattern = "/^channelJoin@@\d+$/";
  617. if (preg_match($pattern, $data)) {
  618. $roomId = preg_replace('/^channelJoin@@/', '', $data);
  619. $res = (new RoomService($telegram))->join($from->id, $roomId);
  620. foreach ($res as $r) {
  621. if (isset($r['message_id'])) {
  622. $telegram->editMessageText($r);
  623. } else {
  624. $telegram->sendMessage($r);
  625. }
  626. }
  627. }
  628. DB::commit();
  629. } //
  630. catch (MessageException $e) {
  631. DB::rollBack();
  632. $msg = $e->getMessage();
  633. $msg = json_decode($msg, true);
  634. $telegram->sendMessage($msg);
  635. } //
  636. catch (TelegramSDKException $e) {
  637. DB::rollBack();
  638. $m = new Message();
  639. $m->json = $e->getMessage();
  640. $m->save();
  641. $telegram->sendMessage([
  642. 'chat_id' => $chatId,
  643. 'text' => '‼️‼️系统发生了错误,请联系客服'
  644. ]);
  645. }//
  646. catch (\Exception $e) {
  647. DB::rollBack();
  648. $m = new Message();
  649. $m->json = json_encode([
  650. 'line' => $e->getLine(),
  651. 'message' => $e->getMessage()
  652. ]);
  653. $m->save();
  654. $telegram->sendMessage([
  655. 'chat_id' => $chatId,
  656. 'text' => '‼️‼️系统发生了错误,请联系客服'
  657. ]);
  658. }
  659. } //
  660. else {
  661. $update = $request->all();
  662. if (isset($update['message'])) {
  663. $message = $update['message'];
  664. $chatId = $message['chat']['id'];
  665. $messageId = $message['message_id'];
  666. DB::beginTransaction();
  667. try {
  668. $m = new Message();
  669. $m->json = json_encode($update);
  670. $m->save();
  671. $user = User::where('member_id', $chatId)->first();
  672. if (!$user) {
  673. $user = new User();
  674. $user->member_id = $chatId;
  675. }
  676. $user->first_name = $message['chat']['first_name'];
  677. $user->save();
  678. //给每个用户生成一个专属的USDT钱包
  679. WalletService::getUserWallet($chatId);
  680. //用户发送图片,结算截图
  681. if (isset($message['photo'])) {
  682. $stepStatus = Cache::get(get_step_key($chatId), -1);
  683. $stepStatus = intval($stepStatus);
  684. //结算截图
  685. if ($stepStatus === StepStatus::INPUT_IMAGE) {
  686. $photo = $message['photo'][count($message['photo']) - 1];
  687. $res = (new SettlementService())->photo($photo, $chatId);
  688. if ($res) $telegram->sendMessage($res);
  689. }//
  690. //充值截图
  691. else if ($stepStatus === StepStatus::INPUT_TOP_UP_IMAGE) {
  692. $photo = $message['photo'][count($message['photo']) - 1];
  693. $res = TopUpService::photo($chatId, $photo);
  694. if (isset($res['message_id'])) $telegram->editMessageText($res);
  695. else $telegram->sendMessage($res);
  696. }
  697. } //用户发送了消息
  698. else if (isset($message['text'])) {
  699. $text = $message['text'];
  700. if ($message['chat']['type'] === 'private') {
  701. switch ($text) {
  702. //教程帮助
  703. case "/tutorial":
  704. Util::delCache($chatId);
  705. $res = (new TutorialService())->index($chatId);
  706. $telegram->sendMessage($res);
  707. break;
  708. //提现管理
  709. case "/withdraw":
  710. Util::delCache($chatId);
  711. $res = WithdrawService::index($chatId, $message['chat']['first_name']);
  712. $telegram->sendMessage($res);
  713. break;
  714. //我的战绩
  715. case "/record":
  716. Util::delCache($chatId);
  717. $res = (new RecordService())->getList($chatId, $message['chat']['first_name']);
  718. $telegram->sendMessage($res);
  719. break;
  720. //充值管理
  721. case "/topup":
  722. Util::delCache($chatId);
  723. $res = (new TopUpService())->index($chatId, $message['chat']['first_name']);
  724. $telegram->sendMessage($res);
  725. break;
  726. //在线房间
  727. case"/online":
  728. Util::delCache($chatId);
  729. $data = (new OnLineService())->getList($chatId, null, 1);
  730. $telegram->sendMessage($data);
  731. break;
  732. //创建房间
  733. case "/room":
  734. Util::delCache($chatId);
  735. $roomService = new RoomService($telegram);
  736. $res = $roomService->create($chatId);
  737. $telegram->sendMessage($res);
  738. break;
  739. case "/start":
  740. Util::delCache($chatId);
  741. $user = User::where('member_id', $chatId)->first();
  742. if (!$user) {
  743. $user = new User();
  744. $user->member_id = $chatId;
  745. }
  746. $user->first_name = $message['chat']['first_name'];
  747. $user->save();
  748. //给每个用户生成一个专属的USDT钱包
  749. WalletService::getUserWallet($chatId);
  750. $username = config('services.telegram.username');
  751. $telegram->sendMessage(['chat_id' => $chatId, 'text' => "🎉欢迎使用 @{$username}"]);
  752. break;
  753. default:
  754. $stepStatus = Cache::get(get_step_key($chatId), -1);
  755. $stepStatus = intval($stepStatus);
  756. switch ($stepStatus) {
  757. case StepStatus::INPUT_GAME_INTRODUCTION:
  758. $res = (new RoomService($telegram))->setIntroduction($chatId, $text, $messageId);
  759. if (isset($res['message_id'])) $telegram->editMessageText($res);
  760. else $telegram->sendMessage($res);
  761. break;
  762. case StepStatus::INPUT_TOP_UP_MONEY:
  763. $res = TopUpService::inputAmount($chatId, $text, $messageId);
  764. if (isset($res['message_id'])) $telegram->editMessageText($res);
  765. else $telegram->sendMessage($res);
  766. break;
  767. case StepStatus::INPUT_JOIN_ROOM_ID:
  768. $res = OnLineService::join($chatId, $text, $messageId);
  769. foreach ($res as $r) {
  770. if (isset($r['message_id'])) $telegram->editMessageText($r);
  771. else $telegram->sendMessage($r);
  772. }
  773. break;
  774. case StepStatus::INPUT_ADDRESS_ALIAS:
  775. $res = WithdrawService::inputAlias($chatId, $text, $messageId);
  776. $telegram->sendMessage($res);
  777. break;
  778. case StepStatus::INPUT_ADDRESS_TRC20:
  779. $res = WithdrawService::inputAddress($chatId, $text, $messageId);
  780. $telegram->sendMessage($res);
  781. break;
  782. case StepStatus::INPUT_WITHDRAW_MONEY:
  783. $res = (new WithdrawService())->inputAmount($chatId, $text, $messageId);
  784. foreach ($res as $r) $telegram->sendMessage($r);
  785. break;
  786. case StepStatus::INPUT_TOP_UP_IMAGE:
  787. $telegram->sendMessage([
  788. 'chat_id' => $chatId,
  789. 'text' => "输入错误,请上传充值截图!",
  790. 'reply_to_message_id' => $messageId
  791. ]);
  792. break;
  793. case StepStatus::INPUT_IMAGE:
  794. $telegram->sendMessage([
  795. 'chat_id' => $chatId,
  796. 'text' => "输入错误,请上传战绩截图!",
  797. 'reply_to_message_id' => $messageId
  798. ]);
  799. break;
  800. //输入结算分数
  801. case StepStatus::INPUT_SCORE:
  802. $res = (new SettlementService())->submitSettle($chatId, $text, $messageId);
  803. foreach ($res['data'] as $r) $telegram->sendMessage($r);
  804. break;
  805. //输入游戏名称
  806. case StepStatus::INPUT_GAME_NAME:
  807. $gameName = $text;
  808. $roomService = new RoomService($telegram);
  809. $res = $roomService->setGameName($chatId, $gameName, $messageId);
  810. if (isset($res['message_id'])) $telegram->editMessageText($res);
  811. else $telegram->sendMessage($res);
  812. break;
  813. //输入房间号
  814. case StepStatus::INPUT_ROOM_ID:
  815. $roomId = $text;
  816. $roomService = new RoomService($telegram);
  817. $res = $roomService->setNum($chatId, $roomId, $messageId);
  818. if (isset($res['message_id'])) $telegram->editMessageText($res);
  819. else $telegram->sendMessage($res);
  820. break;
  821. //输入是否允许中途加入
  822. case StepStatus::INPUT_MIDWAY:
  823. $midway = $text;
  824. $roomService = new RoomService($telegram);
  825. $res = $roomService->setGameMidway($chatId, $midway, $messageId);
  826. if (isset($res['message_id'])) $telegram->editMessageText($res);
  827. else $telegram->sendMessage($res);
  828. break;
  829. //输入 人数/局数
  830. case StepStatus::INPUT_ROOM_NUM:
  831. $array = explode('/', $text);
  832. $roomService = new RoomService($telegram);
  833. $res = $roomService->setNumberOfGames($chatId, $array, $messageId);
  834. if (isset($res['message_id'])) $telegram->editMessageText($res);
  835. else $telegram->sendMessage($res);
  836. break;
  837. //输入 游戏ID
  838. case StepStatus::INPUT_GAME_ID:
  839. $gameId = $text;
  840. $rs = new RoomService($telegram);
  841. $res = $rs->setGameID($chatId, $gameId, $messageId);
  842. foreach ($res as $r) {
  843. if (isset($r['message_id'])) $telegram->editMessageText($r);
  844. else $telegram->sendMessage($r);
  845. }
  846. break;
  847. break;
  848. default:
  849. Util::delCache($chatId);
  850. $res = (new TutorialService())->index($chatId);
  851. $telegram->sendMessage($res);
  852. break;
  853. }
  854. break;
  855. }
  856. }
  857. }
  858. DB::commit();
  859. } //
  860. catch (MessageException $e) {
  861. DB::rollBack();
  862. $m = new Message();
  863. $m->json = $e->getMessage();
  864. $m->save();
  865. try {
  866. $msg = $e->getMessage();
  867. $msg = json_decode($msg, true);
  868. $telegram->sendMessage($msg);
  869. } catch (TelegramSDKException $e) {
  870. }
  871. } //
  872. catch (TelegramSDKException $e) {
  873. DB::rollBack();
  874. $m = new Message();
  875. $m->json = $e->getMessage();
  876. $m->save();
  877. $telegram->sendMessage([
  878. 'chat_id' => $chatId,
  879. 'text' => '‼️‼️系统发生了错误,请联系客服'
  880. ]);
  881. }//
  882. catch (\Exception $e) {
  883. DB::rollBack();
  884. $m = new Message();
  885. $m->json = $e->getMessage();
  886. $m->save();
  887. $telegram->sendMessage([
  888. 'chat_id' => $chatId,
  889. 'text' => '‼️‼️系统发生了错误,请联系客服'
  890. ]);
  891. }
  892. }
  893. }
  894. return response()->json(['status' => 'ok']);
  895. }
  896. }