Skip to content

Commit fee5d84

Browse files
committed
钉钉
1 parent e2e569a commit fee5d84

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed
Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
package com.cyl.ctrbt.controller;
22

3+
import cn.hutool.json.JSONObject;
4+
import cn.hutool.json.JSONUtil;
5+
import com.cyl.ctrbt.openai.OpenAiUtil;
6+
import com.dingtalk.api.DefaultDingTalkClient;
7+
import com.dingtalk.api.DingTalkClient;
8+
import com.dingtalk.api.request.OapiRobotSendRequest;
9+
import com.dingtalk.api.response.OapiRobotSendResponse;
10+
import com.taobao.api.ApiException;
11+
import com.theokanning.openai.completion.CompletionChoice;
12+
import org.springframework.beans.factory.annotation.Autowired;
313
import org.springframework.http.ResponseEntity;
4-
import org.springframework.web.bind.annotation.GetMapping;
5-
import org.springframework.web.bind.annotation.RequestMapping;
6-
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.bind.annotation.*;
15+
16+
import java.util.List;
17+
import java.util.Map;
18+
import java.util.stream.Collectors;
719

820
@RequestMapping("/ding-talk")
921
@RestController
1022
public class DingTalkController {
11-
@GetMapping("/hello-world")
12-
public ResponseEntity<String> helloWorld() {
13-
return ResponseEntity.ok("hello world");
23+
24+
@Autowired
25+
private OpenAiUtil openAiUtil;
26+
27+
@PostMapping("/receive")
28+
public String helloRobots(@RequestBody(required = false) JSONObject json) {
29+
System.out.println(JSONUtil.toJsonStr(json));
30+
String content = json.getJSONObject("text").get("content").toString().replaceAll(" ", "");
31+
String sessionWebhook = json.getStr("sessionWebhook");
32+
DingTalkClient client = new DefaultDingTalkClient(sessionWebhook);
33+
if ("text".equals(json.getStr("msgtype"))) {
34+
text(client,content);
35+
}
36+
return null;
37+
}
38+
39+
private void text(DingTalkClient client,String content) {
40+
try {
41+
OapiRobotSendRequest request = new OapiRobotSendRequest();
42+
request.setMsgtype("text");
43+
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
44+
List<CompletionChoice> completionChoices = openAiUtil.sendComplete(content);
45+
text.setContent(completionChoices.stream().map(it->it.getText()).collect(Collectors.joining(";")));
46+
request.setText(text);
47+
OapiRobotSendResponse response = client.execute(request);
48+
System.out.println(response.getBody());
49+
} catch (ApiException e) {
50+
e.printStackTrace();
1451
}
52+
}
1553
}

0 commit comments

Comments
 (0)