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
相关推荐
易辰君31 分钟前
【Python爬虫实战】正则:中文匹配与贪婪非贪婪模式详解
开发语言·爬虫·python
秀儿还能再秀34 分钟前
正则表达式核心语法 + Python的 re 库中常用方法
python·正则表达式
xcLeigh34 分钟前
Python入门:Python3 正则表达式全面学习教程
python·学习·正则表达式·教程·python3
多恩Stone1 小时前
【C++ debug】在 VS Code 中无 Attach 调试 Python 调用的 C++ 扩展
开发语言·c++·python
XW01059992 小时前
4-11判断素数
前端·python·算法·素数
深蓝电商API2 小时前
爬虫增量更新:基于时间戳与哈希去重
爬虫·python
两万五千个小时2 小时前
构建mini Claude Code:06 - Agent 如何「战略性遗忘」(上下文压缩)
人工智能·python
两万五千个小时2 小时前
构建mini Claude Code:12 - 从「文件冲突」到「分身协作」:Worktree 如何让多 Agent 安全并行
人工智能·python·架构
yuki_uix2 小时前
为什么我的 Auth Token 藏在了 Network 面板的 Doc 里?
前端·python·debug
甲枫叶3 小时前
【claude+weelinking产品经理系列16】数据可视化——用图表讲述产品数据的故事
java·人工智能·python·信息可视化·产品经理·ai编程