Home.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. ['菜单1', '菜单2'], // 第一排按钮
  111. ['帮助', '设置'] // 第二排按钮
  112. ];
  113. $replyMarkup = [
  114. 'keyboard' => $keyboard,
  115. 'resize_keyboard' => true, // 自适应大小
  116. 'one_time_keyboard' => false, // 保持显示,不会点击后收起
  117. ];
  118. $telegram->sendMessage([
  119. 'chat_id' => $chatId,
  120. 'text' => '请选择一个操作:',
  121. 'reply_markup' => json_encode($replyMarkup),
  122. ]);
  123. }
  124. public function getUpdates()
  125. {
  126. try {
  127. $telegram = new Api(config('services.telegram.token'));
  128. $telegram->deleteWebhook();
  129. $res = $telegram->getUpdates();
  130. } catch (TelegramSDKException $e) {
  131. return $this->error($e->getCode(), $e->getMessage());
  132. }
  133. return $this->success($res);
  134. }
  135. public function setWebHook()
  136. {
  137. $this->setMyCommands();
  138. try {
  139. $telegram = new Api(config('services.telegram.token'));
  140. // 设置 Webhook
  141. $webhookUrl = url('/api/onMessage'); // Webhook URL,指向刚才定义的路由
  142. $allowed_updates = [
  143. 'message', 'callback_query',
  144. // 'message_reaction', 'message_reaction_count',
  145. // 'removed_chat_boost', 'chat_boost',
  146. // 'chat_join_request', 'chat_member'
  147. ];
  148. $res = $telegram->setWebhook(['url' => $webhookUrl, 'allowed_updates' => $allowed_updates, 'drop_pending_updates' => true]);
  149. } catch (TelegramSDKException $e) {
  150. return $this->error($e->getCode(), $e->getMessage());
  151. }
  152. return $this->success($res);
  153. }
  154. }