|
1 | 1 | package com.cyl.ctrbt.controller;
|
2 | 2 |
|
| 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; |
3 | 13 | 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; |
7 | 19 |
|
8 | 20 | @RequestMapping("/ding-talk")
|
9 | 21 | @RestController
|
10 | 22 | 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(); |
14 | 51 | }
|
| 52 | + } |
15 | 53 | }
|
0 commit comments