官方文档:
发送图片
解决 :
{"errcode":301019,"errmsg":"media md5 not match, hint: [1684315383531332325922225], from ip: 222.71.242.114, more info at https://open.work.weixin.qq.com/devtool/query?e=301019"}
这个是瞎说,参考下文示例即可
import hashlib
import base64
import requests
import json
def sendBot(url, image_path):
'''
:param url: 传入企业微信机器人webhoot
:param image_path: 本地图片路径
:return:
'''
with open(image_path, "br") as f:
fcont = f.read()
# 转化图片的base64
ls_f = base64.b64encode(fcont)
# 计算图片的md5
fmd5 = hashlib.md5(fcont)
data = {"msgtype": "image", "image": {"base64": ls_f.decode('utf8'), "md5": fmd5.hexdigest()}}
data_json = json.dumps(data)
print('推送的json%s' % data_json)
prequte = requests.post(url, data=data_json)
return prequte.text
if __name__ == '__main__':
demo = sendBot('https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx', "2.png")
if json.loads(demo)['errcode'] == 0:
print('调用成功')
else:
print('调用失败%s' % demo)
参考:
workwechat_webhook/webhook_Image.py at master · xiaomu003/workwechat_webhook
其他示例
data ={
"msgtype": "markdown",
"markdown": {
"content": content
}
}
def send_wechat(webhook, data):
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
info = requests.post(url=webhook, json=data, headers=header)
print(info.text)
send_wechat(webhook,data)
注意:是json,不用二次转化了,这样好很多
标签:__,python,微信,image,base64,webhook,json,data From: https://www.cnblogs.com/ministep/p/17409456.html