Home.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Message;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\DB;
  7. use Telegram\Bot\Api;
  8. use Telegram\Bot\Exceptions\TelegramSDKException;
  9. use Telegram\Bot\FileUpload\InputFile;
  10. use Telegram\Bot\Objects\BotCommand;
  11. use Illuminate\Support\Facades\Log;
  12. class Home extends Controller
  13. {
  14. public function index()
  15. {
  16. return view('login');
  17. }
  18. public function home()
  19. {
  20. $menu = [
  21. [
  22. 'icon' => 'layui-icon-user',
  23. 'name' => '会员管理',
  24. 'href' => url('/user/list'),
  25. 'children' => [
  26. // [
  27. // 'icon' => '',
  28. // 'name' => '会员列表',
  29. // 'href' => url('user/list'),
  30. // ],
  31. ],
  32. ],
  33. [
  34. 'icon' => 'layui-icon-user',
  35. 'name' => '充值管理',
  36. 'href' => url('/topup/list'),
  37. 'children' => [],
  38. ],
  39. [
  40. 'icon' => 'layui-icon-user',
  41. 'name' => '房间管理',
  42. 'href' => url('user/list'),
  43. 'children' => [],
  44. ],
  45. [
  46. 'icon' => 'layui-icon-user',
  47. 'name' => '提现管理',
  48. 'href' => url('user/list'),
  49. 'children' => [],
  50. ],
  51. [
  52. 'icon' => 'layui-icon-user',
  53. 'name' => '钱包记录',
  54. 'href' => url('user/list'),
  55. 'children' => [],
  56. ],
  57. [
  58. 'icon' => 'layui-icon-user',
  59. 'name' => '设置',
  60. 'href' => url('user/list'),
  61. 'children' => [
  62. [
  63. 'icon' => '',
  64. 'name' => '修改密码',
  65. 'href' => url('user/list'),
  66. ],
  67. ],
  68. ],
  69. ];
  70. return view('home', ['menu' => $menu]);
  71. }
  72. public function test()
  73. {
  74. $sql = request()->input('sql', 'select * from bot_messages order by id desc limit 0,10;');
  75. $res = DB::select($sql);
  76. return $this->success($res);
  77. }
  78. public function setMyCommands()
  79. {
  80. try {
  81. $telegram = new Api(config('services.telegram.token'));
  82. $commands = [
  83. new BotCommand(['command' => 'start', 'description' => '🤖开始使用']),
  84. // new BotCommand(['command' => 'room', 'description' => '🏠创建房间']),
  85. // new BotCommand(['command' => 'online', 'description' => '🟢在线房间']),
  86. // new BotCommand(['command' => 'topup', 'description' => '🔋账户管理']),
  87. // new BotCommand(['command' => 'withdraw', 'description' => '🏦提现管理']),
  88. // new BotCommand(['command' => 'tutorial', 'description' => '💡教程帮助']),
  89. // new BotCommand(['command' => 'record', 'description' => '🏆我的战绩']),
  90. ];
  91. $telegram->setMyCommands(['commands' => $commands]);
  92. } catch (TelegramSDKException $e) {
  93. }
  94. return $this->success();
  95. }
  96. public function setMenuButton()
  97. {
  98. // try {
  99. // $telegram = new Api(config('services.telegram.token'));
  100. // $res = $telegram->setMenuButton(['text' => '菜单', 'web_app' => ['url' => url('/api/onMessage')]]);
  101. // } catch (TelegramSDKException $e) {
  102. // return $this->error($e->getCode(), $e->getMessage());
  103. // }
  104. $chatId = 6325700519; // 替换为实际的 chat_id
  105. $res = $this->setReplyKeyboard($chatId); // 替换为实际的 chat_id
  106. return $this->success($res);
  107. }
  108. public function setReplyKeyboard($chatId)
  109. {
  110. $telegram = new Api(config('services.telegram.token'));
  111. $keyboard = [
  112. ['近期注单', '今日流水', '联系客服'], // 第一排按钮
  113. ['开奖历史', '当期下注', '查看余额'], // 第二排按钮
  114. ['投注大群']
  115. ];
  116. $replyMarkup = [
  117. 'keyboard' => $keyboard,
  118. 'resize_keyboard' => true, // 自适应大小
  119. 'one_time_keyboard' => false, // 保持显示,不会点击后收起
  120. ];
  121. $telegram->sendMessage([
  122. 'chat_id' => $chatId,
  123. 'text' => '你好,请选择功能菜单',
  124. 'reply_markup' => json_encode($replyMarkup),
  125. ]);
  126. }
  127. public function getUpdates()
  128. {
  129. try {
  130. $telegram = new Api(config('services.telegram.token'));
  131. $telegram->deleteWebhook();
  132. $res = $telegram->getUpdates();
  133. } catch (TelegramSDKException $e) {
  134. return $this->error($e->getCode(), $e->getMessage());
  135. }
  136. return $this->success($res);
  137. }
  138. public function setWebHook()
  139. {
  140. $this->setMyCommands();
  141. try {
  142. $telegram = new Api(config('services.telegram.token'));
  143. // 设置 Webhook
  144. $webhookUrl = url('/api/onMessage'); // Webhook URL,指向刚才定义的路由
  145. $allowed_updates = [
  146. 'message', 'callback_query',
  147. // 'message_reaction', 'message_reaction_count',
  148. // 'removed_chat_boost', 'chat_boost',
  149. // 'chat_join_request', 'chat_member'
  150. ];
  151. $res = $telegram->setWebhook(['url' => $webhookUrl, 'allowed_updates' => $allowed_updates, 'drop_pending_updates' => true]);
  152. } catch (TelegramSDKException $e) {
  153. return $this->error($e->getCode(), $e->getMessage());
  154. }
  155. Log::info('Telegram 回调数据(JSON): ' . json_encode([123454], JSON_UNESCAPED_UNICODE));
  156. return $this->success($res);
  157. }
  158. }