程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python sends files to enterprise wechat

編輯:Python

python Send files to enterprise wechat

purpose

Send files to enterprise wechat , It can be used to send scheduled reports, etc .

python Script

File name :wechat_send_file.py

#!/usr/bin/python3
#--coding: utf-8--
import os
import json
import urllib3
import sys
file_path = sys.argv[1] # File path 
class WinxinApi(object):
def __init__(self,corpid,secret,agentid,touser):
self.secret = secret # Enterprise wechat application voucher 
self.corpid = corpid # Enterprise WeChat id
self.agentid = agentid # application Agentid
self.touser = touser # To receive a message userid
self.http = urllib3.PoolManager()
def __get_token(self):
'''token obtain '''
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(self.corpid,self.secret)
r = self.http.request('GET', url)
req = r.data.decode("utf-8")
data = json.loads(req)
if data["errcode"] == 0:
return data['access_token']
else:
raise data["errmsg"]
def __upload_file(self,file_path,type='file'):
''' Upload temporary files '''
if not os.path.exists(file_path):
raise ValueError("{}, file does not exist ".format(file_path))
file_name = file_path.split("\\")[-1]
token = self.__get_token()
with open(file_path,'rb') as f:
file_content = f.read()
files = {
'filefield': (file_name, file_content, 'text/plain')}
url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type={}".format(token,type)
r = self.http.request('POST', url,fields=files)
req = r.data.decode("utf-8")
data = json.loads(req)
if data["errcode"] == 0:
return data['media_id']
else:
raise data["errmsg"]
def send_file_message(self,file_path):
token = self.__get_token()
media_id = self.__upload_file(file_path)
body = {

"agentid" : self.agentid, # agentid
"touser" : self.touser,
"msgtype" : "file", # Message type , Fixed as :file
"file" : {
"media_id" : media_id},# file id, You can call the upload temporary material interface to obtain 
"safe":0 # Indicates whether it is a confidential message ,0 Indicate no ,1 Said is 
}
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(token)
bodyjson = json.dumps(body)
r = self.http.request('POST', url, body=bodyjson)
req = r.data.decode("utf-8")
data = json.loads(req)
if data["errcode"] == 0:
print(" Sending files to enterprise wechat succeeded ")
else:
raise data["errmsg"]
wechat = WinxinApi("***corpid***","***secret***","***agentid***","***touser***")
wechat.send_file_message(file_path)

Call mode

./wechat_send_file.py myfile.txt

exception handling

# If the prompt is missing during execution urllib3:
File "./wechat_send_file.py", line 5, in <module>
import urllib3
ModuleNotFoundError: No module named 'urllib3'
# terms of settlement :
yum install -y python3-pip
pip3 install urllib3

— end —


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved