Python 发送钉钉消息(markdown格式)

send.py

python 复制代码
import requests
import json
from config import *

def sendDing():
    """
    给钉钉发送消息,使用markdown格式在发送的消息后,需要手动@一次
    """
    text = msg + ' '.join([f"@{i}" for i in at_list])
    data = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": text
        },
        "at": {
            "atMobiles": at_list, "isAtAll": isAtAll
        }
    }
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    send_data = json.dumps(data).encode('utf-8')
    result = requests.post(url=token, data=send_data, headers=headers)
    # print(result.json())
    # 判断发送是否成功
    if result.json()['errcode'] == 0:
        print('发送成功')
    else:
        print('发送失败')


if __name__ == '__main__':
    sendDing()

config.py

python 复制代码
token = "https://oapi.dingtalk.com/robot/send?access_token=" \
       "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"     # 钉钉机器人的token

title = "发送的主题名称"

# @群成员的手机号
at_list = ['181xxxxxxxx', '181xxxxxxxx']

# 是否@所有人
isAtAll = False

# 消息内容
msg = "消息内容\n\n"

执行 send.py脚本

shell 复制代码
python3 send.py
相关推荐
Ulyanov26 分钟前
卡尔曼滤波技术博客系列:第四篇:多目标跟踪:数据关联与航迹管理
python·目标跟踪·系统仿真·雷达电子战·仿真引擎
Three~stone36 分钟前
MATLAB vs Python 两者区别和安装教程
开发语言·python·matlab
soragui1 小时前
【Python】第 1 章:Python 解释器原理
开发语言·python
Ulyanov1 小时前
卡尔曼滤波技术博客系列:第三篇 雷达目标跟踪:运动模型与坐标转换
python·目标跟踪·系统仿真·雷达电子战
nimadan121 小时前
生成剧本杀软件2025推荐,创新剧情设计工具引领潮流
人工智能·python
极光代码工作室1 小时前
基于深度学习的智能垃圾分类系统
python·深度学习·神经网络·机器学习·ai
MediaTea1 小时前
Pandas 操作指南(二):数据选取与条件筛选
人工智能·python·机器学习·数据挖掘·pandas
小陈工1 小时前
Python Web开发入门(十二):使用Flask-RESTful构建API——让后端开发更优雅
开发语言·前端·python·安全·oracle·flask·restful
无心水1 小时前
20、Spring陷阱:Feign AOP切面为何失效?配置优先级如何“劫持”你的设置?
java·开发语言·后端·python·spring·java.time·java时间处理
夜雨飘零12 小时前
零门槛!用 AI 生成 HTML 并一键部署到云端桌面
人工智能·python·html