A utility for sending messages via WeChat Work bot and email
Project description
Push Tool - 企业消息推送工具
一个通过企业微信机器人和电子邮件发送消息的实用工具库。
功能特性
企业微信机器人
-
文本消息
- 支持@提及特定用户
- 支持@提及所有人
- 支持消息链接
-
图片消息
- 支持本地图片文件
- 自动验证图片格式和大小
- 支持JPEG/PNG格式
-
文件消息
- 支持多种文件类型
- 自动检查文件大小限制(20MB)
-
Markdown消息
- 支持标准Markdown语法
- 支持标题、列表、代码块等格式
- 自动处理特殊字符转义
电子邮件发送
-
基础功能
- 纯文本邮件
- HTML格式邮件
- 支持抄送(CC)和密送(BCC)
-
附件支持
- 多附件支持
- 自动检测MIME类型
- 支持常见文档格式
-
安全连接
- TLS加密支持
- SMTP认证
- 连接超时处理
安装指南
通过PyPI安装
pip install PushInfo
从源码安装
- 克隆仓库:
ngit clone https://github.com/yourusername/push_info.git
cd push_info
- 安装依赖:
pip install .
开发模式安装
pip install -e .[dev]
依赖要求
- Python 3.7+
- 核心依赖:
requests>= 2.25.1python-dotenv>= 0.19.0 (用于环境变量配置)
开发依赖:
pytest>= 6.2.5pytest-cov>= 2.12.1mypy>= 0.910flake8>= 3.9.2
配置要求
企业微信机器人
- 在企业微信中创建群聊机器人
- 获取Webhook URL
- (可选)设置IP白名单
邮件服务器
- SMTP服务器地址和端口
- 认证用户名和密码
- (可选)配置TLS/SSL设置
详细使用指南
企业微信机器人高级用法
环境变量配置
建议将敏感信息存储在环境变量中:
import os
from push_tools import WeChatBot
# 从环境变量读取webhook_url
webhook_url = os.getenv("WECHAT_WEBHOOK_URL")
bot = WeChatBot(webhook_url)
消息发送重试机制
from push_tools import WeChatBot
from requests.exceptions import RequestException
bot = WeChatBot("your_webhook_url")
def send_with_retry(message, max_retries=3):
for attempt in range(max_retries):
try:
return bot.send_text(message)
except RequestException as e:
print(f"Attempt {attempt + 1} failed: {str(e)}")
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt) # 指数退避
send_with_retry("重要通知")
批量发送消息
messages = ["通知1", "通知2", "通知3"]
for msg in messages:
try:
bot.send_text(msg)
except Exception as e:
print(f"Failed to send message: {msg}. Error: {str(e)}")
邮件发送高级功能
使用环境变量配置
from push_tools import EmailSender
import os
sender = EmailSender(
smtp_server=os.getenv("SMTP_SERVER"),
smtp_port=int(os.getenv("SMTP_PORT", "587")),
username=os.getenv("SMTP_USERNAME"),
password=os.getenv("SMTP_PASSWORD")
)
发送带多个收件人和附件的邮件
sender.send_email(
subject="项目报告",
content="请查收项目报告",
to_addrs=["user1@example.com", "user2@example.com"],
cc_addrs=["manager@example.com"],
attachments=["report.pdf", "data.xlsx"]
)
邮件模板
def send_welcome_email(user_email, user_name):
content = f"""
<h1>欢迎{user_name}加入我们!</h1>
<p>您的账号已成功创建。</p>
"""
sender.send_email(
subject="欢迎邮件",
content=content,
to_addrs=user_email,
html=True
)
错误处理与调试
常见错误及解决方案
企业微信机器人错误
-
Webhook URL无效
- 检查URL是否正确
- 确认机器人是否已启用
-
消息发送失败
- 检查网络连接
- 验证消息内容是否符合规范
- 检查IP白名单设置
邮件发送错误
-
SMTP认证失败
- 检查用户名和密码
- 确认服务器是否需要TLS/SSL
-
附件发送失败
- 检查文件路径是否正确
- 验证文件大小是否超过限制
日志记录
建议配置日志记录以帮助调试:
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("push_tools")
try:
bot.send_text("测试消息")
except Exception as e:
logger.error(f"消息发送失败: {str(e)}")
测试指南
运行测试套件
pytest tests/ --cov=push_tools --cov-report=html
测试覆盖率报告
测试完成后,可以在htmlcov目录中查看详细的覆盖率报告:
open htmlcov/index.html
静态类型检查
mypy push_tools/
代码风格检查
flake8 push_tools/
贡献指南
我们欢迎各种形式的贡献,包括但不限于:
-
报告问题
- 在GitHub Issues中描述遇到的问题
- 提供重现步骤和环境信息
-
功能请求
- 详细描述需求场景
- 说明预期的行为
-
代码贡献
- Fork仓库并创建特性分支
- 提交清晰的提交信息
- 确保测试覆盖率不降低
- 更新相关文档
-
文档改进
- 修正拼写错误
- 添加使用示例
- 完善API文档
项目状态
许可证
本项目采用 MIT 许可证。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pushinfo-0.1.0.tar.gz
(8.1 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pushinfo-0.1.0.tar.gz.
File metadata
- Download URL: pushinfo-0.1.0.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
faa827ff3a03351b839ab343857e2e07e70c1a75c7274113228094e886d02d21
|
|
| MD5 |
c5946144b43e40faff893488e9203067
|
|
| BLAKE2b-256 |
38c7b8c898de7965b43ba5dc306e32442d1d7eafa2d5009eef6849f2413fdccf
|
File details
Details for the file pushinfo-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pushinfo-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cfc75b3f7b92632980d2f9421b372094240595fec10ff3f8ef046380e5802834
|
|
| MD5 |
678e8db92a75462c18f394943b5394fa
|
|
| BLAKE2b-256 |
cc913cff495354d442c06c3ba4a504da479919b25bacbdd88f19f2404bc36ed6
|