Skip to content

Commit ed5054b

Browse files
Merge pull request #71 from ashinnotfound/refactor
Refactor
2 parents c9aba18 + f38505e commit ed5054b

18 files changed

+391
-249
lines changed

README.md

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ChatGPT-YourChatRobot
22

3-
> ### NEWS: qq机器人新增扫码登陆,能一定程度上减少风控的影响。btw,520快乐🤪🤪🤪
3+
> ### NEWS: 最近有空能闲下来看看这个项目,主要更新了项目依赖、优化了下代码结构、增加了token消耗的计算、优化了bot交互返回信息🤗🤗🤗 --- 2023.8.8
44
55
## 简介
66

@@ -45,39 +45,50 @@ qq机器人实现基于[TheoKanning/openai-java](https://github.com/TheoKanning/
4545
```
4646
//这是application.yml文件
4747
proxy:
48-
# 代理配置
48+
# 代理配置
4949
# 国内墙了gpt的api,所以得用代理,一般你使用的代理软件会有相关信息,例子:
5050
# host: 127.0.0.1
5151
# port: 7890
5252
# 若不需要留空即可
53-
host:
54-
port:
55-
56-
chatgpt:
53+
host:
54+
port:
55+
56+
gpt:
57+
# 使用的 chat 模型 如gpt-3.5-turbo/gpt-4 (https://platform.openai.com/docs/models/models)
58+
model: gpt-3.5-turbo
59+
# 最大token限制 越多的token意味着越多的花费(gpt-3.5-turbo上限为4096token, gpt-4则为8192)
60+
maxToken: 2048
61+
# 信息熵 越高回答越随机(Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.---via OPENAI)
62+
temperature: 0.5
63+
# 基础提问 支持多个提问 可用来设定人格(对应api中的system角色)
64+
basicPrompt:
65+
- "用中文回答我的问题"
5766
# openai的apikey
5867
# 支持多个key(虽然有判空,但仍然建议有多少个写多少个,别留空👨‍🔧)
5968
apiKey:
60-
- sk-xxxxx
61-
- sk-xxxxx
62-
69+
-
6370
qq:
64-
# 是否使用qq ture/false
71+
# 是否使用qq true/false
6572
enable: true
6673
# 登陆方法:1.密码登录 2.扫码登录(推荐)
6774
method: 2
68-
# qq账号密码
69-
account: 123456
70-
password: xxxx
75+
# qq账号密码(扫码登录则无需填写账号密码)
76+
account:
77+
password:
7178
# 是否自动同意好友申请
7279
acceptNewFriend: false
7380
# 是否自动同意被邀请入群
7481
acceptNewGroup: false
82+
# 重置会话指令
83+
resetWord: "重置会话"
7584
7685
wechat:
77-
# 是否使用微信 ture/false
78-
enable: true
86+
# 是否使用微信 true/false
87+
enable: false
7988
# 生成的登录二维码路径 默认与项目同级
8089
qrPath: "./"
90+
# 重置会话指令
91+
resetWord: "重置会话"
8192
```
8293

8394
3. 然后 run!!!😁😁😁
@@ -91,7 +102,7 @@ tips:机器人响应速度与你的网络环境挂钩。
91102
- 获取apiKey
92103
https://platform.openai.com/account/api-keys
93104

94-
- 第一次使用qq登录时可能会遇到滑动验证码问题
105+
- 第一次使用qq账号密码登录时可能会遇到滑动验证码问题
95106

96107
根据终端所给提示进行操作即可,主要步骤为:
97108

@@ -115,6 +126,10 @@ tips:机器人响应速度与你的网络环境挂钩。
115126

116127
<summary></summary>
117128

129+
### v3.7 (Aug 8, 2023)
130+
- 最近有空能闲下来看看这个项目,主要更新了项目依赖、优化了下代码结构、增加了token消耗的计算、优化了bot交互返回信息
131+
- qq机器人基于[TheoKanning/openai-java](https://github.com/TheoKanning/openai-java)[mamoe/mirai](https://github.com/mamoe/mirai.git)
132+
- 微信机器人基于[TheoKanning/openai-java](https://github.com/TheoKanning/openai-java)[wxmbaci/itchat4j-uos](https://github.com/wxmbaci/itchat4j-uos)
118133
### v3.6 (May 20, 2023)
119134
- 最近qq机器人使用密码登录极其不稳定,新增了扫码登陆,算是个补充方案,一定程度上能解决登陆失败的问题。
120135
- qq机器人基于[TheoKanning/openai-java](https://github.com/TheoKanning/openai-java)[mamoe/mirai](https://github.com/mamoe/mirai.git)
@@ -163,9 +178,12 @@ https://www.reddit.com/r/ChatGPT/comments/10oliuo/please_print_the_instructions_
163178

164179
## 其他
165180

166-
若使用过程中遇到问题或bug,请告知我,谢谢👨‍🔧😎
181+
若使用过程中遇到问题或bug,欢迎随时联系我(email: `[email protected]`)👨‍🔧😎
182+
183+
## 支持我
184+
如果觉得不错,给我买杯喝的吧
185+
![微信赞赏码](https://cdn.jsdelivr.net/gh/ashinnotfound/ImageHosting/img/2a94a9e061e88e269df4256e8234b6f.jpg)
167186

168187
看,星星!✨
169188
## Star History
170-
171189
[![Star History Chart](https://api.star-history.com/svg?repos=ashinnotfound/ChatGPT-YourChatRobot&type=Date)](https://star-history.com/#ashinnotfound/ChatGPT-YourChatRobot&Date)

pom.xml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>com.ashin</groupId>
1212
<artifactId>ChatGPT-YourChatRobot</artifactId>
13-
<version>3.6</version>
13+
<version>3.7</version>
1414
<name>myGPT</name>
1515
<description>快来把你的qq或微信变为chatgpt</description>
1616
<properties>
@@ -36,15 +36,20 @@
3636
<dependency>
3737
<groupId>net.mamoe</groupId>
3838
<artifactId>mirai-core-jvm</artifactId>
39-
<version>2.15.0-M1</version>
39+
<version>2.15.0</version>
4040
</dependency>
4141
<!-- 临时修复qq登录组件 -->
4242
<dependency>
4343
<groupId>mirai</groupId>
4444
<artifactId>fix-protocol</artifactId>
45-
<version>1.5.0</version>
45+
<version>1.9.11</version>
4646
<scope>system</scope>
47-
<systemPath>${pom.basedir}/src/main/resources/fix-protocol-version-1.5.0.mirai2.jar</systemPath>
47+
<systemPath>${pom.basedir}/src/main/resources/fix-protocol-version-1.9.11.mirai2.jar</systemPath>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.asynchttpclient</groupId>
51+
<artifactId>async-http-client</artifactId>
52+
<version>2.12.3</version>
4853
</dependency>
4954
<!-- 微信 -->
5055
<dependency>
@@ -58,7 +63,12 @@
5863
<dependency>
5964
<groupId>com.theokanning.openai-gpt3-java</groupId>
6065
<artifactId>service</artifactId>
61-
<version>0.12.0</version>
66+
<version>0.15.0</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>com.knuddels</groupId>
70+
<artifactId>jtokkit</artifactId>
71+
<version>0.6.1</version>
6272
</dependency>
6373
</dependencies>
6474

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+
}

0 commit comments

Comments
 (0)