Python自动化之企业微信机器人




1、发送文本

1.1、发送文本消息

def send_text(webhook, content, mentioned_list=None, mentioned_mobile_list=None):
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    data = {
        "msgtype": "text",
        "text": {
            "content": content,
            "mentioned_list": mentioned_list,
            "mentioned_mobile_list": mentioned_mobile_list
        }
    }
    data = json.dumps(data)
    info = requests.post(url=webhook, data=data, headers=header)

其中,mentioned_list、mentioned_mobile_list表示需要艾特哪些成员,user_id列表、手机号列表类型,艾特所有人:@all

1.2、发送Markdown消息

content = """
### **提醒!实时新增用户反馈**:<font color="warning">**123例**</font>\n
#### **请相关同事注意,及时跟进!**\n
[点击链接](https://work.weixin.qq.com/api/doc) \n
> 类型:<font color="info">用户反馈</font> \n
> 普通用户反馈:<font color="warning">117例</font> \n
> VIP用户反馈:<font color="warning">6例</font>
"""

'''目前只支持3种内置颜色
<font color="info">绿色</font>
<font color="comment">灰色</font>
<font color="warning">橙色</font>
'''

def send_md(webhook, content):
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    data = {
        "msgtype": "markdown",
        "markdown": {
            "content": content
        }
    }
    data = json.dumps(data)
    info = requests.post(url=webhook, data=data, headers=header)

2、发送图文

一个图文消息支持1到8条图文

articles = [
    {
        "title": "xxx",        # 标题,不超过128个字节,超过会自动截断
        "description": "xxx",  # 描述,不超过512个字节,超过会自动截断
        "url": "url",          # 点击后跳转的链接
        "picurl": "img-url"    # 图文消息中的图片链接,支持JPG、PNG格式,较好的效果为大图1068*455,小图150*150
    }
]

def send_img_txt(webhook, articles):
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    data = {
        "msgtype": "news",
        "news": {
            "articles": articles
        }
    }
    data = json.dumps(data)
    info = requests.post(url=webhook, data=data, headers=header)

3、发送文件

企业微信支持推送文件,首先将文件上传至企业微信指定的地址,然后返回media_id;文件应小于20M,且media_id有效时间为三天

def send_file(webhook, file):
    # 获取media_id
    key = webhook.split('key=')[1]
    id_url = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={key}&type=file'
    files = {'file': open(file, 'rb')}
    res = requests.post(url=id_url, files=files)
    media_id = res.json()['media_id']
    # 发送文件
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    data = {
        "msgtype": "file",
        "file": {
            "media_id": media_id
        }
    }
    # info = requests.post(url=webhook, json=data, headers=header) 或
    data = json.dumps(data)
    info = requests.post(url=webhook, data=data, headers=header)

key = 'robot_id'
webhook = rf'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={key}'
file = r'文件路径'
send_file(webhook, file)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值