提交 efef5042 编写于 作者: 小傅哥's avatar 小傅哥

小傅哥,feat:第5节:整合知识星球与ChatGPT

上级 72ad219e
......@@ -11,5 +11,29 @@
<artifactId>chatbot-api-application</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>cn.bugstack.ai</groupId>
<artifactId>chatbot-api-domain</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package cn.bugstack.chatbot.api.application.job;
import cn.bugstack.chatbot.api.domain.ai.IOpenAI;
import cn.bugstack.chatbot.api.domain.zsxq.IZsxqApi;
import cn.bugstack.chatbot.api.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates;
import cn.bugstack.chatbot.api.domain.zsxq.model.vo.Topics;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Resource;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Random;
/**
* @author 小傅哥,微信:fustack
* @description 问题任务
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
@EnableScheduling
@Configuration
public class ChatbotSchedule {
private Logger logger = LoggerFactory.getLogger(ChatbotSchedule.class);
@Value("${chatbot-api.groupId}")
private String groupId;
@Value("${chatbot-api.cookie}")
private String cookie;
@Resource
private IZsxqApi zsxqApi;
@Resource
private IOpenAI openAI;
// 表达式:cron.qqe2.com
@Scheduled(cron = "0/30 * * * * ?")
public void run() {
try {
if (new Random().nextBoolean()) {
logger.info("随机打烊中...");
return;
}
GregorianCalendar calendar = new GregorianCalendar();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour > 22 || hour < 7) {
logger.info("打烊时间不工作,AI 下班了!");
return;
}
// 1. 检索问题
UnAnsweredQuestionsAggregates unAnsweredQuestionsAggregates = zsxqApi.queryUnAnsweredQuestionsTopicId(groupId, cookie);
logger.info("检索结果:{}", JSON.toJSONString(unAnsweredQuestionsAggregates));
List<Topics> topics = unAnsweredQuestionsAggregates.getResp_data().getTopics();
if (null == topics || topics.isEmpty()) {
logger.info("本次检索未查询到待会答问题");
return;
}
// 2. AI 回答
Topics topic = topics.get(0);
String answer = openAI.doChatGPT(topic.getQuestion().getText().trim());
// 3. 问题回复
boolean status = zsxqApi.answer(groupId, cookie, topic.getTopic_id(), answer, false);
logger.info("编号:{} 问题:{} 回答:{} 状态:{}", topic.getTopic_id(), topic.getQuestion().getText(), answer, status);
} catch (Exception e) {
logger.error("自动回答问题异常", e);
}
}
}
......@@ -42,12 +42,6 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<classifier>jdk15</classifier><!-- jdk版本 -->
</dependency>
</dependencies>
</project>
\ No newline at end of file
package cn.bugstack.chatbot.api.domain.zsxq;
import cn.bugstack.chatbot.api.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
/**
......
......@@ -6,7 +6,7 @@ import cn.bugstack.chatbot.api.domain.zsxq.model.req.AnswerReq;
import cn.bugstack.chatbot.api.domain.zsxq.model.req.ReqData;
import cn.bugstack.chatbot.api.domain.zsxq.model.res.AnswerRes;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
......@@ -73,7 +73,7 @@ public class ZsxqApi implements IZsxqApi {
*/
AnswerReq answerReq = new AnswerReq(new ReqData(text, silenced));
String paramJson = JSONObject.fromObject(answerReq).toString();
String paramJson = JSONObject.toJSONString(answerReq);
StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json", "UTF-8"));
post.setEntity(stringEntity);
......
......@@ -78,6 +78,11 @@
<artifactId>chatbot-api-domain</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.bugstack.ai</groupId>
<artifactId>chatbot-api-application</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -2,6 +2,7 @@ package cn.bugstack.chatbot.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
/**
* @author 小傅哥,微信:fustack
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册