需求:
我們希望將圖片通過機(jī)器人快速發(fā)到對應(yīng)的群內(nèi)栅哀。
話不多說氏义,直接看代碼:
import requests
import base64
import hashlib
#通過企業(yè)微信發(fā)送圖片文件
def Webhook地址():
with open('Webhook地址.txt', 'r', encoding='utf8') as file:
return file.read()
def wx_image(image,url):
with open(image, 'rb') as file: # 轉(zhuǎn)換圖片成base64格式
data = file.read()
encodestr = base64.b64encode(data)
image_data = str(encodestr, 'utf-8')
with open(image, 'rb') as file: # 圖片的MD5值
md = hashlib.md5()
md.update(file.read())
image_md5 = md.hexdigest()
headers = {"Content-Type": "application/json"}
data = {
"msgtype": "image",
"image": {
"base64": image_data,
"md5": image_md5
}
}
result = requests.post(url, headers=headers, json=data)
return result
if __name__ == '__main__':
url = Webhook地址()
wx_image('picture.png',url) # 傳入圖片路徑
- 新建<Webhook地址.txt>
放入自己的機(jī)器人地址
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=
- 隨便一張圖片衡创,將其命名為-picture.png 即可俄烁。
當(dāng)然它呀,我也將其進(jìn)行了封裝苞也,你可以直接使用:
下載地址:
·https://wwkg.lanzoum.com/ipBNV0uyvdba 密碼:6y45