You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 11 месеца
преди 1 година
преди 11 месеца
преди 1 година
преди 1 година
преди 11 месеца
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # encoding:utf-8
  2. import plugins
  3. from bridge.context import ContextType
  4. from bridge.reply import Reply, ReplyType
  5. from channel.chat_message import ChatMessage
  6. from common.log import logger
  7. from plugins import *
  8. from config import conf
  9. @plugins.register(
  10. name="Hello",
  11. desire_priority=-1,
  12. hidden=True,
  13. desc="A simple plugin that says hello",
  14. version="0.1",
  15. author="lanvent",
  16. )
  17. class Hello(Plugin):
  18. def __init__(self):
  19. super().__init__()
  20. try:
  21. self.config = super().load_config()
  22. if not self.config:
  23. self.config = self._load_config_template()
  24. self.group_welc_fixed_msg = self.config["group_welc_fixed_msg"]
  25. self.group_welc_prompt = self.config["group_welc_prompt"]
  26. self.group_exit_prompt = self.config["group_exit_prompt"]
  27. self.patpat_prompt = self.config["patpat_prompt"]
  28. logger.info("[Hello] inited")
  29. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  30. except Exception as e:
  31. logger.error(f"[Hello]初始化异常:{e}")
  32. raise "[Hello] init failed, ignore "
  33. def on_handle_context(self, e_context: EventContext):
  34. if e_context["context"].type not in [
  35. ContextType.TEXT,
  36. ContextType.JOIN_GROUP,
  37. ContextType.PATPAT,
  38. ContextType.EXIT_GROUP
  39. ]:
  40. return
  41. msg: ChatMessage = e_context["context"]["msg"]
  42. group_name = msg.from_user_nickname
  43. if e_context["context"].type == ContextType.JOIN_GROUP:
  44. if "group_welcome_msg" in conf() or group_name in self.group_welc_fixed_msg:
  45. reply = Reply()
  46. reply.type = ReplyType.TEXT
  47. if group_name in self.group_welc_fixed_msg:
  48. reply.content = self.group_welc_fixed_msg.get(group_name, "")
  49. else:
  50. reply.content = conf().get("group_welcome_msg", "")
  51. e_context["reply"] = reply
  52. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  53. return
  54. e_context["context"].type = ContextType.TEXT
  55. e_context["context"].content = self.group_welc_prompt.format(nickname=msg.actual_user_nickname)
  56. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  57. if not self.config or not self.config.get("use_character_desc"):
  58. e_context["context"]["generate_breaked_by"] = EventAction.BREAK
  59. return
  60. if e_context["context"].type == ContextType.EXIT_GROUP:
  61. if conf().get("group_chat_exit_group"):
  62. e_context["context"].type = ContextType.TEXT
  63. e_context["context"].content = self.group_exit_prompt.format(nickname=msg.actual_user_nickname)
  64. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  65. return
  66. e_context.action = EventAction.BREAK
  67. return
  68. if e_context["context"].type == ContextType.PATPAT:
  69. e_context["context"].type = ContextType.TEXT
  70. e_context["context"].content = self.patpat_prompt
  71. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  72. if not self.config or not self.config.get("use_character_desc"):
  73. e_context["context"]["generate_breaked_by"] = EventAction.BREAK
  74. return
  75. content = e_context["context"].content
  76. logger.debug("[Hello] on_handle_context. content: %s" % content)
  77. if content == "Hello":
  78. reply = Reply()
  79. reply.type = ReplyType.TEXT
  80. if e_context["context"]["isgroup"]:
  81. reply.content = f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}"
  82. else:
  83. reply.content = f"Hello, {msg.from_user_nickname}"
  84. e_context["reply"] = reply
  85. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  86. if content == "Hi":
  87. reply = Reply()
  88. reply.type = ReplyType.TEXT
  89. reply.content = "Hi"
  90. e_context["reply"] = reply
  91. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑,一般会覆写reply
  92. if content == "End":
  93. # 如果是文本消息"End",将请求转换成"IMAGE_CREATE",并将content设置为"The World"
  94. e_context["context"].type = ContextType.IMAGE_CREATE
  95. content = "The World"
  96. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
  97. def get_help_text(self, **kwargs):
  98. help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n"
  99. return help_text
  100. def _load_config_template(self):
  101. logger.debug("No Hello plugin config.json, use plugins/hello/config.json.template")
  102. try:
  103. plugin_config_path = os.path.join(self.path, "config.json.template")
  104. if os.path.exists(plugin_config_path):
  105. with open(plugin_config_path, "r", encoding="utf-8") as f:
  106. plugin_conf = json.load(f)
  107. return plugin_conf
  108. except Exception as e:
  109. logger.exception(e)