当前位置:   article > 正文

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

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

其中,mentioned_listmentioned_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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

官方文档:https://developer.work.weixin.qq.com/document/path/91770


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/766106
推荐阅读
相关标签
  

闽ICP备14008679号