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

小傅哥,feat:第3节:知识星球API调用,领域服务开发

上级 c197d9f9
......@@ -11,5 +11,43 @@
<artifactId>chatbot-api-domain</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>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<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;
/**
* @author 小傅哥,微信:fustack
* @description 知识星球 API 接口
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public interface IZsxqApi {
UnAnsweredQuestionsAggregates queryUnAnsweredQuestionsTopicId(String groupId, String cookie) throws IOException;
boolean answer(String groupId, String cookie, String topicId, String text, boolean silenced) throws IOException;
}
\ No newline at end of file
package cn.bugstack.chatbot.api.domain.zsxq.model.aggregates;
import cn.bugstack.chatbot.api.domain.zsxq.model.res.RespData;
/**
* @author 小傅哥,微信:fustack
* @description 未回答问题的聚合信息
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class UnAnsweredQuestionsAggregates {
private boolean succeeded;
private RespData resp_data;
public boolean isSucceeded() {
return succeeded;
}
public void setSucceeded(boolean succeeded) {
this.succeeded = succeeded;
}
public RespData getResp_data() {
return resp_data;
}
public void setResp_data(RespData resp_data) {
this.resp_data = resp_data;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.req;
/**
* @author 小傅哥,微信:fustack
* @description 请求问答接口信息
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class AnswerReq {
private ReqData req_data;
public AnswerReq(ReqData req_data) {
this.req_data = req_data;
}
public ReqData getReq_data() {
return req_data;
}
public void setReq_data(ReqData req_data) {
this.req_data = req_data;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.req;
/**
* @author 小傅哥,微信:fustack
* @description 请求对象
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class ReqData {
private String text;
private String[] image_ids = new String[]{};
private boolean silenced;
public ReqData(String text, boolean silenced) {
this.text = text;
this.silenced = silenced;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String[] getImage_ids() {
return image_ids;
}
public void setImage_ids(String[] image_ids) {
this.image_ids = image_ids;
}
public boolean isSilenced() {
return silenced;
}
public void setSilenced(boolean silenced) {
this.silenced = silenced;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.res;
/**
* @author 小傅哥,微信:fustack
* @description 请求问答接口结果
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class AnswerRes {
private boolean succeeded;
public boolean isSucceeded() {
return succeeded;
}
public void setSucceeded(boolean succeeded) {
this.succeeded = succeeded;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.res;
import cn.bugstack.chatbot.api.domain.zsxq.model.vo.Topics;
import java.util.List;
/**
* @author 小傅哥,微信:fustack
* @description 结果数据
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class RespData {
private List<Topics> topics;
public List<Topics> getTopics() {
return topics;
}
public void setTopics(List<Topics> topics) {
this.topics = topics;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.vo;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class Group {
private String group_id;
private String name;
private String type;
public void setGroup_id(String group_id){
this.group_id = group_id;
}
public String getGroup_id(){
return this.group_id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setType(String type){
this.type = type;
}
public String getType(){
return this.type;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.vo;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class Owner {
private String user_id;
private String name;
private String avatar_url;
private String location;
public void setUser_id(String user_id){
this.user_id = user_id;
}
public String getUser_id(){
return this.user_id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAvatar_url(String avatar_url){
this.avatar_url = avatar_url;
}
public String getAvatar_url(){
return this.avatar_url;
}
public void setLocation(String location){
this.location = location;
}
public String getLocation(){
return this.location;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.vo;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class OwnerDetail {
private int questions_count;
private String join_time;
public void setQuestions_count(int questions_count){
this.questions_count = questions_count;
}
public int getQuestions_count(){
return this.questions_count;
}
public void setJoin_time(String join_time){
this.join_time = join_time;
}
public String getJoin_time(){
return this.join_time;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.vo;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class Question {
private Owner owner;
private Questionee questionee;
private String text;
private boolean expired;
private boolean anonymous;
private OwnerDetail owner_detail;
private String owner_location;
public void setOwner(Owner owner){
this.owner = owner;
}
public Owner getOwner(){
return this.owner;
}
public void setQuestionee(Questionee questionee){
this.questionee = questionee;
}
public Questionee getQuestionee(){
return this.questionee;
}
public void setText(String text){
this.text = text;
}
public String getText(){
return this.text;
}
public void setExpired(boolean expired){
this.expired = expired;
}
public boolean getExpired(){
return this.expired;
}
public void setAnonymous(boolean anonymous){
this.anonymous = anonymous;
}
public boolean getAnonymous(){
return this.anonymous;
}
public void setOwner_detail(OwnerDetail owner_detail){
this.owner_detail = owner_detail;
}
public OwnerDetail getOwner_detail(){
return this.owner_detail;
}
public void setOwner_location(String owner_location){
this.owner_location = owner_location;
}
public String getOwner_location(){
return this.owner_location;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.vo;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class Questionee {
private String user_id;
private String name;
private String avatar_url;
private String description;
private String location;
public void setUser_id(String user_id){
this.user_id = user_id;
}
public String getUser_id(){
return this.user_id;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAvatar_url(String avatar_url){
this.avatar_url = avatar_url;
}
public String getAvatar_url(){
return this.avatar_url;
}
public void setDescription(String description){
this.description = description;
}
public String getDescription(){
return this.description;
}
public void setLocation(String location){
this.location = location;
}
public String getLocation(){
return this.location;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.vo;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class Topics {
private String topic_id;
private Group group;
private String type;
private Question question;
private boolean answered;
private int likes_count;
private int rewards_count;
private int comments_count;
private int reading_count;
private int readers_count;
private boolean digested;
private boolean sticky;
private String create_time;
private UserSpecific user_specific;
public void setTopic_id(String topic_id){
this.topic_id = topic_id;
}
public String getTopic_id(){
return this.topic_id;
}
public void setGroup(Group group){
this.group = group;
}
public Group getGroup(){
return this.group;
}
public void setType(String type){
this.type = type;
}
public String getType(){
return this.type;
}
public void setQuestion(Question question){
this.question = question;
}
public Question getQuestion(){
return this.question;
}
public void setAnswered(boolean answered){
this.answered = answered;
}
public boolean getAnswered(){
return this.answered;
}
public void setLikes_count(int likes_count){
this.likes_count = likes_count;
}
public int getLikes_count(){
return this.likes_count;
}
public void setRewards_count(int rewards_count){
this.rewards_count = rewards_count;
}
public int getRewards_count(){
return this.rewards_count;
}
public void setComments_count(int comments_count){
this.comments_count = comments_count;
}
public int getComments_count(){
return this.comments_count;
}
public void setReading_count(int reading_count){
this.reading_count = reading_count;
}
public int getReading_count(){
return this.reading_count;
}
public void setReaders_count(int readers_count){
this.readers_count = readers_count;
}
public int getReaders_count(){
return this.readers_count;
}
public void setDigested(boolean digested){
this.digested = digested;
}
public boolean getDigested(){
return this.digested;
}
public void setSticky(boolean sticky){
this.sticky = sticky;
}
public boolean getSticky(){
return this.sticky;
}
public void setCreate_time(String create_time){
this.create_time = create_time;
}
public String getCreate_time(){
return this.create_time;
}
public void setUser_specific(UserSpecific user_specific){
this.user_specific = user_specific;
}
public UserSpecific getUser_specific(){
return this.user_specific;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.model.vo;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
public class UserSpecific {
private boolean liked;
private boolean subscribed;
public void setLiked(boolean liked){
this.liked = liked;
}
public boolean getLiked(){
return this.liked;
}
public void setSubscribed(boolean subscribed){
this.subscribed = subscribed;
}
public boolean getSubscribed(){
return this.subscribed;
}
}
package cn.bugstack.chatbot.api.domain.zsxq.service;
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.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 org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
@Service
public class ZsxqApi implements IZsxqApi {
private Logger logger = LoggerFactory.getLogger(ZsxqApi.class);
@Override
public UnAnsweredQuestionsAggregates queryUnAnsweredQuestionsTopicId(String groupId, String cookie) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet get = new HttpGet("https://api.zsxq.com/v2/groups/" + groupId + "/topics?scope=unanswered_questions&count=20");
get.addHeader("cookie", cookie);
get.addHeader("Content-Type", "application/json;charset=utf8");
CloseableHttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String jsonStr = EntityUtils.toString(response.getEntity());
logger.info("拉取提问数据。groupId:{} jsonStr:{}", groupId, jsonStr);
return JSON.parseObject(jsonStr, UnAnsweredQuestionsAggregates.class);
} else {
throw new RuntimeException("queryUnAnsweredQuestionsTopicId Err Code is " + response.getStatusLine().getStatusCode());
}
}
@Override
public boolean answer(String groupId, String cookie, String topicId, String text, boolean silenced) throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("https://api.zsxq.com/v2/topics/" + topicId + "/answer");
post.addHeader("cookie", cookie);
post.addHeader("Content-Type", "application/json;charset=utf8");
post.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
/* 测试数据
String paramJson = "{\n" +
" \"req_data\": {\n" +
" \"text\": \"自己去百度!\\n\",\n" +
" \"image_ids\": [],\n" +
" \"silenced\": false\n" +
" }\n" +
"}";
*/
AnswerReq answerReq = new AnswerReq(new ReqData(text, silenced));
String paramJson = JSONObject.fromObject(answerReq).toString();
StringEntity stringEntity = new StringEntity(paramJson, ContentType.create("text/json", "UTF-8"));
post.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String jsonStr = EntityUtils.toString(response.getEntity());
logger.info("回答问题结果。groupId:{} topicId:{} jsonStr:{}", groupId, topicId, jsonStr);
AnswerRes answerRes = JSON.parseObject(jsonStr, AnswerRes.class);
return answerRes.isSucceeded();
} else {
throw new RuntimeException("answer Err Code is " + response.getStatusLine().getStatusCode());
}
}
}
......@@ -72,6 +72,12 @@
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</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
server:
port: 8090
\ No newline at end of file
port: 8090
chatbot-api:
groupId: 28885518425541
cookie: __cuid=5330a556392a4c5b8084b4cbc165e0f3; amp_fef1e8=930aec23-e22e-4f11-8864-0389bd5095d1R...1g55hl79m.1g55hl79t.1.1.2; UM_distinctid=183e61195d535b-0bddac94679c75-19525635-1aeaa0-183e61195d7c52; sensorsdata2015jssdkcross={"distinct_id":"241858242255511","first_id":"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7","props":{"$latest_traffic_source_type":"直接流量","$latest_search_keyword":"未取到值_直接打开","$latest_referrer":""},"$device_id":"17ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7","identities":"eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTgwMmQ2YjZiOWIxZjMtMGQ4YzMzZjhmYTA3YmEtMzU3MzZhMDMtMTI5NjAwMC0xODAyZDZiNmI5YzEwODYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIyNDE4NTgyNDIyNTU1MTEifQ==","history_login_id":{"name":"$identity_login_id","value":"241858242255511"}}; abtest_env=product; zsxqsessionid=8fae9a083a4874ab833c2158a44deb82; zsxq_access_token=5D862869-1229-A9B6-1BC1-C662EC4B16DD_D625BA7FD9CBBDFA
\ No newline at end of file
package cn.bugstack.chatbot.api.test;
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.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
/**
* @author 小傅哥,微信:fustack
* @description
* @github https://github.com/fuzhengwei
* @Copyright 公众号:bugstack虫洞栈 | 博客:https://bugstack.cn - 沉淀、分享、成长,让自己和他人都能有所收获!
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRunTest {
private Logger logger = LoggerFactory.getLogger(SpringBootRunTest.class);
@Value("${chatbot-api.groupId}")
private String groupId;
@Value("${chatbot-api.cookie}")
private String cookie;
@Resource
private IZsxqApi zsxqApi;
@Test
public void test_zsxqApi() throws IOException {
UnAnsweredQuestionsAggregates unAnsweredQuestionsAggregates = zsxqApi.queryUnAnsweredQuestionsTopicId(groupId, cookie);
logger.info("测试结果:{}", JSON.toJSONString(unAnsweredQuestionsAggregates));
List<Topics> topics = unAnsweredQuestionsAggregates.getResp_data().getTopics();
for (Topics topic : topics) {
String topicId = topic.getTopic_id();
String text = topic.getQuestion().getText();
logger.info("topicId:{} text:{}", topicId, text);
// 回答问题
zsxqApi.answer(groupId, cookie, topicId, text, false);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册