Home > other >  Python push file in enterprise WeChat error
Python push file in enterprise WeChat error

Time:12-10

# - * - coding: utf-8 - * -
__author__='gg'
The import requests, json
The import urllib3
Urllib3. Disable_warnings ()
"" "
Python implementation enterprise WeChat push file
Note: support for Chinese name, etc.
"" "


The class WechatFile (object) :

Def get_token (self, corpid, secret) :
Url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send? Key=e48 d810a962-5-41 ab - "
Data={" corpid ": corpid, https://bbs.csdn.net/topics/
"Corpsecret" : the secret}
R=requests. Get (url=url, params=data, verify=False)
Token=r.j son () [' access_token ']
Return token

Def get_file_url (self, token, path) :
Url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send? Key=e48 d810a962-5-41 ab - b8e5 - & amp; Type=the file "% token
Data={" media ": the open https://bbs.csdn.net/topics/(path, 'rb')}
R=requests. Post (url=url, files=data)
Dict_data=(https://bbs.csdn.net/topics/r.json)
Return dict_data [' media_id]

Def send_news_message (self, user, path) :
Corpid='*'
Secret='*'
Agentid='*'
Token=self. Get_token (corpid, secret)
File_meida=self. Get_file_url (token, path)
Url="https://qyapi.weixin.qq.com/cgi-bin/webhook/send? Key=e48 d810a962-5-41 ab "% token
Data={" touser ": the user, https://bbs.csdn.net/topics/
"Agentid", agentid,
"Msgtype", "file",
"File" : {" media_id ": file_meida},
"Safe" : "0"}
The content-type headers={' ':' application/json '}
Data_dict=json. Dumps (data, ensure_ascii=False). The encode (' utf-8)
R=requests. Post (url=url, headers=headers, data=https://bbs.csdn.net/topics/data_dict)
Status=eval (r.t ext)
Print (status)
The return status


If __name__=="__main__ ':
User="gg"
Path="C:/Users/0061599/Desktop/piac A.x LSX"
Wechat_file=WechatFile ()
Wechat_file. Send_news_message (user, path)



Refer to
  • Related