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
相关推荐
战略性的菠萝13 分钟前
研究生基础-Python快速入门(终)——面向对象
python
陈皮波比茶18 分钟前
Python项目实战-网络机器人(爬虫)
开发语言·网络·爬虫·python·机器人
winfredzhang1 小时前
从零构建家庭照片管理系统:Django 模块化单体实战,以及我踩到的 4 个真实坑
python·postgresql·django·sqlite·bootsrap
jyOverQ1 小时前
LangGraph 流式执行详解:stream、astream 与 stream_mode 怎么选?
python·langchain
闲猫1 小时前
LangGraph / Capabilities / Stores
python·agent·langgraph
pjj198542 小时前
机器学习-NumPy2
人工智能·python·机器学习
OPEN-F2 小时前
Python进阶教程:单元测试与代码质量
开发语言·python·单元测试
Logintern092 小时前
装饰器和洋葱模式的区分
开发语言·python·架构
呆呆敲代码的小Y3 小时前
10 分钟搞懂 cua:开源 AI 操作电脑基础设施,附 Python 沙箱与 Agent 上手代码
人工智能·python·开源·ai agent·awesome·llm应用·cua
工具分享3 小时前
带店托管必用爆单AI选品,一人公司轻松掌握
人工智能·python