Skip to main content

A bridge for sending notifications to various platforms

Project description

notify-bridge

A flexible notification bridge for sending messages to various platforms.

Python Version Nox PyPI Version Downloads Downloads Downloads License PyPI Format Maintenance Codecov

一个灵活的通知桥接器,用于向各种平台发送消息。

特性

  • 🚀 简单直观的 API
  • 🔌 插件系统,方便扩展
  • 🔄 同时支持同步和异步操作
  • 🛡️ 使用 Pydantic 模型进行类型安全验证
  • 📝 丰富的消息格式(文本、Markdown 等)
  • 🌐 支持多个平台

快速开始

from notify_bridge import NotifyBridge

# 创建桥接器实例
bridge = NotifyBridge()

# 同步发送通知
response = bridge.send(
    "feishu",
    webhook_url="YOUR_WEBHOOK_URL",
    title="测试消息",
    content="来自 notify-bridge 的问候!",
    msg_type="text"
)
print(response)


# 异步发送通知
async def send_async():
    response = await bridge.notify_async(
        "feishu",
        webhook_url="YOUR_WEBHOOK_URL",
        title="异步测试消息",
        content="# 来自 notify-bridge 的问候!\n\n这是一条 **Markdown** 消息。",
        msg_type="post"
    )
    print(response)

安装

pip install notify-bridge

支持的平台

  • 飞书 (Feishu)
  • 企业微信 (WeCom)
  • GitHub (Issues)
  • Notify (通用通知 API)
  • 钉钉 (DingTalk)
  • 电子邮件 (Email)
  • Slack
  • Discord

使用示例

飞书 (Feishu)

# 发送文本消息
bridge.send(
    "feishu",
    webhook_url="YOUR_WEBHOOK_URL",
    content="这是一条文本消息",
    msg_type="text"
)

# 发送富文本消息
bridge.send(
    "feishu",
    webhook_url="YOUR_WEBHOOK_URL",
    title="消息标题",
    content="这是一条富文本消息的内容",
    msg_type="post"
)

# 发送图片消息
bridge.send(
    "feishu",
    webhook_url="YOUR_WEBHOOK_URL",
    image_path="path/to/image.jpg",  # 或者使用 image_key
    msg_type="image"
)

# 发送文件消息
bridge.send(
    "feishu",
    webhook_url="YOUR_WEBHOOK_URL",
    file_path="path/to/document.pdf",  # 或者使用 file_key
    msg_type="file"
)

企业微信 (WeCom)

# 发送文本消息
bridge.send(
    "wecom",
    webhook_url="YOUR_WEBHOOK_URL",
    content="这是一条文本消息",
    msg_type="text"
)

# 发送 Markdown 消息
bridge.send(
    "wecom",
    webhook_url="YOUR_WEBHOOK_URL",
    content="**粗体文本**\n> 引用\n[链接](https://example.com)",
    msg_type="markdown"
)

# 发送图文消息
bridge.send(
    "wecom",
    webhook_url="YOUR_WEBHOOK_URL",
    title="图文消息标题",
    content="图文消息描述",
    msg_type="news",
    articles=[{
        "title": "文章标题",
        "description": "文章描述",
        "url": "https://example.com",
        "picurl": "https://example.com/image.jpg"
    }]
)

GitHub

# 创建 Issue
bridge.send(
    "github",
    owner="username",
    repo="repository",
    token="YOUR_GITHUB_TOKEN",
    title="Test Issue",
    message="Hello from notify-bridge! This is a test issue.",
    labels=["test", "notify-bridge"],
    msg_type="text"
)

# 创建 Markdown Issue
bridge.send(
    "github",
    owner="username",
    repo="repository",
    token="YOUR_GITHUB_TOKEN",
    title="Test Markdown Issue",
    message="# Hello from notify-bridge!\n\nThis is a **markdown** issue.",
    labels=["test", "notify-bridge"],
    msg_type="markdown"
)

# 异步创建多个 Issues
async def create_issues():
    tasks = [
        bridge.send_async(
            "github",
            owner="username",
            repo="repository",
            token="YOUR_GITHUB_TOKEN",
            title=f"Async Test Issue {i}",
            message=f"This is async test issue {i}",
            labels=["test", "notify-bridge"],
            msg_type="text"
        ) for i in range(3)
    ]
    responses = await asyncio.gather(*tasks)
    return responses

Notify (通用通知 API)

# 发送文本消息
bridge.send(
    "notify",
    base_url="YOUR_NOTIFY_BASE_URL",
    title="Test Message",
    message="Hello from notify-bridge! This is a test message.",
    tags=["test", "notify-bridge"],
    msg_type="text"
)

