80 lignes
3.2KB

  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. @plugins.register(
  9. name="Hello",
  10. desire_priority=-1,
  11. hidden=True,
  12. desc="A simple plugin that says hello",
  13. version="0.1",
  14. author="lanvent",
  15. )
  16. class Hello(Plugin):
  17. def __init__(self):
  18. super().__init__()
  19. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  20. logger.info("[Hello] inited")
  21. def on_handle_context(self, e_context: EventContext):
  22. if e_context["context"].type not in [
  23. ContextType.TEXT,
  24. ContextType.JOIN_GROUP,
  25. ContextType.PATPAT,
  26. ]:
  27. return
  28. if e_context["context"].type == ContextType.JOIN_GROUP:
  29. e_context["context"].type = ContextType.TEXT
  30. msg: ChatMessage = e_context["context"]["msg"]
  31. e_context[
  32. "context"
  33. ].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。'
  34. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
  35. return
  36. if e_context["context"].type == ContextType.PATPAT:
  37. e_context["context"].type = ContextType.TEXT
  38. msg: ChatMessage = e_context["context"]["msg"]
  39. e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。"
  40. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
  41. return
  42. content = e_context["context"].content
  43. logger.debug("[Hello] on_handle_context. content: %s" % content)
  44. if content == "Hello":
  45. reply = Reply()
  46. reply.type = ReplyType.TEXT
  47. msg: ChatMessage = e_context["context"]["msg"]
  48. if e_context["context"]["isgroup"]:
  49. reply.content = (
  50. f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}"
  51. )
  52. else:
  53. reply.content = f"Hello, {msg.from_user_nickname}"
  54. e_context["reply"] = reply
  55. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  56. if content == "Hi":
  57. reply = Reply()
  58. reply.type = ReplyType.TEXT
  59. reply.content = "Hi"
  60. e_context["reply"] = reply
  61. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑,一般会覆写reply
  62. if content == "End":
  63. # 如果是文本消息"End",将请求转换成"IMAGE_CREATE",并将content设置为"The World"
  64. e_context["context"].type = ContextType.IMAGE_CREATE
  65. content = "The World"
  66. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
  67. def get_help_text(self, **kwargs):
  68. help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n"
  69. return help_text