diff --git a/Alice/src/main/java/cn/penidea/alice/AliceApplication.java b/Alice/src/main/java/cn/penidea/alice/AliceApplication.java index cce8bdb..b0e1e4f 100644 --- a/Alice/src/main/java/cn/penidea/alice/AliceApplication.java +++ b/Alice/src/main/java/cn/penidea/alice/AliceApplication.java @@ -45,6 +45,10 @@ public class AliceApplication { private boolean isPublic; @Value("${clientBaseConfig.isConcurrency}") private boolean isConcurrency; + @Value("${clientBaseConfig.isAddself}") + private boolean isAddself; + @Value("${clientBaseConfig.isLoading}") + private boolean isLoading; @Value("${executor.corePoolSize}") private Integer corePoolSize; @@ -84,10 +88,12 @@ public BaseConfigBean baseConfig() { loadingWord = props.getStr("loadingWord"); isPublic = props.getBool("isPublic"); isConcurrency = props.getBool("isConcurrency"); + isAddself = props.getBool("isAddself"); + isLoading = props.getBool("isLoading"); } userList.put(adminQQ, ""); userList.put("admin", adminQQ); - return new BaseConfigBean(userList, "[CQ:at,qq=" + robotQQ + "]", wakeUpWord, robotName, standbyWord, promptUpWord, standbyPrompt, loadingWord, isPublic, isConcurrency); + return new BaseConfigBean(userList, "[CQ:at,qq=" + robotQQ + "]", wakeUpWord, robotName, standbyWord, promptUpWord, standbyPrompt, loadingWord, isPublic, isConcurrency, isAddself, isLoading); } @SneakyThrows diff --git a/Alice/src/main/java/cn/penidea/alice/service/impl/ChatGPTServiceImpl.java b/Alice/src/main/java/cn/penidea/alice/service/impl/ChatGPTServiceImpl.java index d6d9d40..505a129 100644 --- a/Alice/src/main/java/cn/penidea/alice/service/impl/ChatGPTServiceImpl.java +++ b/Alice/src/main/java/cn/penidea/alice/service/impl/ChatGPTServiceImpl.java @@ -36,7 +36,7 @@ public String askQuestion(String question) { return message.get("message").toString(); } catch (Exception e) { e.printStackTrace(); - return "Token失效或服务器延迟,如多次尝试询问无果请尝试重新获取cookie"; + return "你这要求有点问题啊,我答不上来"; } } diff --git a/Alice/src/main/java/cn/penidea/alice/util/BaseConfigBean.java b/Alice/src/main/java/cn/penidea/alice/util/BaseConfigBean.java index 711e90a..8c98e8a 100644 --- a/Alice/src/main/java/cn/penidea/alice/util/BaseConfigBean.java +++ b/Alice/src/main/java/cn/penidea/alice/util/BaseConfigBean.java @@ -28,4 +28,6 @@ public class BaseConfigBean { private String loadingWord; private boolean isPublic; private boolean isConcurrency; + private boolean isAddself; + private boolean isLoading; } diff --git a/Alice/src/main/java/cn/penidea/alice/ws/Client.java b/Alice/src/main/java/cn/penidea/alice/ws/Client.java index 3ee7206..dd6c4ee 100644 --- a/Alice/src/main/java/cn/penidea/alice/ws/Client.java +++ b/Alice/src/main/java/cn/penidea/alice/ws/Client.java @@ -147,9 +147,9 @@ public static void filterFunction(Message message) throws IOException { String from = message.getUser_id();//发送方qq String groupId = message.getGroup_id();//群聊号 boolean isAt = false; - if ((baseConfigBean.getUserList().get(from) != null || baseConfigBean.isPublic() || groupList.get(groupId) != null) && (baseConfigBean.getUserList().get("admin").equals(from) || !isAlone)) { - if (msg.contains(baseConfigBean.getAtRobotCQ() + " Say hello")) { - sendMessage(from, groupId, messageType, "大家好!我是" + baseConfigBean.getRobotName(), false); + if ((groupList.get(groupId) != null && (baseConfigBean.getUserList().get(from) != null || baseConfigBean.isPublic())) || baseConfigBean.getUserList().get("admin").equals(from)) { + if (msg.contains(baseConfigBean.getAtRobotCQ() + " 问个好")) { + sendMessage(from, groupId, messageType, "我可是" + baseConfigBean.getRobotName() + ",随随便便就想让我给大家问好?", false); return; } else if ((MASTER == null || baseConfigBean.getUserList().get("admin").equals(from)) && !baseConfigBean.isConcurrency()) { if ((msg.equals(baseConfigBean.getWakeUpWord()) || msg.startsWith(baseConfigBean.getAtRobotCQ()))) { @@ -157,13 +157,11 @@ public static void filterFunction(Message message) throws IOException { if (msg.equals(baseConfigBean.getAtRobotCQ()) || msg.equals(baseConfigBean.getAtRobotCQ() + " ") || msg.equals(baseConfigBean.getWakeUpWord())) { sendMessage(from, groupId, messageType, baseConfigBean.getPromptUpWord(), false); } - } else if (msg.equals(baseConfigBean.getStandbyWord())) { - MASTER = null; - sendMessage(from, groupId, messageType, baseConfigBean.getStandbyPrompt(), false); } } - if ((MASTER != null && from.equals(MASTER)) || baseConfigBean.isConcurrency()) { + if (MASTER != null) { + if (from.equals(MASTER) || baseConfigBean.isConcurrency()) { if (msg.startsWith(baseConfigBean.getAtRobotCQ()) && !msg.equals(baseConfigBean.getAtRobotCQ()) && !msg.equals(baseConfigBean.getAtRobotCQ() + " ")) { if (msg.equals(baseConfigBean.getAtRobotCQ() + " ")) { msg = msg.substring(msg.indexOf("]") + 3); @@ -172,103 +170,155 @@ public static void filterFunction(Message message) throws IOException { } isAt = true; } + if (msg.equals(baseConfigBean.getStandbyWord())) { + MASTER = null; + sendMessage(from, groupId, messageType, baseConfigBean.getStandbyPrompt(), false); + return; + } if (baseConfigBean.getUserList().get("admin").equals(from)) { - if (msg.startsWith("add [CQ:at,qq=") && !baseConfigBean.isPublic()) { - String qq = msg.substring(msg.indexOf("add [CQ:at,qq=") + 14, msg.indexOf("]")); + if (msg.startsWith("也和这位聊吧[CQ:at,qq=")) { + if (!baseConfigBean.isPublic()) { + String qq = msg.substring(msg.indexOf("也和这位聊吧[CQ:at,qq=") + 16, msg.indexOf("]")); baseConfigBean.getUserList().put(qq, ""); - sendMessage(from, groupId, messageType, "添加主人" + qq + "成功!", false); + sendMessage(from, groupId, messageType, "要让我和[CQ:at,qq=" + qq + "] 聊啊,行吧!", false); + } else { + sendMessage(from, groupId, messageType, "我现在已经可以和大家都聊天了啊", false); + } + if (!isAlone) { MASTER = null; + } return; - } else if (msg.startsWith("del [CQ:at,qq=") && !baseConfigBean.isPublic()) { - String qq = msg.substring(msg.indexOf("del [CQ:at,qq=") + 14, msg.indexOf("]")); + } else if (msg.startsWith("別理[CQ:at,qq=")) { + if (!baseConfigBean.isPublic()) { + String qq = msg.substring(msg.indexOf("别理[CQ:at,qq=") + 12, msg.indexOf("]")); if (baseConfigBean.getUserList().get(qq) != null) { baseConfigBean.getUserList().remove(qq); - sendMessage(from, groupId, messageType, "移除主人" + qq + "成功!", false); + sendMessage(from, groupId, messageType, "好吧,那我再也不理[CQ:at,qq=" + qq + "] 了", false); } else { - sendMessage(from, groupId, messageType, "移除失败,主人列表里并没有他", false); + sendMessage(from, groupId, messageType, "哼哼,我早就不理他了", false); } + } else { + sendMessage(from, groupId, messageType, "你不是让我和大家都聊天吗?", false); + } + if (!isAlone) { MASTER = null; + } return; - } else if (msg.equals("#public")) { - isAlone = false; + } else if (msg.equals("#设为公有")) { threadPoolMember = new ConcurrentHashMap<>(); baseConfigBean.setPublic(true); - sendMessage(from, groupId, messageType, "机器人已设为公有化", false); + sendMessage(from, groupId, messageType, "想让我和大家都聊天?好吧...", false); + if (!isAlone) { MASTER = null; + } return; - } else if (msg.equals("#private")) { - isAlone = false; + } else if (msg.equals("#设为私有")) { threadPoolMember = new ConcurrentHashMap<>(); baseConfigBean.setPublic(false); - sendMessage(from, groupId, messageType, "机器人已设为私有化", false); + sendMessage(from, groupId, messageType, "哼哼,我现在只和我认识的人聊天了", false); + if (!isAlone) { MASTER = null; + } return; - } else if (msg.equals("#reset")) { + } else if (msg.equals("#重置所有会话")) { if (baseConfigBean.isConcurrency() && isAlone) { threadMap = ExpiringMap.builder(). expiration(1000 * 60 * 20, TimeUnit.MILLISECONDS). expirationPolicy(ExpirationPolicy.CREATED). build(); threadPoolMember = new ConcurrentHashMap<>(); - sendMessage(from, groupId, messageType, "所有会话已重置", false); + sendMessage(from, groupId, messageType, "啊啊啊,我聊天的记忆,全部消失了...", false); return; } chatGPTService.reset(); - sendMessage(from, groupId, messageType, "会话已重置", false); - MASTER = null; + sendMessage(from, groupId, messageType, "啊啊啊,我和你聊天的记忆,正在消失...", false); + if (!isAlone) { + MASTER = null; + } return; - } else if (msg.equals("#reset me") && baseConfigBean.isConcurrency()) { + } else if (msg.equals("#重置我的会话") && baseConfigBean.isConcurrency()) { threadMap.remove(from); - sendMessage(from, groupId, messageType, "会话已重置", false); + sendMessage(from, groupId, messageType, "啊啊啊,我和你聊天的记忆,消失了...", false); return; - } else if (msg.equals("#more")) { - MASTER = null; + } else if (msg.equals("#开启多线程")) { + if (!isAlone) { + MASTER = null; + } baseConfigBean.setConcurrency(true); - sendMessage(from, groupId, messageType, "已开启多线程", false); + sendMessage(from, groupId, messageType, "哈?要让我同时和每个人聊天?也不是不行", false); return; - } else if (msg.equals("#less")) { + } else if (msg.equals("#开启单线程")) { baseConfigBean.setConcurrency(false); threadPoolMember = new ConcurrentHashMap<>(); threadMap = ExpiringMap.builder(). expiration(1000 * 60 * 20, TimeUnit.MILLISECONDS). expirationPolicy(ExpirationPolicy.CREATED). build(); - sendMessage(from, groupId, messageType, "已切换单人问答", false); + sendMessage(from, groupId, messageType, "同时和那么多人聊天即使对于我" + baseConfigBean.getRobotName() + "来说还是有点困难啊", false); return; - } else if (msg.equals("#alone")) { + } else if (msg.equals("#开启一问到底")) { MASTER = from; isAlone = true; threadPoolMember = new ConcurrentHashMap<>(); - sendMessage(from, groupId, messageType, "一问到底模式已开启", false); + sendMessage(from, groupId, messageType, "真懒,连每次@我都懒得@", false); return; - } else if (msg.equals("#add this")) { + } else if (msg.equals("#在本群开机")) { groupList.put(groupId, ""); - sendMessage(from, groupId, messageType, "已将本群纳入权限列表", false); + sendMessage(from, groupId, messageType, "哼哈哈,本王来了!", false); return; - } else if (msg.equals("#del this")) { + } else if (msg.equals("#在本群关机")) { groupList.remove(groupId); - sendMessage(from, groupId, messageType, "已将本群从权限列表中移除", false); + sendMessage(from, groupId, messageType, "走了走了", false); + return; + } else if (msg.equals("#开关自添加")) { + if (!baseConfigBean.isPublic()) { + if (baseConfigBean.isAddself()) { + baseConfigBean.setAddself(false); + sendMessage(from, groupId, messageType, "哼哼,接下来就算你求我,我也不会愿意和你讲话了", false); + } else { + baseConfigBean.setAddself(true); + sendMessage(from, groupId, messageType, "想和我聊天?@我+“也和我聊聊呗”,我会考虑考虑的,哈哈", false); + } + } else { + sendMessage(from, groupId, messageType, "我现在愿意和大家都聊天,没必要开关这个", false); + } + if (!isAlone) { + MASTER = null; + } return; - } else if (msg.equals("#help")) { - String commands = "add @xxx 给某个qq添加权限\n----------\n" + - "del @xxx 删除某个qq的权限\n----------\n" + - "#public 将设置为所有人可用\n----------\n" + - "#private 将设为权限列表内可用\n----------\n" + - "#reset 在多线程模式下清除所有会话,单线程模式下清除会话\n----------\n" + - "#reset me 用于多线程时清除管理员自己的会话\n----------\n" + - "#more 开启多线程模式\n----------\n" + - "#less 关闭多线程模式\n----------\n" + - "#alone 开启一问到底(既无需唤醒机器人)\n----------\n" + - "#add this 将当前的群聊加入权限列表\n----------\n" + - "#del this 将当前的群聊从权限列表中移除"; + } else if (msg.equals("#开关等待词")) { + if (baseConfigBean.isLoading()) { + baseConfigBean.setLoading(false); + sendMessage(from, groupId, messageType, "哈?不想让我说 " + baseConfigBean.getLoadingWord() + " 了?唉,行吧", false); + } else { + baseConfigBean.setLoading(true); + sendMessage(from, groupId, messageType, "嘿嘿,那你每次问我问题我都要说 " + baseConfigBean.getLoadingWord(), false); + } + return; + } else if (msg.equals("#帮助")) { + String commands = "也和这位聊吧@xxx 让我愿意和他聊天\n----------\n" + + "别理@xxx 让我不理他\n----------\n" + + "#设为公有 让我愿意和群里所有人聊天\n----------\n" + + "#设为私有 让我只想和愿意的人聊天\n----------\n" + + "#重置所有会话 别想清除我的所有聊天记忆\n----------\n" + + "#重置我的会话 别想清除我和你的聊天记忆\n----------\n" + + "#开启多线程 让我同时和许多人聊天\n----------\n" + + "#开启单线程 让我一次只和一个人聊天\n----------\n" + + "#开启一问到底 不想叫本王又想和我聊天,真懒\n----------\n" + + "#在本群开机 让我愿意在这个群聊天\n----------\n" + + "#在本群关机 让我不愿意在这个群聊天\n----------\n" + + "#开关自添加 让我接受或不接受别人把他自己添加到我的聊天对象列表中\n----------\n" + + "#开关等待词 不想听“" + baseConfigBean.getLoadingWord() + "”了是吧"; sendMessage(from, groupId, messageType, commands, false); } } if (!msg.equals(baseConfigBean.getAtRobotCQ()) && !msg.equals(baseConfigBean.getAtRobotCQ() + " ") && !msg.equals(baseConfigBean.getWakeUpWord())) { if (baseConfigBean.isConcurrency() && isAt && threadPoolMember.get(from) == null && !isAlone) { - sendMessage(from, groupId, messageType, baseConfigBean.getLoadingWord(), false); - String conversationId = null; + if (baseConfigBean.isLoading()) { + sendMessage(from, groupId, messageType, baseConfigBean.getLoadingWord(), false); + } + String conversationId = null; String parentId = UUID.randomUUID().toString(); Map map = threadMap.get(from); if (map != null) { @@ -282,7 +332,9 @@ public static void filterFunction(Message message) throws IOException { try { if (MASTER != null && threadPoolMember.get(from) == null) { threadPoolMember.put(from, 1); + if (baseConfigBean.isLoading()) { sendMessage(from, groupId, messageType, baseConfigBean.getLoadingWord(), false); + } String answer = chatGPTService.askQuestion(msg); answer = answer.replace("Assistant", baseConfigBean.getRobotName()); sendMessage(from, groupId, messageType, answer, false); @@ -294,8 +346,26 @@ public static void filterFunction(Message message) throws IOException { chatGPTService.refresh(); } } - - } + } + } else { + if ((msg.equals(baseConfigBean.getWakeUpWord()) || msg.startsWith(baseConfigBean.getAtRobotCQ()))) { + sendMessage(from, groupId, messageType, "别吵吵,我还在和[CQ:at,qq=" + MASTER + "] 聊天呢,等下哈", false); + } + return; + } + } else if (groupList.get(groupId) != null && !baseConfigBean.isPublic() && msg.contains(baseConfigBean.getAtRobotCQ() + " 也和我聊聊呗")){ + if (baseConfigBean.isAddself()) { + baseConfigBean.getUserList().put(from, ""); + if (from.equals(MASTER)) { + sendMessage(from, groupId, messageType, "行行行,我也可以和你聊天", false); + } else { + sendMessage(from, groupId, messageType, "行,等我和[CQ:at,qq=" + MASTER + "] 聊完再跟你聊天", false); + } + return; + } else { + sendMessage(from, groupId, messageType, "就你还想和本大王聊天?", false); + return; + } } } diff --git a/Alice/src/main/resources/application.yml b/Alice/src/main/resources/application.yml index f43be37..6a93ec7 100644 --- a/Alice/src/main/resources/application.yml +++ b/Alice/src/main/resources/application.yml @@ -13,6 +13,8 @@ clientBaseConfig: loadingWord: 正在检索... isPublic: false isConcurrency: false + isAddself: false + isLoading: true #线程池配置 executor: diff --git a/Alice/src/main/resources/static/config.properties b/Alice/src/main/resources/static/config.properties index d4e80e0..0cb5874 100644 --- a/Alice/src/main/resources/static/config.properties +++ b/Alice/src/main/resources/static/config.properties @@ -24,9 +24,13 @@ loadingWord=正在检索... isPublic=false #是否开启多人同时提问 isConcurrency=false +#是否允许私有状态下用户列表外的用户把自己添加到用户列表 +isAddself=false +#是否打开加载提示词 +isLoading=false #线程初始数量 corePoolSize=5 #最大线程数量 maxPoolSize=20 #最大队列长度 -workQueue=25 \ No newline at end of file +workQueue=25 diff --git a/README.md b/README.md index 538888c..8d09980 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ AliceBot =========================== ###### 环境依赖 -* ubuntu 18.04 +* ubuntu 18.04/22.04 * jdk8 * go-cqhttp_linux_arm64 @@ -15,28 +15,30 @@ AliceBot 下载到服务器后使用tar -zxvf进行解压
运行其中的的在解压目录下运行screen -S gocq
再运行./go-cqhttp,第一次运行会先让你选连接方式,我们选择2:正向Websocket,gocq在解压目录下生成config.yml,这时我们先Ctrl+C
- 运行nano config.yml,拉到最下面找到address: 0.0.0.0:8080,把8080替换成9099,然后Ctrl+X,然后按Y,再回车一下保存
+ 运行nano config.yml,拉到最下面找到address: 0.0.0.0:8080,把0.0.0.0:8080替换成192.168.0.102:9099,然后Ctrl+X,然后按Y,再回车一下保存
然后再次运行./go-cqhttp,完成登录后按Ctrl+A+D放到后台运行
2. 执行jar包 先输入screen -S Alice 创建个会话
- 然后使用java -jar -Xmx512m --clientBaseConfig.admin=管理员QQ --clientBaseConfig.robot=机器人QQ --clientBaseConfig.wakeUpWord=唤醒词 --clientBaseConfig.standbyWord=待机词 --clientBaseConfig.promptUpWord=提示词 --clientBaseConfig.robotName=机器人名称 --chatGPT.email=gpt邮箱 --chatGPT.password=gpt密码 --chatGPT.sessionToken=token --server.port=8080
- 运行后再使用Ctrl+A+D放到后台运行即可 + 然后使用java -jar <此处包含两边括号替换为jar包名称,包含.jar> -Xmx512m
+ 运行后等待生成config.properties文件,编辑该文件后再使用上面的指令,之后再使用Ctrl+A+D放到后台运行即可 ###### 机器人的所有操作指令(以下指令皆需唤醒机器人后再进行发送才能生效)
-#private 将机器人设为私有的,需要权限(默认就是私有)
-#public 将机器人设为所有人可用
-add @某人 添加一个拥有权限的使用者
-del @某人 将某人从使用者列表移除
-#reset 在多线程模式下清除所有会话,单线程模式下清除会话
-#reset me 用于多线程时清除管理员自己的会话
-#more 开启多线程模式
-#less 关闭多线程模式
-#alone 开启一问到底(既无需唤醒机器人)
-#add this 将当前的群聊加入权限列表
-#del this 将当前的群聊从权限列表中移除
-#help 查看指令列表
+#设为私有 将机器人设为私有的,需要权限(默认就是私有)
+#设为公有 将机器人设为所有人可用
+也和这位聊吧@某人 添加一个拥有权限的使用者
+别理@某人 将某人从使用者列表移除
+#重置所有会话 在多线程模式下清除所有会话,单线程模式下清除会话
+#重置我的会话 用于多线程时清除管理员自己的会话
+#开启多线程 开启多线程模式
+#开启单线程 关闭多线程模式
+#开启一问到底 开启一问到底(既无需唤醒机器人)
+#在本群开机 将当前的群聊加入权限列表
+#在本群关机 将当前的群聊从权限列表中移除
+#帮助 查看指令列表
+#开关自添加 机器人为私有时,让机器人接受或不接受别人把他自己添加到机器人的聊天对象列表中
+#开关等待词 开关等待词
-###### V3.0.0 版本内容更新 +###### V0.0.3 版本内容更新 1. 新功能 多线程
2. 新功能 一问到底
3. 新功能 群聊权限