TelegramWebHook.php 46 KB

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