# 发送带认证的消息
bridge.send(
    "notify",
    base_url="YOUR_NOTIFY_BASE_URL",
    token="YOUR_BEARER_TOKEN",  # 可选的认证令牌
    title="Authenticated Message",
    message="This message requires authentication.",
    tags=["secure", "notify-bridge"],
    msg_type="text"
)

# 异步发送多条消息
async def send_messages():
    tasks = [
        bridge.send_async(
            "notify",
            base_url="YOUR_NOTIFY_BASE_URL",
            title=f"Async Test Message {i}",
            message=f"This is async test message {i}",
            tags=["test", "notify-bridge"],
            msg_type="text"
        ) for i in range(3)
    ]
    responses = await asyncio.gather(*tasks)
    return responses

环境变量

你可以使用环境变量来存储敏感信息,比如 webhook URL:

# .env
FEISHU_WEBHOOK_URL=https://open.feishu.cn/open-apis/bot/v2/hook/xxx
WECOM_WEBHOOK_URL=https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx

# Python 代码
import os
from dotenv import load_dotenv

load_dotenv()

bridge.send(
    "feishu",
    webhook_url=os.getenv("FEISHU_WEBHOOK_URL"),
    content="测试消息",
    msg_type="text"
)

错误处理

from notify_bridge.exceptions import NotificationError, ValidationError

try:
    response = bridge.send(
        "feishu",
        webhook_url="YOUR_WEBHOOK_URL",
        content="测试消息",
        msg_type="text"
    )
except ValidationError as e:
    print(f"验证错误:{e}")
except NotificationError as e:
    print(f"通知错误:{e}")

创建插件

  1. 创建通知器类:
from notify_bridge.schema import BaseNotifier, NotificationSchema
from pydantic import Field


class MySchema(NotificationSchema):
    """自定义通知模式。"""
    webhook_url: str = Field(..., description="Webhook URL")
    title: str = Field(None, description="消息标题")
    content: str = Field(..., description="消息内容")
    msg_type: str = Field("text", description="消息类型")


class MyNotifier(BaseNotifier):
    """自定义通知器。"""
    name = "my_notifier"  # 通知器名称
    schema = MySchema  # 通知器模式

    def notify(self, notification: NotificationSchema) -> NotificationResponse:
        """同步发送通知。"""
        # 实现你的通知逻辑
        pass

    async def notify_async(self, notification: NotificationSchema) -> NotificationResponse:
        """异步发送通知。"""
        # 实现你的异步通知逻辑
        pass
  1. pyproject.toml 中注册你的插件:
[project.entry-points."notify_bridge.notifiers"]
my_notifier = "my_package.my_module:MyNotifier"

开发指南

  1. 克隆仓库:
git clone https://github.com/loonghao/notify-bridge.git
cd notify-bridge
  1. 安装依赖:
pip install -e ".[dev]"
  1. 运行测试:
pytest
  1. 运行代码检查:
nox

贡献

欢迎贡献!请随时提交 Pull Request。

  1. Fork 仓库
  2. 创建你的功能分支:git checkout -b feature/my-feature
  3. 提交你的更改:git commit -am 'Add some feature'
  4. 推送到分支:git push origin feature/my-feature
  5. 提交 Pull Request

许可证

本项目基于 MIT 许可证 - 详见 LICENSE 文件。

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

notify_bridge-0.8.0.tar.gz (46.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

notify_bridge-0.8.0-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file notify_bridge-0.8.0.tar.gz.

File metadata

  • Download URL: notify_bridge-0.8.0.tar.gz
  • Upload date:
  • Size: 46.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for notify_bridge-0.8.0.tar.gz
Algorithm Hash digest
SHA256 3d3aa7dc0652b78b025eb324767e22d7472e60f8171e6ead814b76d8f3af6dac
MD5 017ab80e9e304c287b6856974304e23f
BLAKE2b-256 5d1001adebbd7d107aaa4b209d2b65f2014e939a9188ec4559591c295a9f8b6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for notify_bridge-0.8.0.tar.gz:

Publisher: python-publish.yml on loonghao/notify-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file notify_bridge-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: notify_bridge-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 31.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for notify_bridge-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 951b6559f403d60fb69cfd24ab1e84d34dba9d5000b99bc2fb8979aa32626b68
MD5 d50333d5fe3aa0736752b825f7ee4f2d
BLAKE2b-256 eb826d346bae0ebbdf904741262db4d4dee580a8a6940784b079a58c503f50c3

See more details on using hashes here.

Provenance

The following attestation bundles were made for notify_bridge-0.8.0-py3-none-any.whl:

Publisher: python-publish.yml on loonghao/notify-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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