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
相关推荐
bitbrowser36 分钟前
哪些指纹浏览器支持模拟SSL指纹
网络·python·网络协议·ssl
limenga1022 小时前
TensorFlow Keras:快速搭建神经网络模型
人工智能·python·深度学习·神经网络·机器学习·tensorflow
心软小念2 小时前
用Python requests库玩转接口自动化测试!测试工程师的实战秘籍
java·开发语言·python
sanggou3 小时前
【Python爬虫】手把手教你从零开始写爬虫,小白也能轻松学会!(附完整源码)
开发语言·爬虫·python
geng_zhaoying4 小时前
在VPython中使用向量计算3D物体移动
python·3d·vpython
半tour费4 小时前
TextCNN-NPU移植与性能优化实战
python·深度学习·分类·cnn·华为云
普通网友4 小时前
使用Flask快速搭建轻量级Web应用
jvm·数据库·python
百锦再5 小时前
第17章 模式与匹配
开发语言·后端·python·rust·django·内存·抽象
普通网友5 小时前
Python函数定义与调用:编写可重用代码的基石
jvm·数据库·python
普通网友5 小时前
使用Python进行PDF文件的处理与操作
jvm·数据库·python