在办公自动化中有一个场景会经常用到,及时使用企业微信机器人发哦那个图片,这个我将范例分享给大家。

import requests

def send_image_to_wechat(image_path, robot_key, proxy_host, proxy_port):
    with open(image_path, 'rb') as file:
        data = file.read()
        encodestr = base64.b64encode(data)
        image_data = str(encodestr, 'utf-8')

    with open(image_path, 'rb') as file:
        md = hashlib.md5()
        md.update(file.read())
        image_md5 = md.hexdigest()

    url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={robot_key}"
    headers = {
        "Content-Type": "application/json",
        "User-Agent": "Your User-Agent String"
    }
    data = {
        "msgtype": "image",
        "image": {
            "base64": image_data,
            "md5": image_md5
        }
    }

    proxies = {
        "http": f"http://{proxy_host}:{proxy_port}",
        "https": f"http://{proxy_host}:{proxy_port}"
    }

    result = requests.post(url, headers=headers, json=data, proxies=proxies)
最后修改:2023 年 07 月 21 日
如果觉得我的文章对你有用,请随意赞赏