CommandController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Http\Controllers\Admin\Telegram;
  3. use Illuminate\Http\Request;
  4. use App\Services\AipHttpClient;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\Telegram\TelegramBot;
  8. use App\Models\Telegram\TelegramBotCommand;
  9. use Telegram\Bot\Api;
  10. class CommandController extends Controller
  11. {
  12. public $CommandType = ["1" => "通用", "2" => "私聊", "3" => "群聊"];
  13. public function index(Request $request)
  14. {
  15. $CommandType = $this->CommandType;
  16. $botData = TelegramBot::pluck('bot_username','rid');
  17. return view('admin.telegram.command.index',compact("CommandType","botData"));
  18. }
  19. //列表
  20. public function getData(Request $request)
  21. {
  22. $model = TelegramBotCommand::from('telegram_bot_command as a')
  23. ->join('telegram_bot as b','a.bot_rid','b.rid')
  24. ->where(function($query) use ($request){
  25. if ($request->command != '') {
  26. $query->where('a.command', 'like' ,"%" . $request->command ."%");
  27. }
  28. if ($request->bot_rid != '') {
  29. $query->where('a.bot_rid', $request->bot_rid);
  30. }
  31. });
  32. $count = $model->count();
  33. $limit = $request->limit ?? 15;
  34. $offset = $request->page ? ($request->page - 1) * $limit : 0;
  35. $data = $model->limit($limit)->offset($offset)->select('a.*','a.bot_rid','b.bot_token','b.bot_firstname','b.bot_username')->orderBy('a.rid','desc')->get();
  36. $keys = ["CommandType" => $this->CommandType];
  37. $data = $data->map(function($query) use ($keys){
  38. $query->command_type_val = $keys["CommandType"][$query->command_type];
  39. return $query;
  40. });
  41. return ['code' => '0', 'data' => $data, 'count' => $count];
  42. }
  43. //添加
  44. public function add(Request $request)
  45. {
  46. $data = TelegramBotCommand::where('bot_rid', $request->bot_rid)->where('command', $request->command)->first();
  47. if(!empty($data)){
  48. return $this->responseData(400, '机器人命令已存在');
  49. }
  50. $res = TelegramBotCommand::create([
  51. 'bot_rid' => $request->bot_rid,
  52. 'command' => $request->command,
  53. 'description' => $request->description,
  54. 'command_type' => $request->command_type ?? 1,
  55. 'seq_sn' => $request->seq_sn ?? 0,
  56. 'create_time' => nowDate()
  57. ]);
  58. return $res ? $this->responseData(200, '添加成功') : $this->responseData(400, '添加失败');
  59. }
  60. //删除
  61. public function delete(Request $request)
  62. {
  63. $res = TelegramBotCommand::where('rid', $request->rid)->delete();
  64. return $res ? $this->responseData(200, '删除成功') : $this->responseData(400, '删除失败');
  65. }
  66. //编辑
  67. public function update(Request $request)
  68. {
  69. $data = TelegramBotCommand::where('bot_rid', $request->bot_rid)->where('command', $request->command)->where('rid','<>',$request->rid)->first();
  70. if(!empty($data)){
  71. return $this->responseData(400, '机器人命令已存在');
  72. }
  73. DB::beginTransaction();
  74. try {
  75. $data = TelegramBotCommand::where('rid', $request->rid)->first();
  76. $data->bot_rid = $request->bot_rid;
  77. $data->command = $request->command;
  78. $data->description = $request->description;
  79. $data->command_type = $request->command_type;
  80. $data->seq_sn = $request->seq_sn;
  81. $data->update_time = nowDate();
  82. $data->save();
  83. DB::commit();
  84. return $this->responseData(200, '更新成功');
  85. } catch (\Exception $e) {
  86. DB::rollBack();
  87. return $this->responseData(400, '更新失败'.$e->getMessage());
  88. }
  89. }
  90. //同步
  91. public function sync(Request $request)
  92. {
  93. $data = TelegramBot::where('rid', $request->bot_rid)->first();
  94. if(empty($data)){
  95. return $this->responseData(400, '数据不存在');
  96. }
  97. $commandData = TelegramBotCommand::where('bot_rid', $request->bot_rid)->get();
  98. $commandsone = [];
  99. $commandstwo = [];
  100. if($commandData->count() > 0){
  101. $sone = 0;
  102. $stwo = 0;
  103. foreach ($commandData as $k => $v) {
  104. //私聊
  105. if($v->command_type == 1 || $v->command_type == 2){
  106. $commandone = [];
  107. //内联按钮
  108. $commandone['command'] = $v->command;
  109. $commandone['description'] = $v->description;
  110. $commandsone[$sone] = $commandone;
  111. $sone++;
  112. }
  113. //群聊
  114. if($v->command_type == 1 || $v->command_type == 3){
  115. $commandtwo = [];
  116. //内联按钮
  117. $commandtwo['command'] = $v->command;
  118. $commandtwo['description'] = $v->description;
  119. $commandstwo[$stwo] = $commandtwo;
  120. $stwo++;
  121. }
  122. }
  123. }
  124. //私聊
  125. $encodedCommandsone = json_encode($commandsone);
  126. $sendmessageurl = "https://api.telegram.org/bot". $data->bot_token ."/setMyCommands?commands=".$encodedCommandsone."&scope=".'{"type":"all_private_chats"}';
  127. $res = Get_Curl($sendmessageurl);
  128. //群聊
  129. $encodedCommandstwo = json_encode($commandstwo);
  130. $sendmessageurl = "https://api.telegram.org/bot". $data->bot_token ."/setMyCommands?commands=".$encodedCommandstwo."&scope=".'{"type":"all_group_chats"}';
  131. $res = Get_Curl($sendmessageurl);
  132. if(empty($res)){
  133. return $this->responseData(400, '同步失败,检查服务器是否可以访问外网');
  134. }
  135. $res = json_decode($res,true);
  136. if($res['ok'] && $res['result']){
  137. return $this->responseData(200, '同步成功');
  138. }
  139. return $this->responseData(400, '同步失败:'.$res['description']);
  140. }
  141. //复制
  142. public function copyPaste(Request $request)
  143. {
  144. if(empty($request->copy_bot_rid) || empty($request->paste_bot_rid)){
  145. return $this->responseData(400, '覆盖和来源机器人必填');
  146. }
  147. if($request->copy_bot_rid == $request->paste_bot_rid){
  148. return $this->responseData(400, '覆盖和来源机器人不能一致');
  149. }
  150. $copyData = TelegramBot::where('rid', $request->copy_bot_rid)->first();
  151. if(empty($copyData)){
  152. return $this->responseData(400, '来源机器人不存在');
  153. }
  154. $pasteData = TelegramBot::where('rid', $request->paste_bot_rid)->first();
  155. if(empty($pasteData)){
  156. return $this->responseData(400, '覆盖机器人不存在');
  157. }
  158. $data = TelegramBotCommand::where('bot_rid', $request->copy_bot_rid)->get();
  159. if($data->count() == 0){
  160. return $this->responseData(400, '来源机器人无数据可复制');
  161. }
  162. DB::beginTransaction();
  163. try {
  164. TelegramBotCommand::where('bot_rid', $request->paste_bot_rid)->delete();
  165. TelegramBotCommand::insertUsing([
  166. 'bot_rid', 'command', 'description', 'command_type','seq_sn','create_time'
  167. ], TelegramBotCommand::selectRaw(
  168. "$request->paste_bot_rid, command, description, command_type, seq_sn, sysdate()"
  169. )->where('bot_rid', $request->copy_bot_rid));
  170. DB::commit();
  171. return $this->responseData(200, '复制成功');
  172. } catch (\Exception $e) {
  173. DB::rollBack();
  174. return $this->responseData(400, '复制失败'.$e->getMessage());
  175. }
  176. }
  177. }