天天看點

python+釘釘API實作發送檔案消息

python3.7+釘釘API實作自動發送檔案消息

需求:每天固定時間需要向釘釘群推送資料檔案

1.由于釘釘群機器人推送消息類型隻支援text,link,markdown等形式,并不支援檔案和圖檔類媒體檔案的推送,是以咱們這裡就需要用到釘釘的API接口來實作了,進入釘釘開放平台:

python+釘釘API實作發送檔案消息

在這裡咱們是可以看到支援檔案類消息的,下面怎麼實作呢

2.群消息的推送是需要三個參數的,access_token(調用接口憑證),chatid(群會話id,可以通過jsapi工具擷取,後邊詳情介紹),msg(步驟1),咱們一個一個參數來看

python+釘釘API實作發送檔案消息

3.access_token:

def getAccess_token():
    url = 'https://oapi.dingtalk.com/gettoken?appkey=********&appsecret=*******'
    appkey = '*********' # 管理者賬号登入開發者平台,應用開發-建立應用-檢視詳情-appkey
    appsecret = '********' # 應用裡的appsecret
    headers = {
        'Content-Type': "application/x-www-form-urlencoded"
    }
    data = {'appkey': appkey,
            'appsecret': appsecret}
    r = requests.request('GET', url, data=data, headers=headers)
    access_token = r.json()["access_token"]
    return access_token
           

4.msg中的media_id:

釘釘推送媒體檔案需要先上傳至媒體檔案中,生成一個獨立的media_id,然後進行調用才可以

python+釘釘API實作發送檔案消息

access_token和type兩個參數都顯然沒有問題,這個media_id怎麼傳遞呢,它需要的是檔案名為media,并且包含filelength、content-type等資訊,咱們可以把需要上傳的檔案放在files的參數裡來實作

def getMedia_id():
    access_token = getAccess_token()  # 拿到接口憑證
    path = '*********'  # 檔案路徑
    url = 'https://oapi.dingtalk.com/media/upload?access_token=' + access_token + '&type=file'
    files = {'media': open(path, 'rb')}
    data = {'access_token': access_token,
            'type': 'file'}
    response = requests.post(url, files=files, data=data)
    json = response.json()
    return json["media_id"]
           

是不是發現沒有headers,在網上看到過很多例子都将檔案以multipart/form-data形式把資料放在了headers裡面,結果你發現人家壓根不讀你的頭資訊,你說氣不氣,這也是這文檔坑的地方了,檔案也上傳了,media_id咱們也拿到了,接下來就可以愉快的推送檔案了

5.推送消息至釘釘群:

def SendFile():
    access_token = getAccess_token()
    media_id = getMedia_id()
    chatid = '********'  # 通過jsapi工具擷取的群聊id
    url = 'https://oapi.dingtalk.com/chat/send?access_token=' + access_token
    header = {
        'Content-Type': 'application/json'
    }
    data = {'access_token': access_token,
            'chatid': chatid,
            'msg': {
                'msgtype': 'file',
                'file': {'media_id': media_id}
            }}
    r = requests.request('POST', url, data=json.dumps(data), headers=header)
    print(r.json())
           

6.完整代碼:

# -*- coding: utf-8 -*-
import requests
import json


def getAccess_token():
    url = 'https://oapi.dingtalk.com/gettoken?appkey=********&appsecret=*******'
    appkey = '*********' # 管理者賬号登入開發者平台,應用開發-建立應用-檢視詳情-appkey
    appsecret = '********' # 應用裡的appsecret
    headers = {
        'Content-Type': "application/x-www-form-urlencoded"
    }
    data = {'appkey': appkey,
            'appsecret': appsecret}
    r = requests.request('GET', url, data=data, headers=headers)
    access_token = r.json()["access_token"]
    return access_token


def getMedia_id():
    access_token = getAccess_token() # 拿到接口憑證
    path = '*********' # 檔案路徑
    url = 'https://oapi.dingtalk.com/media/upload?access_token=' + access_token + '&type=file'
    files = {'media': open(path, 'rb')}
    data = {'access_token': access_token,
            'type': 'file'}
    response = requests.post(url, files=files, data=data)
    json = response.json()
    return json["media_id"]


def SendFile():
    access_token = getAccess_token()
    media_id = getMedia_id()
    chatid = '*******'# 通過jsapi工具擷取的群聊id
    url = 'https://oapi.dingtalk.com/chat/send?access_token=' + access_token
    header = {
        'Content-Type': 'application/json'
    }
    data = {'access_token': access_token,
            'chatid': chatid,
            'msg': {
                'msgtype': 'file',
                'file': {'media_id': media_id}
            }}
    r = requests.request('POST', url, data=json.dumps(data), headers=header)
    print(r.json())


if __name__ == '__main__':
    SendFile()

           

7.chatid的擷取:

進入JSAPI控制台https://wsdebug.dingtalk.com,手機釘釘掃碼登入(主管理者),往下劃,執行v0.1.2中的biz.chat.chooseConversationByCorpId,這時手機會彈出選擇群聊,選擇你需要擷取chatid的群,JSAPI控制台就會出現chatid和群聊名稱。

python+釘釘API實作發送檔案消息

語音和圖檔類消息同道理,鄙人菜鳥一枚,才疏學淺,希望路過的各位大佬希望不吝賜教!

    對python感興趣的朋友們可以掃碼關注公衆号,會不定時更新一些python基礎、網絡爬蟲、資料分析等知識,也歡迎大家前來探讨交流,公衆号加星标,不迷路!

python+釘釘API實作發送檔案消息