Skip to content

Repository files navigation

一个简单可扩展的异步消息通知库

Package versionSupported Python versions

安装

pip install use-notify

使用

fromuse_notifyimportuseNotify, useNotifyChannelnotify=useNotify()
notify.add(
# 添加多个通知渠道useNotifyChannel.Bark({"token": "xxxxxx"}),
useNotifyChannel.Ding({
"token": "xxxxx",
"at_all": True
})
)
notify.publish(title="消息标题", content="消息正文")

动态凭据

tokentopicuser 等凭据字段可以传入字符串,也可以传入无参数函数。发送时会解析函数返回值,适合把刷新、缓存逻辑放在业务侧:

useNotifyChannel.Bark({"token": lambda: get_current_bark_token()})
useNotifyChannel.Ntfy({"topic": lambda: get_current_ntfy_topic()})
useNotifyChannel.PushOver({
"token": lambda: get_current_app_token(),
"user": lambda: get_current_user_key(),
})

库只负责发送前读取当前凭据,不内置各平台 OAuth 或后台刷新线程。

useNotify.from_settings(...) 同样支持动态凭据,并使用内置渠道注册表解析渠道名:

fromuse_notifyimportuseNotifynotify=useNotify.from_settings({
"bark": {"token": lambda: get_current_bark_token()},
"ntfy": {"topic": lambda: get_current_ntfy_topic()},
})

渠道名大小写不敏感;未知渠道名会抛出 ValueError,避免配置拼写错误被静默忽略。

装饰器使用(推荐)

使用 @notify 装饰器可以自动为函数执行发送通知:

fromuse_notifyimportuseNotify, useNotifyChannel, notify, set_default_notify_instance# 创建并设置当前执行上下文的默认通知实例default_notify=useNotify()
default_notify.add(
useNotifyChannel.Bark({"token": "your_bark_token"}),
useNotifyChannel.Ding({"token": "your_ding_token"})
)
set_default_notify_instance(default_notify)
# 现在可以直接使用装饰器,无需传递 notify_instance@notify()defdata_processing():
# 数据处理逻辑return"数据处理完成"@notify(title="重要任务")defimportant_task():
# 重要任务逻辑return"重要任务完成"@notify(notify_on_error=True, notify_on_success=False)defrisky_operation():
# 只在出错时通知ifsome_condition:
raiseException("操作失败")
return"操作成功"# 异步函数支持@notify()asyncdefasync_task():
awaitsome_async_operation()
return"异步任务完成"

装饰器特性:

  • ✅ 自动发送成功/失败通知
  • ✅ 支持同步和异步函数
  • ✅ 可配置通知条件和自定义消息
  • ✅ 默认实例机制,简化使用
  • ✅ 执行上下文信息收集

📖 详细文档: 装饰器完整使用指南

支持的消息通知渠道列表

  • Wechat(企微)
  • Ding(钉钉)
  • Bark
  • Email
  • Chanify
  • PushDeer
  • PushOver
  • FeiShu(飞书)
  • Ntfy

自己开发消息通知

fromuse_notifyimportuseNotifyChannelclassCustom(useNotifyChannel.BaseChannel):
"""自定义消息通知"""defsend(self, content, title=None):
...
asyncdefsend_async(self, content, title=None):
...

更多示例

文档

Agent Skill

  • Installable skill package: SKILL.md + skill-references/
  • Purpose: help AI coding agents generate correct use-notify integration snippets, choose channels, configure dynamic credentials, and troubleshoot retry/error behavior

特性

  • 🚀 简单易用: 几行代码即可集成多种通知渠道
  • 🔄 异步支持: 完整的异步/同步双模式支持
  • 🎯 装饰器模式: 使用 @notify 装饰器自动化通知
  • 🔧 高度可扩展: 轻松添加自定义通知渠道
  • 📱 多渠道支持: 支持微信、钉钉、Bark、邮件等多种通知方式
  • 🔐 动态凭据: 支持发送前解析 tokentopicuser 等凭据字段
  • ⚙️ 灵活配置: 支持条件通知、自定义模板、默认实例、超时与重试等高级功能

About

一个简易且可扩展的消息通知组件,已内置钉钉,企微,bark,pushover,pushdeer,飞书,邮件等消息通知组件

Topics

Resources

Stars

27 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages