mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 13:28:11 +08:00
Merge branch Pull Request #882 into master
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -22,4 +22,5 @@ plugins/**/
|
|||||||
!plugins/banwords
|
!plugins/banwords
|
||||||
!plugins/banwords/**/
|
!plugins/banwords/**/
|
||||||
!plugins/hello
|
!plugins/hello
|
||||||
!plugins/role
|
!plugins/role
|
||||||
|
!plugins/keyword
|
||||||
13
plugins/keyword/README.md
Normal file
13
plugins/keyword/README.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# 目的
|
||||||
|
关键字匹配并回复
|
||||||
|
|
||||||
|
# 试用场景
|
||||||
|
目前是在微信公众号下面使用过。
|
||||||
|
|
||||||
|
# 使用步骤
|
||||||
|
1. 复制 `config.json.template` 为 `config.json`
|
||||||
|
2. 在关键字 `keyword` 新增需要关键字匹配的内容
|
||||||
|
3. 重启程序做验证
|
||||||
|
|
||||||
|
# 验证结果
|
||||||
|

|
||||||
1
plugins/keyword/__init__.py
Normal file
1
plugins/keyword/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from .keyword import *
|
||||||
5
plugins/keyword/config.json.template
Normal file
5
plugins/keyword/config.json.template
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"keyword": {
|
||||||
|
"关键字匹配": "测试成功"
|
||||||
|
}
|
||||||
|
}
|
||||||
67
plugins/keyword/keyword.py
Normal file
67
plugins/keyword/keyword.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# encoding:utf-8
|
||||||
|
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import plugins
|
||||||
|
from bridge.context import ContextType
|
||||||
|
from bridge.reply import Reply, ReplyType
|
||||||
|
from common.log import logger
|
||||||
|
from plugins import *
|
||||||
|
|
||||||
|
|
||||||
|
@plugins.register(
|
||||||
|
name="Keyword",
|
||||||
|
desire_priority=900,
|
||||||
|
hidden=True,
|
||||||
|
desc="关键词匹配过滤",
|
||||||
|
version="0.1",
|
||||||
|
author="fengyege.top",
|
||||||
|
)
|
||||||
|
class Keyword(Plugin):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
try:
|
||||||
|
curdir = os.path.dirname(__file__)
|
||||||
|
config_path = os.path.join(curdir, "config.json")
|
||||||
|
conf = None
|
||||||
|
if not os.path.exists(config_path):
|
||||||
|
logger.debug(f"[keyword]不存在配置文件{config_path}")
|
||||||
|
conf = {"keyword": {}}
|
||||||
|
with open(config_path, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(conf, f, indent=4)
|
||||||
|
else:
|
||||||
|
logger.debug(f"[keyword]加载配置文件{config_path}")
|
||||||
|
with open(config_path, "r", encoding="utf-8") as f:
|
||||||
|
conf = json.load(f)
|
||||||
|
# 加载关键词
|
||||||
|
self.keyword = conf["keyword"]
|
||||||
|
|
||||||
|
logger.info("[keyword] {}".format(self.keyword))
|
||||||
|
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
|
||||||
|
logger.info("[keyword] inited.")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warn(
|
||||||
|
"[keyword] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/keyword ."
|
||||||
|
)
|
||||||
|
raise e
|
||||||
|
|
||||||
|
def on_handle_context(self, e_context: EventContext):
|
||||||
|
if e_context["context"].type != ContextType.TEXT:
|
||||||
|
return
|
||||||
|
|
||||||
|
content = e_context["context"].content.strip()
|
||||||
|
logger.debug("[keyword] on_handle_context. content: %s" % content)
|
||||||
|
if content in self.keyword:
|
||||||
|
logger.debug(f"[keyword] 匹配到关键字【{content}】")
|
||||||
|
reply_text = self.keyword[content]
|
||||||
|
|
||||||
|
reply = Reply()
|
||||||
|
reply.type = ReplyType.TEXT
|
||||||
|
reply.content = reply_text
|
||||||
|
e_context["reply"] = reply
|
||||||
|
e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
|
||||||
|
|
||||||
|
def get_help_text(self, **kwargs):
|
||||||
|
help_text = "关键词过滤"
|
||||||
|
return help_text
|
||||||
BIN
plugins/keyword/test-keyword.png
Normal file
BIN
plugins/keyword/test-keyword.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Reference in New Issue
Block a user