Slack通知
一个简单的Python库,用于在应用程序里程碑时发送Slack通知。
特性
- 🚀 用于里程碑通知的简单API
- ⚙️ 通过环境变量或配置文件灵活配置
- 🔄 异步支持非阻塞通知
- 🛡️ 全面的错误处理和记录
- 📝 完整的类型提示和文档
- 🧪 通过示例进行了充分测试
- 🤖 新 用于AI代理集成的MCP服务器
- ⏰ 新 具有时间查询功能的实时Slack Agent
安装
pip install slack-notifications快速开始
- 设置您的Slack应用程序并获取机器人令牌:
- 首选 松弛API - 创建新应用程序或使用现有应用程序 - 将机器人添加到您的工作区 - 复制Bot用户OAuth令牌
- 配置环境变量:
# Copy the example file and fill in your values
cp .env.example .env
# Edit .env with your actual Slack bot token或者直接设置环境变量:
export SLACK_BOT_TOKEN="xoxb-your-bot-token-here"
export SLACK_DEFAULT_CHANNEL="#general"- 在Python代码中使用:
from slack_notifications import notify_milestone
# Send a simple milestone notification
notify_milestone("Application started successfully!")
# Send to a specific channel with custom level
notify_milestone("Database migration completed", channel="#dev-ops", level="info")
# Send error notifications
notify_milestone("Critical error occurred", level="error")配置
应用程序支持多种配置方法,自动按以下优先级顺序加载:
- 环境变量(最高优先级)
.env当前目录中的文件slack_notifications.toml文件- 内置默认值(最低优先级)
使用.env文件(推荐)
对于安全的静态部署配置:
- 复制示例文件:
cp .env.example .env- 编辑
.env根据您的实际值:
# Required
SLACK_BOT_TOKEN=xoxb-your-actual-bot-token
# Optional (with defaults shown)
SLACK_DEFAULT_CHANNEL=#general
SLACK_TIMEOUT=30
SLACK_MAX_RETRIES=3安全说明: 这 .env Git会自动忽略该文件,并且不应将其提交到版本控制中。
环境变量
您还可以通过环境变量设置配置:
SLACK_BOT_TOKEN:您的Slack机器人令牌(必填)SLACK_DEFAULT_CHANNEL:通知的默认通道(默认:“#general”)SLACK_TIMEOUT:请求超时(秒)(默认值:30)SLACK_MAX_RETRIES:最大重试次数(默认值:3)
TOML配置文件
或者,使用 slack_notifications.toml 文件:
# slack_notifications.toml
[slack]
bot_token = "xoxb-your-bot-token-here"
default_channel = "#notifications"
timeout = 30
max_retries = 3异步用法
对于异步应用程序中的非阻塞通知:
import asyncio
from slack_notifications import notify_milestone_async
async def main():
# Send notification asynchronously
await notify_milestone_async("Async operation completed")
# Continue with other work immediately
await do_other_work()
asyncio.run(main())错误处理
该库包括全面的错误处理:
from slack_notifications import notify_milestone, SlackNotificationError
try:
notify_milestone("Test notification")
print("Notification sent successfully!")
except SlackNotificationError as e:
print(f"Failed to send notification: {e}")高级用法
自定义配置
from slack_notifications import SlackNotifier
# Create notifier with custom config
notifier = SlackNotifier(
bot_token="xoxb-custom-token",
default_channel="#custom-channel",
timeout=60
)
notifier.notify("Custom notification")日志集成
该库与Python的日志模块集成:
import logging
from slack_notifications import SlackHandler
# Add Slack handler to your logger
logger = logging.getLogger()
slack_handler = SlackHandler(channel="#logs", level=logging.ERROR)
logger.addHandler(slack_handler)
# Now errors will be sent to Slack automatically
logger.error("This error will appear in Slack!")用于AI代理的MCP服务器
slack代理项目现在包括一个FastMCP服务器,允许AI代理通过模型上下文协议(MCP)向slack通道发送通知。
启动MCP服务器
python slack_mcp_server.py可用的MCP工具
send_slack_message(message, channel, level)-发送通知消息send_slack_success(message, channel)-发送成功通知send_slack_warning(message, channel)-发送警告通知send_slack_error(message, channel)-发送错误通知configure_slack_notifications(...)-配置Slack设置
AI代理集成
AI代理可以使用这些工具发送Slack消息。例如:
AI Agent: I need to notify the team about the deployment.
MCP Tool Call: send_slack_success("🚀 Production deployment completed successfully!", "#devops")看 docs/mcp-service-setup.md 有关详细的设置和使用说明。
模型配置(概念变更)
松弛代理项目支持人工智能模型配置和管理方式的概念性变化。这一变化在以下方面引入了明确的分离 模型 (用户与之交互的内容)以及 后端 (提供者实现)。
模型与后端概念
- 模型:具有友好名称、描述和功能的面向用户的界面
- 后端:实际提供者实现(OpenAI、Anthropic、Ollama等)
这实现了灵活的路由、故障转移、成本优化和多提供商支持。
配置结构
# models: What users see and select
models:
creative-writer:
backend: "anthropic/claude-3-sonnet"
description: "Creative writing assistant"
capabilities: ["chat", "completion"]
fast-chat:
backend: "ollama/llama3.2:3b"
description: "Fast local responses"
# backends: Provider configurations
backends:
anthropic:
provider: "anthropic"
api_key: "${ANTHROPIC_API_KEY}"
ollama:
provider: "ollama"
base_url: "http://localhost:11434"好处
- 提供者不可知论:在不更改面向用户的型号名称的情况下切换提供程序
- 故障转移:自动回退到其他提供商
- 成本优化:找到最便宜的供应商
- 负载平衡:跨多个后端分发请求
看 docs/model-backend-concept.md 完整的概念文件和 examples/model_configure.yaml 用于配置示例。
Slack代理
该项目包括一个Slack机器人程序,使用Web API监控频道,并使用当前CST时间响应时间查询。
运行Slack代理
export SLACK_BOT_TOKEN="xoxb-your-bot-token-here"
python slack_agent.py配置
Slack Agent支持其他配置:
# Optional: Comma-separated list of channel IDs to monitor
export SLACK_AGENT_CHANNELS="C1234567890,C0987654321"
# Optional: Polling interval in seconds (default: 5)
export SLACK_AGENT_POLL_INTERVAL=5如果 SLACK_AGENT_CHANNELS 如果未设置,代理将自动发现和监视名称中带有“general”的频道。
时间查询响应
代理会响应与时间相关的确切查询:
- “现在几点了”→ “当前时间为2025年12月25日美国中部时间上午11:00:00”
- “现在几点了?”→ 反应
- “时间”→ 反应
- “当前时间”→ 反应
日志记录
所有交互都会用CST时间戳记录到stdout中:
2025-12-25 11:00:00 CST - slack_agent - INFO - [2025-12-25 11:00:00 CST] Starting Slack Agent...
2025-12-25 11:00:00 CST - slack_agent - INFO - Bot authenticated as user: U1234567890
2025-12-25 11:00:00 CST - slack_agent - INFO - Monitoring channels: C1234567890
2025-12-25 11:00:00 CST - slack_agent - INFO - [2025-12-25 11:00:00 CST] MESSAGE - Channel: C1234567890, User: U1234567890, Text: 'what time is it?'
2025-12-25 11:00:00 CST - slack_agent - INFO - [2025-12-25 11:00:00 CST] RESPONSE - Sent time to channel C1234567890: The current time is 11:00:00 AM CST on 2025-12-25看 docs/slack-agent-usage.md 了解完整的设置和使用说明。
文档
对于AI代理
面向开发者
- 建筑 -系统架构与设计
- 实施参考 -代码模式和实践
- MCP服务设置 -MCP服务器配置
- Slack API设置 -Slack集成指南
- Slack代理使用情况 -Slack代理文档
- 故障排除 -常见问题和解决方案
系统文档
- 代理内核README -完整的代理内核文档
- 完成的普遍定义 -代理内核通用要求
- Python完成的定义 -Python特定标准
发展
设置
git clone https://github.com/yourusername/slack-notifications.git
cd slack-notifications
pip install -e ".[dev,test]"测试
pytest建筑
python -m build文档
对于AI代理
- 代理商.md -AI代理的强制性工作流程
- 完成的定义 -质量标准
- 工作流 -开发工作流程
面向开发者
- 文档索引 -完整的文档导航
- 建筑 -系统架构
- 实施参考 -代码模式
- 贡献 -贡献指南
例子
- 基本用法 -简单的通知示例
- 里程碑通知 -基于里程碑的通知
- 幽默演示 -创意演示脚本
指南
- Slack API设置 -配置Slack应用程序和令牌
- MCP服务设置 -FastMCP服务器配置
- 故障排除 -常见问题
______________________________________________________________________
最后更新日期:2026-02-01
许可证-请参阅 许可证 文件以获取详细信息。
贡献
欢迎投稿!请参阅 贡献.md 作为指导方针。
