Home.php 5.9 KB

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