starbot-message


Namestarbot-message JSON
Version 0.0.1b0 PyPI version JSON
download
home_pagehttps://github.com/Starlwr/StarBotMessage
SummaryStarBot 消息推送接口
upload_time2024-03-28 14:31:09
maintainerNone
docs_urlNone
authorStarlwr
requires_python<4.0,>=3.10
licenseAGPL-3.0-only
keywords starbot bilibili bot
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <div align="center">

# StarBotMessage

[![PyPI](https://img.shields.io/pypi/v/starbot-message)](https://pypi.org/project/starbot-message)
[![Python](https://img.shields.io/badge/python-3.10%20|%203.11-blue)](https://www.python.org)
[![License](https://img.shields.io/github/license/Starlwr/StarBotMessage)](https://github.com/Starlwr/StarBotMessage/blob/master/LICENSE)
[![STARS](https://img.shields.io/github/stars/Starlwr/StarBotMessage?color=yellow&label=Stars)](https://github.com/Starlwr/StarBotMessage/stargazers)

**StarBot 消息推送接口**
</div>

## 快速开始
### 安装

```shell
pip install starbot-message
```

### 开发

通过继承抽象类 MessageHandler 并在其中实现消息收发方法,即可实现其他平台的消息推送,示例:[example/simple.py](https://github.com/Starlwr/StarBotMessage/blob/master/example/simple.py)

```python
import abc
from typing import Union, Tuple, Optional, NoReturn

from starbot_executor import executor

from starbot_message import EventType
from starbot_message import MessageSend, MessageReceive


class MessageHandler(metaclass=abc.ABCMeta):
    """
    消息处理器基类,较简单的消息处理器实现请直接继承本类,在构造方法中初始化,并实现消息发送与接收,示例:example/simple.py
    """
    platform: str
    """推送平台唯一标识符,请使用 平台名称/自定义名称(建议使用推送平台实现所在的代码仓库名) 的格式,并注意唯一性,例:QQ/StarBot"""

    account: Union[int, str]
    """机器人账号"""

    def __init__(self, platform: str, account: Union[int, str]):
        """
        请在子类中重写构造方法,在其中使用 super().__init__("平台名称/自定义名称", account) 进行父类构造
        """
        if platform is None or account is None:
            raise ValueError("消息处理器的推送平台标识符及账号不能为空")

        self.platform = platform
        self.account = account

    @abc.abstractmethod
    async def run(self) -> NoReturn:
        """
        在所有消息处理器注册完毕后,此方法会被依次自动调用,若不需要使用可保留为空实现
        """
        pass

    @abc.abstractmethod
    async def send(self, message: MessageSend) -> Tuple[bool, Optional[str]]:
        """
        发送消息,需实现 MessageSend 类型到平台消息类型的转换,并将消息发送至对应平台

        Args:
            message: 原始发送消息实例

        Returns:
            由 (是否发送成功, 发送失败原因) 组成的元组,当消息发送成功时忽略第二个返回值
        """
        pass

    @classmethod
    def received(cls, message: MessageReceive) -> NoReturn:
        """
        请自行实现消息接收方法,并在接收到消息后,将平台消息类型转换为 MessageReceive 类型后调用此方法,以触发命令

        Args:
            message: 接收消息实例
        """
        executor.dispatch(message, EventType.ReceiveMessageEvent)
```

对于较复杂的实现,可继承消息处理器抽象工厂实现,示例:[example/complex.py](https://github.com/Starlwr/StarBotMessage/blob/master/example/complex.py)

```python
import abc
from typing import Union, List

from starbot_message import MessageHandler

class AbstractMessageHandlerFactory(metaclass=abc.ABCMeta):
    """
    消息处理器工厂,较复杂的消息处理器实现请继承本类,实现生产方法,可返回一个或多个 MessageHandler 实例,示例:example/complex.py
    """

    @abc.abstractmethod
    async def produce(self, account: Union[int, str]) -> Union[MessageHandler, List[MessageHandler]]:
        """
        用于创建复杂的 MessageHandler 实例
        例如为某平台的好友推送与群推送分别实现消息处理器,可在此初始化后,生成两个对应的 MessageHandler 实例,封装在列表中返回
        """
        pass
```

## 鸣谢

* [StarBotExecutor](https://github.com/Starlwr/StarBotExecutor): 一个基于订阅发布模式的异步执行器

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Starlwr/StarBotMessage",
    "name": "starbot-message",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.10",
    "maintainer_email": null,
    "keywords": "starbot, bilibili, bot",
    "author": "Starlwr",
    "author_email": "lwr1104@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/b5/c7/6075d80d82fece1e0ae95008d488aa66165c353113425ccbd2830aefe67e/starbot_message-0.0.1b0.tar.gz",
    "platform": null,
    "description": "<div align=\"center\">\n\n# StarBotMessage\n\n[![PyPI](https://img.shields.io/pypi/v/starbot-message)](https://pypi.org/project/starbot-message)\n[![Python](https://img.shields.io/badge/python-3.10%20|%203.11-blue)](https://www.python.org)\n[![License](https://img.shields.io/github/license/Starlwr/StarBotMessage)](https://github.com/Starlwr/StarBotMessage/blob/master/LICENSE)\n[![STARS](https://img.shields.io/github/stars/Starlwr/StarBotMessage?color=yellow&label=Stars)](https://github.com/Starlwr/StarBotMessage/stargazers)\n\n**StarBot \u6d88\u606f\u63a8\u9001\u63a5\u53e3**\n</div>\n\n## \u5feb\u901f\u5f00\u59cb\n### \u5b89\u88c5\n\n```shell\npip install starbot-message\n```\n\n### \u5f00\u53d1\n\n\u901a\u8fc7\u7ee7\u627f\u62bd\u8c61\u7c7b MessageHandler \u5e76\u5728\u5176\u4e2d\u5b9e\u73b0\u6d88\u606f\u6536\u53d1\u65b9\u6cd5\uff0c\u5373\u53ef\u5b9e\u73b0\u5176\u4ed6\u5e73\u53f0\u7684\u6d88\u606f\u63a8\u9001\uff0c\u793a\u4f8b\uff1a[example/simple.py](https://github.com/Starlwr/StarBotMessage/blob/master/example/simple.py)\n\n```python\nimport abc\nfrom typing import Union, Tuple, Optional, NoReturn\n\nfrom starbot_executor import executor\n\nfrom starbot_message import EventType\nfrom starbot_message import MessageSend, MessageReceive\n\n\nclass MessageHandler(metaclass=abc.ABCMeta):\n    \"\"\"\n    \u6d88\u606f\u5904\u7406\u5668\u57fa\u7c7b\uff0c\u8f83\u7b80\u5355\u7684\u6d88\u606f\u5904\u7406\u5668\u5b9e\u73b0\u8bf7\u76f4\u63a5\u7ee7\u627f\u672c\u7c7b\uff0c\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\u521d\u59cb\u5316\uff0c\u5e76\u5b9e\u73b0\u6d88\u606f\u53d1\u9001\u4e0e\u63a5\u6536\uff0c\u793a\u4f8b\uff1aexample/simple.py\n    \"\"\"\n    platform: str\n    \"\"\"\u63a8\u9001\u5e73\u53f0\u552f\u4e00\u6807\u8bc6\u7b26\uff0c\u8bf7\u4f7f\u7528 \u5e73\u53f0\u540d\u79f0/\u81ea\u5b9a\u4e49\u540d\u79f0(\u5efa\u8bae\u4f7f\u7528\u63a8\u9001\u5e73\u53f0\u5b9e\u73b0\u6240\u5728\u7684\u4ee3\u7801\u4ed3\u5e93\u540d) \u7684\u683c\u5f0f\uff0c\u5e76\u6ce8\u610f\u552f\u4e00\u6027\uff0c\u4f8b\uff1aQQ/StarBot\"\"\"\n\n    account: Union[int, str]\n    \"\"\"\u673a\u5668\u4eba\u8d26\u53f7\"\"\"\n\n    def __init__(self, platform: str, account: Union[int, str]):\n        \"\"\"\n        \u8bf7\u5728\u5b50\u7c7b\u4e2d\u91cd\u5199\u6784\u9020\u65b9\u6cd5\uff0c\u5728\u5176\u4e2d\u4f7f\u7528 super().__init__(\"\u5e73\u53f0\u540d\u79f0/\u81ea\u5b9a\u4e49\u540d\u79f0\", account) \u8fdb\u884c\u7236\u7c7b\u6784\u9020\n        \"\"\"\n        if platform is None or account is None:\n            raise ValueError(\"\u6d88\u606f\u5904\u7406\u5668\u7684\u63a8\u9001\u5e73\u53f0\u6807\u8bc6\u7b26\u53ca\u8d26\u53f7\u4e0d\u80fd\u4e3a\u7a7a\")\n\n        self.platform = platform\n        self.account = account\n\n    @abc.abstractmethod\n    async def run(self) -> NoReturn:\n        \"\"\"\n        \u5728\u6240\u6709\u6d88\u606f\u5904\u7406\u5668\u6ce8\u518c\u5b8c\u6bd5\u540e\uff0c\u6b64\u65b9\u6cd5\u4f1a\u88ab\u4f9d\u6b21\u81ea\u52a8\u8c03\u7528\uff0c\u82e5\u4e0d\u9700\u8981\u4f7f\u7528\u53ef\u4fdd\u7559\u4e3a\u7a7a\u5b9e\u73b0\n        \"\"\"\n        pass\n\n    @abc.abstractmethod\n    async def send(self, message: MessageSend) -> Tuple[bool, Optional[str]]:\n        \"\"\"\n        \u53d1\u9001\u6d88\u606f\uff0c\u9700\u5b9e\u73b0 MessageSend \u7c7b\u578b\u5230\u5e73\u53f0\u6d88\u606f\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u5e76\u5c06\u6d88\u606f\u53d1\u9001\u81f3\u5bf9\u5e94\u5e73\u53f0\n\n        Args:\n            message: \u539f\u59cb\u53d1\u9001\u6d88\u606f\u5b9e\u4f8b\n\n        Returns:\n            \u7531 (\u662f\u5426\u53d1\u9001\u6210\u529f, \u53d1\u9001\u5931\u8d25\u539f\u56e0) \u7ec4\u6210\u7684\u5143\u7ec4\uff0c\u5f53\u6d88\u606f\u53d1\u9001\u6210\u529f\u65f6\u5ffd\u7565\u7b2c\u4e8c\u4e2a\u8fd4\u56de\u503c\n        \"\"\"\n        pass\n\n    @classmethod\n    def received(cls, message: MessageReceive) -> NoReturn:\n        \"\"\"\n        \u8bf7\u81ea\u884c\u5b9e\u73b0\u6d88\u606f\u63a5\u6536\u65b9\u6cd5\uff0c\u5e76\u5728\u63a5\u6536\u5230\u6d88\u606f\u540e\uff0c\u5c06\u5e73\u53f0\u6d88\u606f\u7c7b\u578b\u8f6c\u6362\u4e3a MessageReceive \u7c7b\u578b\u540e\u8c03\u7528\u6b64\u65b9\u6cd5\uff0c\u4ee5\u89e6\u53d1\u547d\u4ee4\n\n        Args:\n            message: \u63a5\u6536\u6d88\u606f\u5b9e\u4f8b\n        \"\"\"\n        executor.dispatch(message, EventType.ReceiveMessageEvent)\n```\n\n\u5bf9\u4e8e\u8f83\u590d\u6742\u7684\u5b9e\u73b0\uff0c\u53ef\u7ee7\u627f\u6d88\u606f\u5904\u7406\u5668\u62bd\u8c61\u5de5\u5382\u5b9e\u73b0\uff0c\u793a\u4f8b\uff1a[example/complex.py](https://github.com/Starlwr/StarBotMessage/blob/master/example/complex.py)\n\n```python\nimport abc\nfrom typing import Union, List\n\nfrom starbot_message import MessageHandler\n\nclass AbstractMessageHandlerFactory(metaclass=abc.ABCMeta):\n    \"\"\"\n    \u6d88\u606f\u5904\u7406\u5668\u5de5\u5382\uff0c\u8f83\u590d\u6742\u7684\u6d88\u606f\u5904\u7406\u5668\u5b9e\u73b0\u8bf7\u7ee7\u627f\u672c\u7c7b\uff0c\u5b9e\u73b0\u751f\u4ea7\u65b9\u6cd5\uff0c\u53ef\u8fd4\u56de\u4e00\u4e2a\u6216\u591a\u4e2a MessageHandler \u5b9e\u4f8b\uff0c\u793a\u4f8b\uff1aexample/complex.py\n    \"\"\"\n\n    @abc.abstractmethod\n    async def produce(self, account: Union[int, str]) -> Union[MessageHandler, List[MessageHandler]]:\n        \"\"\"\n        \u7528\u4e8e\u521b\u5efa\u590d\u6742\u7684 MessageHandler \u5b9e\u4f8b\n        \u4f8b\u5982\u4e3a\u67d0\u5e73\u53f0\u7684\u597d\u53cb\u63a8\u9001\u4e0e\u7fa4\u63a8\u9001\u5206\u522b\u5b9e\u73b0\u6d88\u606f\u5904\u7406\u5668\uff0c\u53ef\u5728\u6b64\u521d\u59cb\u5316\u540e\uff0c\u751f\u6210\u4e24\u4e2a\u5bf9\u5e94\u7684 MessageHandler \u5b9e\u4f8b\uff0c\u5c01\u88c5\u5728\u5217\u8868\u4e2d\u8fd4\u56de\n        \"\"\"\n        pass\n```\n\n## \u9e23\u8c22\n\n* [StarBotExecutor](https://github.com/Starlwr/StarBotExecutor): \u4e00\u4e2a\u57fa\u4e8e\u8ba2\u9605\u53d1\u5e03\u6a21\u5f0f\u7684\u5f02\u6b65\u6267\u884c\u5668\n",
    "bugtrack_url": null,
    "license": "AGPL-3.0-only",
    "summary": "StarBot \u6d88\u606f\u63a8\u9001\u63a5\u53e3",
    "version": "0.0.1b0",
    "project_urls": {
        "Homepage": "https://github.com/Starlwr/StarBotMessage",
        "Repository": "https://github.com/Starlwr/StarBotMessage"
    },
    "split_keywords": [
        "starbot",
        " bilibili",
        " bot"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a805f2602c44f0d4467f5a997aa2c2ff15d7401c5f4c2a57dfc5a44c115e1af8",
                "md5": "10d0a0c47dcedfefef579c315cd60ffa",
                "sha256": "76f905059d1a85414598999c210b2670d753a943620a7e74529f5801ae0ba9b6"
            },
            "downloads": -1,
            "filename": "starbot_message-0.0.1b0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "10d0a0c47dcedfefef579c315cd60ffa",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.10",
            "size": 21585,
            "upload_time": "2024-03-28T14:31:07",
            "upload_time_iso_8601": "2024-03-28T14:31:07.731490Z",
            "url": "https://files.pythonhosted.org/packages/a8/05/f2602c44f0d4467f5a997aa2c2ff15d7401c5f4c2a57dfc5a44c115e1af8/starbot_message-0.0.1b0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b5c76075d80d82fece1e0ae95008d488aa66165c353113425ccbd2830aefe67e",
                "md5": "930ff6d5c6d56b447ac086fa6ec4be25",
                "sha256": "a7e6207a2cbef71b2b07279aef0a9d639bb0eda1f7bc824503ec268caf73d798"
            },
            "downloads": -1,
            "filename": "starbot_message-0.0.1b0.tar.gz",
            "has_sig": false,
            "md5_digest": "930ff6d5c6d56b447ac086fa6ec4be25",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.10",
            "size": 24100,
            "upload_time": "2024-03-28T14:31:09",
            "upload_time_iso_8601": "2024-03-28T14:31:09.774648Z",
            "url": "https://files.pythonhosted.org/packages/b5/c7/6075d80d82fece1e0ae95008d488aa66165c353113425ccbd2830aefe67e/starbot_message-0.0.1b0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-28 14:31:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Starlwr",
    "github_project": "StarBotMessage",
    "github_not_found": true,
    "lcname": "starbot-message"
}
        
Elapsed time: 0.22720s