Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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