参数说明

参数

是否必填

说明

msgtype

消息类型,此时固定为image

base64

图片内容的base64编码

md5

图片内容(base64编码前)的md5值

效果展示

python推送企业微信机器人3-图片类型_json

Demo Code

# -*- coding: utf-8 -*-
# coding:unicode_escape
# Created on 2021年12月31日

# @author: LinHuang(Joker)

import requests
import json
import base64
import hashlib

base64_data =""
base64_data_MD5=""
# mian function
def main():
oWX_URL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=50d640d9-9ad6-4d39-98aa-c94c200212345'
sent_msg = {
"msgtype": "image",
"image": {
"base64": base64_data,
"md5": base64_data_MD5
}
}
headers = {'Content-Type': 'application/json'} # 指定提交的是json
try:
# 转换为json字符串
r = requests.post(url=oWX_URL, data=json.dumps(sent_msg), headers=headers, timeout=5)
except Exception as e:
print(e.args[0])
if __name__ == '__main__':
web_link = 'http://119.29.121.148/ui5/com.joker.shop/uimodule/webapp/img/ShopCarouselShipping.jpg'
web_content = requests.get(web_link).content
base64_data_MD5 = hashlib.md5(web_content).hexdigest()#通过hashlib库获取MD5值
base64_data = str(base64.b64encode(web_content),'utf-8')#获取base64
main()