Skip to content

Commit 774b680

Browse files
committed
improvement:优化代码结构
1 parent 3ee354b commit 774b680

File tree

12 files changed

+258
-226
lines changed

12 files changed

+258
-226
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.ashin.client;
2+
3+
import com.ashin.config.GptConfig;
4+
import com.ashin.config.ProxyConfig;
5+
import com.theokanning.openai.service.OpenAiService;
6+
import lombok.Getter;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.stereotype.Component;
9+
10+
import javax.annotation.PostConstruct;
11+
import javax.annotation.Resource;
12+
import java.time.Duration;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
@Component
17+
@Slf4j
18+
public class GptClient {
19+
@Resource
20+
private ProxyConfig proxyConfig;
21+
@Resource
22+
private GptConfig gptConfig;
23+
24+
// openai服务list
25+
@Getter
26+
private List<OpenAiService> openAiServiceList;
27+
28+
@PostConstruct
29+
public void init() {
30+
//配置代理
31+
if (!proxyConfig.getHost().isEmpty() && !proxyConfig.getPort().isEmpty()) {
32+
System.setProperty("http.proxyHost", proxyConfig.getHost());
33+
System.setProperty("https.proxyHost", proxyConfig.getHost());
34+
System.setProperty("http.proxyPort", proxyConfig.getPort());
35+
System.setProperty("https.proxyPort", proxyConfig.getPort());
36+
log.info("代理设置成功!!! {}:{}", proxyConfig.getHost(), proxyConfig.getPort());
37+
}
38+
openAiServiceList = new ArrayList<>();
39+
for (String apiKey : gptConfig.getApiKey()) {
40+
apiKey = apiKey.trim();
41+
if (!apiKey.isEmpty()) {
42+
openAiServiceList.add(new OpenAiService(apiKey, Duration.ofSeconds(1000)));
43+
log.info("apiKey为 {} 的账号初始化成功", apiKey);
44+
}
45+
}
46+
}
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.ashin.client;
2+
3+
import com.ashin.config.QqConfig;
4+
import com.ashin.handler.QqMessageHandler;
5+
import lombok.Getter;
6+
import lombok.extern.slf4j.Slf4j;
7+
import net.mamoe.mirai.BotFactory;
8+
import net.mamoe.mirai.auth.BotAuthorization;
9+
import net.mamoe.mirai.utils.BotConfiguration;
10+
import org.springframework.stereotype.Component;
11+
import xyz.cssxsh.mirai.tool.FixProtocolVersion;
12+
13+
import javax.annotation.PostConstruct;
14+
import javax.annotation.Resource;
15+
16+
@Component
17+
@Slf4j
18+
public class QqBotClient {
19+
20+
@Resource
21+
private QqMessageHandler qqMessageHandler;
22+
@Resource
23+
private QqConfig qqConfig;
24+
25+
// qq机器人实例
26+
@Getter
27+
private net.mamoe.mirai.Bot qqBot;
28+
29+
@PostConstruct
30+
public void init() {
31+
if (qqConfig.getEnable()) {
32+
//登录 登陆协议有ANDROID_PHONE, ANDROID_PAD, ANDROID_WATCH, IPAD, MACOS
33+
try {
34+
log.info("正在登录qq,请按提示操作:");
35+
if (qqConfig.getMethod() == 1) {
36+
//密码登录
37+
qqBot = BotFactory.INSTANCE.newBot(qqConfig.getAccount(), qqConfig.getPassword().trim(), new BotConfiguration() {{
38+
setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PAD);
39+
}});
40+
//使用临时修复插件
41+
FixProtocolVersion.update();
42+
} else {
43+
//扫码登陆
44+
qqBot = BotFactory.INSTANCE.newBot(qqConfig.getAccount(), BotAuthorization.byQRCode(), configuration -> configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_WATCH));
45+
}
46+
47+
qqBot.login();
48+
log.info("成功登录账号为 {} 的qq, 登陆方式为 {}", qqConfig.getAccount(), qqConfig.getMethod() == 1 ? "密码登录" : "扫码登陆");
49+
//订阅监听事件
50+
qqBot.getEventChannel().registerListenerHost(qqMessageHandler);
51+
} catch (Exception e) {
52+
log.error("登陆失败,qq账号为 {}, 登陆方式为 {} ,原因:{}", qqConfig.getAccount(), qqConfig.getMethod() == 1 ? "密码登录" : "扫码登陆", e.getMessage());
53+
}
54+
}
55+
}
56+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.ashin.client;
2+
3+
import cn.zhouyafeng.itchat4j.Wechat;
4+
import com.ashin.config.WechatConfig;
5+
import com.ashin.handler.WechatMessageHandler;
6+
import lombok.Getter;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.springframework.stereotype.Component;
9+
10+
import javax.annotation.PostConstruct;
11+
import javax.annotation.Resource;
12+
13+
@Component
14+
@Slf4j
15+
public class WechatBotClient {
16+
@Resource
17+
private WechatMessageHandler wechatMessageHandler;
18+
@Resource
19+
private WechatConfig wechatConfig;
20+
21+
@Getter
22+
private Wechat wechatBot;
23+
@PostConstruct
24+
public void init() {
25+
//微信
26+
if (wechatConfig.getEnable()){
27+
log.info("正在登录微信,请按提示操作:");
28+
wechatBot = new Wechat(wechatMessageHandler, wechatConfig.getQrPath());
29+
wechatBot.start();
30+
}
31+
}
32+
}

src/main/java/com/ashin/config/BotConfig.java

Lines changed: 0 additions & 118 deletions
This file was deleted.

src/main/java/com/ashin/config/ChatgptConfig.java renamed to src/main/java/com/ashin/config/GptConfig.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
import java.util.List;
88

99
/**
10-
* chatgpt配置
10+
* gpt配置
1111
*
1212
* @author ashinnotfound
1313
* @date 2023/03/04
1414
*/
1515
@Data
1616
@Component
17-
@ConfigurationProperties("chatgpt")
18-
public class ChatgptConfig {
17+
@ConfigurationProperties("gpt")
18+
public class GptConfig {
19+
private String model;
20+
private Integer maxToken;
21+
private Double temperature;
22+
private List<String> basicPrompt;
1923
private List<String> apiKey;
2024
}

src/main/java/com/ashin/config/QqConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ public class QqConfig {
2020
private String password;
2121
private Boolean acceptNewFriend;
2222
private Boolean acceptNewGroup;
23+
private String resetWord;
2324
}

src/main/java/com/ashin/config/WechatConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
public class WechatConfig {
1717
private Boolean enable;
1818
private String qrPath;
19+
private String resetWord;
1920
}

src/main/java/com/ashin/handler/QqMessageHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public class QqMessageHandler implements ListenerHost {
2929
private InteractService interactService;
3030
@Resource
3131
private QqConfig qqConfig;
32-
33-
private static final String RESET_WORD = "重置会话";
32+
@Resource
33+
private BotUtil botUtil;
3434

3535
/**
3636
* 好友消息事件
@@ -62,9 +62,9 @@ public void onGroupMessageEvent(GroupMessageEvent event){
6262
}
6363
}
6464
private void response(@NotNull MessageEvent event, ChatBO chatBO, String prompt) {
65-
if (RESET_WORD.equals(prompt)) {
65+
if (qqConfig.getResetWord().equals(prompt)) {
6666
//检测到重置会话指令
67-
BotUtil.resetPrompt(chatBO.getSessionId());
67+
botUtil.resetPrompt(chatBO.getSessionId());
6868
event.getSubject().sendMessage("重置会话成功");
6969
} else {
7070
String response;

0 commit comments

Comments
 (0)