Skip to main content

A framework for OICQ(QQ, made by Tencent) headless client "Mirai".

Project description

Mirai HTTP SDK for Python

中文

Derived from Python-Mirai. If you decided to Star this project, please also Star the original project.

A Flask like Python wrapper of Mirai-HTTP-API

See full documentation here

Synced with mirai core 1.0RC

Installation

Install from PyPI

pip install python-mirai-core

Install from GitHub

pip install git+git:://github.com/NatriumLab/python-mirai-core

TL; DR

See code completion generated by PyCharm or VSCode.

Fundamentals:

Bot and Updater are two object to interact with mirai-http-api.

Bot contains all outbound actions (such as send_message), all methods are well documented, and internal methods starts with _

Updater handles all inbound updates (such as receiving events or messages)

A list of available event is under mirai_core.models.Events.

A list of available message components (to construct MessageChain) is under mirai_core.models.Message.

Features

  • Updater handshake with mirai-console automatically when console is restarted or session is expired

  • Similar logic to python-telegram-bot or aiogram

  • Message type is an argument everywhere, no more send_group/friend/temp_message

  • Supports multiple listener for single event, use return True to block further calling for this event only

  • Supports Websocket (enabled by default)

  • Supports xml/json/app message

Example

from mirai_core import Bot, Updater
from mirai_core.models import Event, Message, Types

qq = 123456
host = '127.0.0.1'
port = 18080
auth_key = 'abcdefgh'
scheme = 'http'  # http or https

bot = 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])
async def handler(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
    """
    if isinstance(event, Event.Message):  # handle different types of events
        # see auto completion for event for available attributes
        # echo
        await bot.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 components
        message_chain = [
            # see docstring for __init__ for argument descriptions
            Message.Plain(text='test')
        ]
        if event.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 method
        bot_message = await bot.send_message(target=event.sender,
                                             message_type=event.type,
                                             message=message_chain,
                                             # friend message can also quoted, but only viewable by QQ, not TIM
                                             quote_source=event.messageChain.get_source())

        # in case you want the message id for recalling
        print(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 used
        image_id = image.imageId
        print(image_id)
        return True

# run the updater forever, block the program from exiting
updater.run()

Comprehensive example: see UMR

Thanks

Thanks mamoe brings us mirai, a tremendous work that enables boundless possibilities for QQ Bots.

Thanks Python-Mirai for inspirations and data parsing.

License

GNU AGPLv3

Same as mirai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-mirai-core-0.10.0.tar.gz (21.1 kB view details)

Uploaded Source

File details

Details for the file python-mirai-core-0.10.0.tar.gz.

File metadata

  • Download URL: python-mirai-core-0.10.0.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for python-mirai-core-0.10.0.tar.gz
Algorithm Hash digest
SHA256 b90b803af9066f38c2d739b9c36d16e2a2e061074191d30ffd14812d1d6a920f
MD5 f9ec6178204f29a28f2c38d37611effc
BLAKE2b-256 77bcda1a2da6318199197f3c29afda159559795331e55a292e697fae6cb8b789

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page