McBotController.java 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package com.malk.mc.controller;
  2. import cn.hutool.http.HttpUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.malk.server.common.McException;
  5. import com.malk.server.common.McR;
  6. import com.malk.service.dingtalk.DDClient;
  7. import com.malk.service.dingtalk.DDClient_Extension;
  8. import com.malk.utils.UtilMap;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestBody;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. @Slf4j
  18. @RestController
  19. @RequestMapping("/bot/")
  20. public class McBotController {
  21. @Autowired
  22. private DDClient_Extension ddClientExtension;
  23. @Autowired
  24. private DDClient ddClient;
  25. @PostMapping("groupMsg/send")
  26. public McR sendGroupMsg(@RequestBody JSONObject param){
  27. log.info("推送消息:{}",param);
  28. McException.assertParamException_Null(param,"msgParam","msgKey","openConversationId","robotCode","coolAppCode");
  29. Map map = ddClientExtension.sendGroupMessages(ddClient.getAccessToken(), param.getJSONObject("msgParam"), param.getString("msgKey"), param.getString("openConversationId")
  30. , param.getString("robotCode"), param.getString("coolAppCode"));
  31. return McR.success(map);
  32. }
  33. @PostMapping("card/createAndDeliver")
  34. public McR createAndDeliverCard(@RequestBody JSONObject param){
  35. log.info("推送消息:{}",param);
  36. McException.assertParamException_Null(param,"cardData","outTrackId","openSpaceId","robotCode","cardTemplateId");
  37. Map<String,Object> data = new HashMap();
  38. data.put("imGroupOpenDeliverModel",UtilMap.map("robotCode",param.getString("robotCode")));
  39. data.put("imGroupOpenSpaceModel",UtilMap.map("supportForward",true));
  40. if(param.containsKey("extInfo")){
  41. data.putAll(param.getJSONObject("extInfo"));
  42. }
  43. Map map = ddClientExtension.createAndDeliverCards(ddClient.getAccessToken(),param.getString("cardTemplateId"),param.getString("outTrackId"),
  44. UtilMap.map("cardParamMap",param.getJSONObject("cardData")),param.getString("openSpaceId"),data);
  45. return McR.success(map);
  46. }
  47. @PostMapping("msg")
  48. public McR msg(@RequestBody JSONObject param){
  49. log.info("机器人消息:{}",param);
  50. String text=param.getJSONObject("text").getString("content");
  51. if(text.contains("群信息")){
  52. sendBotTextMsgByWebhook(param.getString("sessionWebhook"),
  53. "群ID:"+param.getString("conversationId"));
  54. }
  55. return McR.success();
  56. }
  57. private void sendBotTextMsgByWebhook(String sessionWebhook,String text){
  58. Map<String,Object> map=new HashMap<>();
  59. map.put("msgtype","text");
  60. map.put("text",UtilMap.map("content",text));
  61. HttpUtil.createPost(sessionWebhook).body(JSONObject.toJSONString(map)).execute();
  62. }
  63. }