企业微信机器人发送文本、图片、文件、markdown、图文信息

import requests
import base64
import hashlib
import json
# 机器人地址的key值
key="811a1652-60e8-4f51-a1d9-231783399ad2"
def path2base64(path):
    """
    文件转换为base64
    :param path: 文件路径
    :return:
    """
    with open(path, "rb") as f:
        byte_data = f.read()
    base64_str = base64.b64encode(byte_data).decode("ascii")    # 二进制转base64
    return base64_str
def path2md5(path):
    """
    文件转换为md5
    :param path: 文件路径
    :return:
    """
    with open(path, "rb") as f:
        byte_data = f.read()
    md5_str = md5(byte_data)
    return md5_str


def md5(text):
    """
    md5加密
    :param text:
    :return:
    """
    m = hashlib.md5()
    m.update(text)
    return m.hexdigest()

# 上传文件
def upload_file(file_path):
    """
    企业微信机器人上传文件,发送文件前需要现上传
    :param file_path: 文件路径
    :return:
    """

    data = {'file': open(file_path, 'rb')}
    resp = requests.post(f"https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={key}&type=file", files=data)
    json_res = resp.json()
    print(json_res)
    if json_res.get('errcode') == 0:
        print(f"企业微信机器人上传文件成功,file:{file_path}")
        return json_res.get('media_id')
    else:
        print(f"企业微信机器人上传文件失败,file:{file_path}")
        return json.loads(resp.text)


class QY:
    def __init__(self):
        self.url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={key}"
    # 发送图片
    def image(self,image):
        data={
            "msgtype": "image",
            "image": {
                "base64":path2base64(image),
                "md5":path2md5(image)
        }}

        resp=requests.post(url=self.url,json=data)
        print(resp.text)
        return json.loads(resp.text)
    # 发送文件
    def send_file(self,file):
        media_id=upload_file(file)
        data={
            "msgtype": "file",
            "file":{
                "media_id":media_id
            }
        }
        resp=requests.post(url=self.url,json=data)
        print(resp.text)
        return json.loads(resp.text)
    # 发送文本
    def send_text(self,text):
        data={
            "msgtype": "text",
            "text":{
                "content":text
            }
        }
        resp=requests.post(url=self.url,json=data)
        print(resp.text)
        return json.loads(resp.text)
    # 发送markdown文本
    def markdown(self,markdown):
        data={
            "msgtype": "markdown",
            "markdown": {
                "content": '''实时新增用户反馈<font color=\"warning\">132例</font>,请相关同事注意。\n
                 >类型:<font color=\"comment\">用户反馈</font>
                 >普通用户反馈:<font color=\"comment\">117例</font>
                 >VIP用户反馈:<font color=\"comment\">15例</font>'''
            }
        }
        resp=requests.post(url=self.url,json=data)
        print(resp.text)
        return json.loads(resp.text)
    # 发送图文类型
    def news(self,news):
        data={
                "msgtype": "news",
                "news": {
                   "articles" : [
                       {
                           "title" : "中秋节礼品领取",
                           "description" : "今年中秋节公司有豪礼相送",
                           "url" : "www.qq.com",
                           "picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"
                       }
                    ]
                }
            }
        resp=requests.post(url=self.url,json=data)
        print(resp.text)
        return json.loads(resp.text)



if __name__ == '__main__':
    qy = QY()
    # qy.image("C:/Users/Administrator/Pictures/Saved Pictures/1.jpg")
    # 发送文件
    # qy.send_file("main.py")
    # 发送文本
    # qy.send_text("hello world")
    # 发送markown文本
    # qy.markdown("")
    #发送图文信息
    # qy.news('') 作者:浅若红尘 https://www.bilibili.com/read/cv28561312/?spm_id_from=333.999.0.0 出处:bilibili

有事情,请到哔哩哔哩

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅若红尘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值