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
相关推荐
夏幻灵2 分钟前
Java中的this关键字解析与应用
java·开发语言·python
LetsonH5 分钟前
Swap 大小一键调整脚本
人工智能·python
半路_出家ren16 分钟前
5.RSA和AES加密(python)
服务器·网络·python·https·aes·rsa·加密算法
SunnyRivers17 分钟前
10分钟入门Python 异步编程
python·异步·asyncio·asyn·asynhttp
m0_6860416129 分钟前
使用Scikit-learn构建你的第一个机器学习模型
jvm·数据库·python
敲上瘾36 分钟前
用Coze打造你的专属AI应用:从智能体到Web部署指南
前端·人工智能·python·阿里云·aigc
开发者小天42 分钟前
python查询天气小示例
开发语言·python
知行合一。。。43 分钟前
Python--04--数据容器(元组)
开发语言·python
2401_838472511 小时前
构建一个桌面版的天气预报应用
jvm·数据库·python
hmywillstronger1 小时前
【Rhino】【Python】对包含特定关键词的文字的MTEXT对象添加指定内容
linux·服务器·python