一个无头客户端“Mirai”的框架。
python-mirai-core的Python项目详细描述
用于HTTP的Python SDK
从Python-Mirai派生。如果您决定Star这个项目,请 还有Star原始项目。在
一个瓶子状的Python包装纸Mirai-HTTP-API
请参阅完整文档here
与mirai core 1.0RC同步
安装
从PyPI安装
pip install python-mirai-core
从GitHub安装
^{pr2}$TL;DR
请参见PyCharm或VSCode生成的代码完成。在
基本原理:
Bot
和{
Bot
包含所有出站操作(例如send_message
),
所有方法都有很好的文档记录,内部方法以_
开头
Updater
处理所有入站更新(例如接收事件或消息)
可用事件列表在mirai_core.models.Events
下。在
可用消息组件列表(用于构造消息链)位于mirai_core.models.Message
下。在
特点
- 在
当控制台重新启动或会话过期时,会自动与mirai控制台进行更新程序握手
在 - 在
类似于python telegram bot或aigram的逻辑
在 - 在
消息类型在任何地方都是一个参数,不再发送“组/朋友/临时消息”
在 - 在
支持单个事件的多个侦听器,请使用
在return True
仅阻止对此事件的进一步调用 - 在
支持Websocket(默认启用)
在 - 在
支持xml/json/app消息
在
示例
frommirai_coreimportBot,Updaterfrommirai_core.modelsimportEvent,Message,Typesqq=123456host='127.0.0.1'port=18080auth_key='abcdefgh'bot=Bot(qq,host,port,auth_key)updater=Updater(bot)# for bot methods, see available methods under mirai_core.Bot# for event types, see mirai_core.models.Event# for enums, see mirai_core.models.Types# for exception types, see mirai_core.exceptions# this is how handling inbound events looks like@updater.add_handler([Event.Message])asyncdefhandler(event:Event.BaseEvent):""" handler for multiple events :param event: generic type of event if only one type of event is handled by this method, the type hinting should be changed accordingly e.g. async def handler(event: BaseEvent.Message): in order to see detailed definition of a certain event, either use isinstance to restrict the type, or change the type hinting in event handler's definition e.g. if isinstance(event, BaseEvent.Message): :return: True for block calling other event handlers for this event, None or False for keep calling the rest """ifisinstance(event,Event.Message):# handle different types of events# see auto completion for event for available attributes# echoawaitbot.send_message(target=event.sender,message_type=event.type,message=event.messageChain,quote_source=event.messageChain.get_source())# custom message# see auto completion for Message for more available message componentsmessage_chain=[# see docstring for __init__ for argument descriptionsMessage.Plain(text='test')]ifevent.type==Types.MessageType.GROUP:message_chain.append(event.member.id)image=Message.Image(path='/root/whatever.jpg')message_chain.append(image)# see docstring for individual methodbot_message=awaitbot.send_message(target=event.sender,message_type=event.type,message=message_chain,# friend message can also quoted, but only viewable by QQ, not TIMquote_source=event.messageChain.get_source())# in case you want the message id for recallingprint(bot_message.messageId)# in case you want the image id (only available when sending via local path instead of url)# the image id is available for two weeks from the last time it is usedimage_id=image.imageIdprint(image_id)returnTrue# run the updater forever, block the program from exitingupdater.run()
综合示例:请参见UMR
谢谢
谢谢^{
感谢^{
许可证
与^{
- 项目
标签: