選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

47 行
1.9KB

  1. # encoding:utf-8
  2. from bridge.context import ContextType
  3. from bridge.reply import Reply, ReplyType
  4. import plugins
  5. from plugins import *
  6. from common.log import logger
  7. @plugins.register(name="Hello", desc="A simple plugin that says hello", version="0.1", author="lanvent", desire_priority= -1)
  8. class Hello(Plugin):
  9. def __init__(self):
  10. super().__init__()
  11. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  12. logger.info("[Hello] inited")
  13. def on_handle_context(self, e_context: EventContext):
  14. if e_context['context'].type != ContextType.TEXT:
  15. return
  16. content = e_context['context'].content
  17. logger.debug("[Hello] on_handle_context. content: %s" % content)
  18. if content == "Hello":
  19. reply = Reply()
  20. reply.type = ReplyType.TEXT
  21. msg = e_context['context']['msg']
  22. if e_context['context']['isgroup']:
  23. reply.content = "Hello, " + msg['ActualNickName'] + " from " + msg['User'].get('NickName', "Group")
  24. else:
  25. reply.content = "Hello, " + msg['User'].get('NickName', "My friend")
  26. e_context['reply'] = reply
  27. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  28. if content == "Hi":
  29. reply = Reply()
  30. reply.type = ReplyType.TEXT
  31. reply.content = "Hi"
  32. e_context['reply'] = reply
  33. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑,一般会覆写reply
  34. if content == "End":
  35. # 如果是文本消息"End",将请求转换成"IMAGE_CREATE",并将content设置为"The World"
  36. e_context['context'].type = ContextType.IMAGE_CREATE
  37. content = "The World"
  38. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