Python / 实践 · 2020年7月4日 1

python实现推送消息到钉钉机器人

阅读了官方文档,整合到了一个文件

#  Copyright (c) 2020.
#  作者:sky
#  邮箱:ztianb@foxmail.com
#  日期:2020-06-16 09:44
#  ---
#  项目:test
#  文件名:to钉钉机器人.py
#  路径:D:/pythonworkspace/test/to钉钉机器人.py
#  Copyright (c) 2020
import json

import requests

url = r"https://oapi.dingtalk.com/robot/send?access_token="
headers = {
    "Content-Type": "application/json",
    "X-Forwarded-For": "123.57.151.56"
}


def textData(content, at, isAtAll):
    data = {
        "msgtype": "text",
        "text": {
            "content": content
        },
        "at": {
            # "atMobiles": [
            #     "156xxxx8827",
            #     "189xxxx8325"
            # ],
            "isAtAll": isAtAll
        }
    }
    return data


def linkData(title, content, picUrl, messageUrl):
    data = {
        "msgtype": "link",
        "link": {
            "text": content,
            "title": title,
            "picUrl": picUrl,
            "messageUrl": messageUrl
        }
    }
    return data


def markdownData(title, content, at, isAtAll):
    data = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": content
        },
        "at": {
            "atMobiles": at,
            "isAtAll": isAtAll
        }
    }
    return data


def ZTActionCardData(title, content, singleURL, picUrl):
    data = {
        "actionCard": {
            "title": title,
            "text": "![screenshot](" + picUrl + ")" + content,
            "btnOrientation": "0",
            "singleTitle": "阅读全文",
            "singleURL": singleURL
        },
        "msgtype": "actionCard"
    }
    return data


def DLActionCardData(title, content, singleURL, picUrl):
    data = {
        "actionCard": {
            "title": title,
            "text": "![screenshot](" + picUrl + ")" + content,
            "btnOrientation": "0",
            "btns": [
                {
                    "title": "内容不错",
                    "actionURL": "https://www.dingtalk.com/"
                },
                {
                    "title": "不感兴趣",
                    "actionURL": "https://www.dingtalk.com/"
                }
            ]
        },
        "msgtype": "actionCard"
    }
    return data


def FeedCardData(links):
    data = {
        "feedCard": {
            "links": links
        },
        "msgtype": "feedCard"
    }
    return data


def dingTalk():
    # data = markdownData("aa", "#### 杭州天气 \n", [], True)
    data = ZTActionCardData("整体跳转卡片测试","这是个测试","https://www.dingtalk.com/","https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png")
    data = FeedCardData([{
                "title": "时代的火车向前开",
                "messageURL": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                "picURL": "https://gw.alicdn.com/tfs/TB1ayl9mpYqK1RjSZLeXXbXppXa-170-62.png"
            },
            {
                "title": "时代的火车向前开2",
                "messageURL": "https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
                "picURL": "https://gw.alicdn.com/tfs/TB1ayl9mpYqK1RjSZLeXXbXppXa-170-62.png"
            }])
    json_data = json.dumps(data)
    response = requests.post(url=url, data=json_data, headers=headers)
    print(response)


dingTalk()