Skip to content

Commit 0be56e5

Browse files
committed
Merge branch Pull Request zhayujie#882 into master
2 parents abcbb34 + 6a13dd0 commit 0be56e5

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ plugins/**/
2222
!plugins/banwords
2323
!plugins/banwords/**/
2424
!plugins/hello
25-
!plugins/role
25+
!plugins/role
26+
!plugins/keyword

plugins/keyword/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 目的
2+
关键字匹配并回复
3+
4+
# 试用场景
5+
目前是在微信公众号下面使用过。
6+
7+
# 使用步骤
8+
1. 复制 `config.json.template``config.json`
9+
2. 在关键字 `keyword` 新增需要关键字匹配的内容
10+
3. 重启程序做验证
11+
12+
# 验证结果
13+
![结果](test-keyword.png)

plugins/keyword/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .keyword import *

plugins/keyword/config.json.template

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"keyword": {
3+
"关键字匹配": "测试成功"
4+
}
5+
}

plugins/keyword/keyword.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# encoding:utf-8
2+
3+
import json
4+
import os
5+
6+
import plugins
7+
from bridge.context import ContextType
8+
from bridge.reply import Reply, ReplyType
9+
from common.log import logger
10+
from plugins import *
11+
12+
13+
@plugins.register(
14+
name="Keyword",
15+
desire_priority=900,
16+
hidden=True,
17+
desc="关键词匹配过滤",
18+
version="0.1",
19+
author="fengyege.top",
20+
)
21+
class Keyword(Plugin):
22+
def __init__(self):
23+
super().__init__()
24+
try:
25+
curdir = os.path.dirname(__file__)
26+
config_path = os.path.join(curdir, "config.json")
27+
conf = None
28+
if not os.path.exists(config_path):
29+
logger.debug(f"[keyword]不存在配置文件{config_path}")
30+
conf = {"keyword": {}}
31+
with open(config_path, "w", encoding="utf-8") as f:
32+
json.dump(conf, f, indent=4)
33+
else:
34+
logger.debug(f"[keyword]加载配置文件{config_path}")
35+
with open(config_path, "r", encoding="utf-8") as f:
36+
conf = json.load(f)
37+
# 加载关键词
38+
self.keyword = conf["keyword"]
39+
40+
logger.info("[keyword] {}".format(self.keyword))
41+
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
42+
logger.info("[keyword] inited.")
43+
except Exception as e:
44+
logger.warn(
45+
"[keyword] init failed, ignore or see https://github.com/zhayujie/chatgpt-on-wechat/tree/master/plugins/keyword ."
46+
)
47+
raise e
48+
49+
def on_handle_context(self, e_context: EventContext):
50+
if e_context["context"].type != ContextType.TEXT:
51+
return
52+
53+
content = e_context["context"].content.strip()
54+
logger.debug("[keyword] on_handle_context. content: %s" % content)
55+
if content in self.keyword:
56+
logger.debug(f"[keyword] 匹配到关键字【{content}】")
57+
reply_text = self.keyword[content]
58+
59+
reply = Reply()
60+
reply.type = ReplyType.TEXT
61+
reply.content = reply_text
62+
e_context["reply"] = reply
63+
e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
64+
65+
def get_help_text(self, **kwargs):
66+
help_text = "关键词过滤"
67+
return help_text

plugins/keyword/test-keyword.png

12.1 KB
Loading

0 commit comments

Comments
 (0)