Package: python-mirai-core
Mirai HTTP SDK for Python中文Derived fromPython-Mirai. If you decided toStarthis project, please
alsoStarthe original project.A Flask like Python wrapper ofMirai-HTTP-APISee full documentationhereSynced with mirai core 1.0RCInstallationInstall from PyPIpipinstallpython-mirai-coreInstall from GitHubpipinstallgit+git:://github.com/NatriumLab/python-mirai-coreTL; DRSee code completion generated by PyCharm or VSCode.Fundamentals:BotandUpdaterare two object to interact with mirai-http-api.Botcontains all outbound actions (such assend_message),
all methods are well documented, and internal methods starts with_Updaterhandles all inbound updates (such as receiving events or messages)A list of available event is undermirai_core.models.Events.A list of available message components (to construct MessageChain) is undermirai_core.models.Message.FeaturesUpdater handshake with mirai-console automatically when console is restarted or session is expiredSimilar logic to python-telegram-bot or aiogramMessage type is an argument everywhere, no more send_group/friend/temp_messageSupports multiple listener for single event, usereturn Trueto block further calling for this event onlySupports Websocket (enabled by default)Supports xml/json/app messageExamplefrommirai_coreimportBot,Updaterfrommirai_core.modelsimportEvent,Message,Typesqq=123456host='127.0.0.1'port=18080auth_key='abcdefgh'scheme='http'# http or httpsbot=Bot(qq,host,port,auth_key,scheme=scheme)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 eventif only one type of event is handled by this method, the type hinting should be changed accordinglye.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 thetype hinting in event handler's definitione.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()Comprehensive example: seeUMRThanksThanksmamoebrings usmirai, a tremendous work that
enables boundless possibilities for QQ Bots.ThanksPython-Miraifor inspirations and data parsing.LicenseGNU AGPLv3Same asmirai