From cc81dd91724761d963a71d6e75e850874cec3270 Mon Sep 17 00:00:00 2001 From: 6vision Date: Sun, 7 Apr 2024 22:31:08 +0800 Subject: [PATCH 1/2] Signed-off-by: 6vision --- plugins/hello/README.md | 41 ++++++++++++++++++++++++++ plugins/hello/config.json.template | 14 +++++++++ plugins/hello/hello.py | 46 ++++++++++++++++++++++-------- 3 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 plugins/hello/README.md create mode 100644 plugins/hello/config.json.template diff --git a/plugins/hello/README.md b/plugins/hello/README.md new file mode 100644 index 0000000..fb560c4 --- /dev/null +++ b/plugins/hello/README.md @@ -0,0 +1,41 @@ +## 插件说明 + +可以根据需求设置入群欢迎、群聊拍一拍、退群等消息的自定义提示词,也支持为每个群设置对应的固定欢迎语。 + +该插件也是用户根据需求开发自定义插件的示例插件,参考[插件开发说明](https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins) + +## 插件配置 + +将 `plugins/hello` 目录下的 `config.json.template` 配置模板复制为最终生效的 `config.json`。 (如果未配置则会默认使用`config.json.template`模板中配置)。 + +以下是插件配置项说明: + +```bash +{ + "group_welc_fixed_msg": { ## 这里可以为特定群里配置特定的固定欢迎语 + "群聊1": "群聊1的固定欢迎语", + "群聊2": "群聊2的固定欢迎语" + }, + + "group_welc_prompt": "请你随机使用一种风格说一句问候语来欢迎新用户\"{nickname}\"加入群聊。", ## 群聊随机欢迎语的提示词 + + "group_exit_prompt": "请你随机使用一种风格跟其他群用户说他违反规则\"{nickname}\"退出群聊。", ## 移出群聊的提示词 + + "patpat_prompt": "请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。", ## 群内拍一拍的提示词 + + "use_character_desc": false ## 是否在Hello插件中使用LinkAI应用的系统设定 +} +``` + + +注意: + + - 设置全局的用户进群固定欢迎语,可以在***项目根目录下***的`config.json`文件里,可以添加参数`"group_welcome_msg": "" `,参考 [#1482](https://github.com/zhayujie/chatgpt-on-wechat/pull/1482) + - 为每个群设置固定的欢迎语,可以在`"group_welc_fixed_msg": {}`配置群聊名和对应的固定欢迎语,优先级高于全局固定欢迎语 + - 如果没有配置以上两个参数,则使用随机欢迎语,如需设定风格,语言等,修改`"group_welc_prompt": `即可 + - 如果使用LinkAI的服务,想在随机欢迎中结合LinkAI应用的设定,配置`"use_character_desc": true ` + - 实际 `config.json` 配置中应保证json格式,不应携带 '#' 及后面的注释 + - 如果是`docker`部署,可通过映射 `plugins/config.json` 到容器中来完成插件配置,参考[文档](https://github.com/zhayujie/chatgpt-on-wechat#3-%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8) + + + diff --git a/plugins/hello/config.json.template b/plugins/hello/config.json.template new file mode 100644 index 0000000..13c3788 --- /dev/null +++ b/plugins/hello/config.json.template @@ -0,0 +1,14 @@ +{ + "group_welc_fixed_msg": { + "群聊1": "群聊1的固定欢迎语", + "群聊2": "群聊2的固定欢迎语" + }, + + "group_welc_prompt": "请你随机使用一种风格说一句问候语来欢迎新用户\"{nickname}\"加入群聊。", + + "group_exit_prompt": "请你随机使用一种风格跟其他群用户说他违反规则\"{nickname}\"退出群聊。", + + "patpat_prompt": "请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。", + + "use_character_desc": false +} \ No newline at end of file diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index e86c609..0309ddd 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -20,9 +20,19 @@ from config import conf class Hello(Plugin): def __init__(self): super().__init__() - self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context - logger.info("[Hello] inited") - self.config = super().load_config() + try: + self.config = super().load_config() + if not self.config: + self.config = self._load_config_template() + self.group_welc_fixed_msg = self.config["group_welc_fixed_msg"] + self.group_welc_prompt = self.config["group_welc_prompt"] + self.group_exit_prompt = self.config["group_exit_prompt"] + self.patpat_prompt = self.config["patpat_prompt"] + logger.info("[Hello] inited") + self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context + except Exception as e: + logger.error(f"[Hello]初始化异常:{e}") + raise "[Hello] init failed, ignore " def on_handle_context(self, e_context: EventContext): if e_context["context"].type not in [ @@ -32,17 +42,21 @@ class Hello(Plugin): ContextType.EXIT_GROUP ]: return + msg: ChatMessage = e_context["context"]["msg"] + group_name = msg.from_user_nickname if e_context["context"].type == ContextType.JOIN_GROUP: - if "group_welcome_msg" in conf(): + if "group_welcome_msg" in conf() or group_name in self.group_welc_fixed_msg: reply = Reply() reply.type = ReplyType.TEXT - reply.content = conf().get("group_welcome_msg", "") + if group_name in self.group_welc_fixed_msg: + reply.content = self.group_welc_fixed_msg.get(group_name, "") + else: + reply.content = conf().get("group_welcome_msg", "") e_context["reply"] = reply e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 return e_context["context"].type = ContextType.TEXT - msg: ChatMessage = e_context["context"]["msg"] - e_context["context"].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。' + e_context["context"].content = self.group_welc_prompt.format(nickname=msg.actual_user_nickname) e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 if not self.config or not self.config.get("use_character_desc"): e_context["context"]["generate_breaked_by"] = EventAction.BREAK @@ -51,8 +65,7 @@ class Hello(Plugin): if e_context["context"].type == ContextType.EXIT_GROUP: if conf().get("group_chat_exit_group"): e_context["context"].type = ContextType.TEXT - msg: ChatMessage = e_context["context"]["msg"] - e_context["context"].content = f'请你随机使用一种风格跟其他群用户说他违反规则"{msg.actual_user_nickname}"退出群聊。' + e_context["context"].content = self.group_exit_prompt.format(nickname=msg.actual_user_nickname) e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 return e_context.action = EventAction.BREAK @@ -60,8 +73,7 @@ class Hello(Plugin): if e_context["context"].type == ContextType.PATPAT: e_context["context"].type = ContextType.TEXT - msg: ChatMessage = e_context["context"]["msg"] - e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。" + e_context["context"].content = self.patpat_prompt e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 if not self.config or not self.config.get("use_character_desc"): e_context["context"]["generate_breaked_by"] = EventAction.BREAK @@ -72,7 +84,6 @@ class Hello(Plugin): if content == "Hello": reply = Reply() reply.type = ReplyType.TEXT - msg: ChatMessage = e_context["context"]["msg"] if e_context["context"]["isgroup"]: reply.content = f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}" else: @@ -96,3 +107,14 @@ class Hello(Plugin): def get_help_text(self, **kwargs): help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n" return help_text + + def _load_config_template(self): + logger.debug("No Hello plugin config.json, use plugins/hello/config.json.template") + try: + plugin_config_path = os.path.join(self.path, "config.json.template") + if os.path.exists(plugin_config_path): + with open(plugin_config_path, "r", encoding="utf-8") as f: + plugin_conf = json.load(f) + return plugin_conf + except Exception as e: + logger.exception(e) \ No newline at end of file From c5b549f450cc3cc5c89f045ca7e46208f02ba665 Mon Sep 17 00:00:00 2001 From: 6vision Date: Mon, 8 Apr 2024 23:06:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96hello=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/hello/hello.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index 0309ddd..23de861 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -17,17 +17,24 @@ from config import conf version="0.1", author="lanvent", ) + + class Hello(Plugin): + + group_welc_prompt = "请你随机使用一种风格说一句问候语来欢迎新用户\"{nickname}\"加入群聊。" + group_exit_prompt = "请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。" + patpat_prompt = "请你随机使用一种风格跟其他群用户说他违反规则\"{nickname}\"退出群聊。" + def __init__(self): super().__init__() try: self.config = super().load_config() if not self.config: self.config = self._load_config_template() - self.group_welc_fixed_msg = self.config["group_welc_fixed_msg"] - self.group_welc_prompt = self.config["group_welc_prompt"] - self.group_exit_prompt = self.config["group_exit_prompt"] - self.patpat_prompt = self.config["patpat_prompt"] + self.group_welc_fixed_msg = self.config.get("group_welc_fixed_msg", {}) + self.group_welc_prompt = self.config.get("group_welc_prompt", self.group_welc_prompt) + self.group_exit_prompt = self.config.get("group_exit_prompt", self.group_exit_prompt) + self.patpat_prompt = self.config.get("patpat_prompt", self.patpat_prompt) logger.info("[Hello] inited") self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context except Exception as e: