python 钉钉 自动发送文件 您所在的位置:网站首页 excel如何把文件发送至微信群 python 钉钉 自动发送文件

python 钉钉 自动发送文件

2023-08-12 04:43| 来源: 网络整理| 查看: 265

钉钉机器人只允许发送文字,markdown等消息,不支持media,所以要自己开发个小程序,不过很简单。

在这里插入图片描述在这里插入图片描述在这里插入图片描述

服务 出口ip的设置要注意了

到这个网址获取

http://myip.fireflysoft.net/

说明:出口ip需要定时到这个网址获取修改

添加发送文件到群的权限

添加企业会话即可 在这里插入图片描述

开始写代码 注意:字符串星号要修改为你自己的,文末说明方法 import requests import json def getAccess_token(): appkey = '****' appsecret = '****' url = 'https://oapi.dingtalk.com/gettoken?appkey=%s&appsecret=%s' % (appkey, 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 = './helloworld.txt' # 文件地址 url = 'https://oapi.dingtalk.com/media/upload?access_token=%s&type=file' % access_token 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()) SendFile() app key secret

在这里插入图片描述在这里插入图片描述

chatid的获取 https://wsdebug.dingtalk.com/

到上面这个网址,用手机钉钉扫网页上的码,搜索biz.chat.chooseConversationByCorpId,然后就修改corpid为你的,点击执行后,手机上会弹出要你选择你要发送文件的那个群,选择群后就会返回这个群的chatid

在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有