python--发送文本消息、链接消息带图片到钉钉告警群

发送文本消息

def sendDingDing(self,text):
    headers = {'Content-Type': 'application/json'}
    webhook = "https://oapi.dingtalk.com/robot/send?access_token="+self.apps.access_token
    data = {
        "msgtype": "text",
        "text": {
            "content": text+'
'
        },
        "at": {
            "atMobiles": [

            ],
            "isAtAll": False
        }
    }

    x = requests.post(url=webhook, data=json.dumps(data), headers=headers)

发送链接消息

import json
import requests

def send_msg(url):
    headers = {'Content-Type': 'application/json;charset=utf-8'}
    data = {
        "msgtype": "link",
        "link": {
            "text": '这是标题',
            "title": "这是内容",
            "picUrl": "http://192.168.1.165:8000/static/images/fail.jpg",      
            "messageUrl": "https://fanyi.baidu.com/translate?aldtype=16047&query=&keyfrom=baidu&smartresult=dict&lang=auto2zh#auto/zh/"

        },
    }
    r = requests.post(url,data = json.dumps(data),headers=headers)
    return r.text
if __name__ == '__main__':
    url = 'https://oapi.dingtalk.com/robot/send?access_token={}'.formate(token)
    print(send_msg(url))

更多跳转官方:https://ding-doc.dingtalk.com/doc#/serverapi2/al5qyp

原文地址:https://www.cnblogs.com/lutt/p/12976797.html