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