本文目录导读:

我来介绍几种钉钉机器人发送消息的Python方法:
使用 Webhook 发送(最简单)
文本消息
import requests
import json
def send_dingtalk_message(webhook_url, content):
"""
发送钉钉机器人消息
"""
headers = {
'Content-Type': 'application/json'
}
data = {
"msgtype": "text",
"text": {
"content": content
}
}
response = requests.post(webhook_url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
webhook_url = "https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN"
result = send_dingtalk_message(webhook_url, "测试消息:Hello from Python!")
print(result)
Markdown 消息
def send_markdown_message(webhook_url, title, text):
"""
发送Markdown格式消息
"""
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "markdown",
"markdown": {
"title": title,
"text": text
}
}
response = requests.post(webhook_url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
markdown_text = """一二
**粗体** *斜体*
[链接](http://www.baidu.com)
1. 第一项
2. 第二项"""
send_markdown_message(webhook_url, "Markdown测试", markdown_text)
完整功能封装
import requests
import json
import hashlib
import base64
import hmac
import time
class DingTalkBot:
def __init__(self, webhook_url, secret=None):
"""
初始化钉钉机器人
:param webhook_url: Webhook地址
:param secret: 安全设置中的加签密钥(可选)
"""
self.webhook_url = webhook_url
self.secret = secret
def _sign(self):
"""
计算签名(如果设置了密钥)
"""
if not self.secret:
return self.webhook_url, {}
timestamp = str(round(time.time() * 1000))
secret_enc = self.secret.encode('utf-8')
string_to_sign = f'{timestamp}\n{self.secret}'
string_to_sign_enc = string_to_sign.encode('utf-8')
hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
sign = base64.b64encode(hmac_code).decode('utf-8')
url = f"{self.webhook_url}×tamp={timestamp}&sign={sign}"
return url, {}
def send_text(self, content, at_mobiles=None, at_all=False):
"""
发送文本消息
"""
url, _ = self._sign()
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "text",
"text": {
"content": content
},
"at": {
"atMobiles": at_mobiles or [],
"isAtAll": at_all
}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
def send_markdown(self, title, text, at_mobiles=None, at_all=False):
"""
发送Markdown消息
"""
url, _ = self._sign()
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "markdown",
"markdown": {
"title": title,
"text": text
},
"at": {
"atMobiles": at_mobiles or [],
"isAtAll": at_all
}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
def send_link(self, title, text, message_url, pic_url=None):
"""
发送链接消息
"""
url, _ = self._sign()
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "link",
"link": {
"title": title,
"text": text,
"messageUrl": message_url,
"picUrl": pic_url if pic_url else ""
}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
# 使用示例
if __name__ == "__main__":
# 无密钥
bot = DingTalkBot("https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN")
# 有密钥(推荐使用)
# bot = DingTalkBot("https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN", "YOUR_SECRET")
# 发送文本消息
result = bot.send_text("Python测试消息")
print(result)
# 发送消息并@所有人
result = bot.send_text("重要通知!", at_all=True)
# 发送消息并@特定人
result = bot.send_text("请查看", at_mobiles=["138xxxx1234"])
# 发送Markdown
result = bot.send_markdown("标题", "# 内容\n**粗体**")
使用第三方库(更方便)
安装库:
pip install dingtalkchatbot
使用示例:
from dingtalkchatbot.chatbot import DingtalkChatbot
# 初始化机器人
webhook = "https://oapi.dingtalk.com/robot/send?access_token=YOUR_TOKEN"
bot = DingtalkChatbot(webhook)
# 发送文本消息
bot.send_text("Hello World!")
# 发送Markdown
bot.send_markdown(title="标题", text="# 内容")
# 发送链接
bot.send_link(title="链接", text="描述", message_url="http://example.com")
# @某人
bot.send_text("@某人", at_mobiles=["138xxxx1234"])
# @所有人
bot.send_text("通知所有人", is_at_all=True)
配置密钥安全验证
import requests
import json
import time
import hmac
import hashlib
import base64
def send_with_signature(webhook_url, secret, content):
"""
使用签名验证发送消息
"""
# 计算签名
timestamp = str(round(time.time() * 1000))
sign = base64.b64encode(
hmac.new(
secret.encode('utf-8'),
f'{timestamp}\n{secret}'.encode('utf-8'),
digestmod=hashlib.sha256
).digest()
).decode('utf-8')
# 拼接URL
url = f"{webhook_url}×tamp={timestamp}&sign={sign}"
headers = {'Content-Type': 'application/json'}
data = {
"msgtype": "text",
"text": {"content": content}
}
response = requests.post(url, headers=headers, data=json.dumps(data))
return response.json()
注意事项
- Webhook URL 获取:在钉钉群设置 → 智能群助手 → 添加机器人获取
- 安全设置:建议使用加签方式,避免URL泄露
- 频率限制:每个机器人每分钟最多发送20条消息限制**:文本消息最大5000字符,Markdown最大10000字符
- 错误处理:建议添加异常处理和重试机制
获取 Webhook 地址
- 打开钉钉群聊
- 点击群设置 → 智能群助手
- 添加机器人 → 自定义机器人
- 设置机器人名称和头像
- 配置安全设置(建议选择加签)
- 复制 Webhook 地址
这样你就可以用Python发送钉钉机器人消息了!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播