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