小陆同学

python 中文名:蟒蛇,设计者:Guido van Rossum

导航

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+'\n'
        },
        "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

posted on 2020-05-27 21:40  小陆同学  阅读(1525)  评论(0编辑  收藏  举报