Python调用企微机器人: 发送常用格式汇总

企微接口文档

发送应用消息 - 接口文档 - 企业微信开发者中心

发送格式

应用支持推送文本、图片、视频、文件、图文等类型。

~~~以下列举常用格式 示例~~~

1.发送文本

代码如下:

复制代码
def sendtxt_robotmsg(self):
     # 正式key
     wx_key = "xx"
     wx_webhookurl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}'.format(wx_key)
     headers = {'Content-Type': 'application/json'}
     //发送消息时在企微自动@的人:xx值必须为同事注册企微的手机号或姓名
	 hbpeople = ["xx"]
     msg = '请关注'
     testdata = json.dumps({"msgtype": "text", "text": {"content": msg, "mentioned_list": hbpeople}})
     r = requests.post(wx_webhookurl, data=testdata, headers=headers, verify=False)
     print(r.text)
     return r

发送结果:

2.发送图片

代码如下:

图片存放在这里:

复制代码
#图片所在文件夹路径
SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
GENERATECASE_DIR = os.path.join(SCRIPTS_DIR, "xx")
DATAS_DIR = os.path.join(GENERATECASE_DIR, "xx")

def sendimage_robotmsg():
    # 调试key
    wx_key = "xx"
    wx_webhookurl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}'.format(wx_key)
    headers = {'Content-Type': 'application/json'}
    #图片(base64编码前)最大不能超过2M,支持JPG,PNG格式
    imgPath = os.path.join(DATAS_DIR, "有福气.png")
    with open(imgPath,"rb") as f:
        fd=f.read()
        base64Content=str(base64.b64encode(fd),"utf-8")
    with open(imgPath,"rb") as f:
        fd=f.read()
        md = hashlib.md5()
        md.update(fd)
        md5Content = md.hexdigest()
    testdata = {"msgtype": "image","image": {"base64": base64Content,"md5": md5Content}}
    r = requests.post(wx_webhookurl, headers=headers, json=testdata)
    print(r.text)
    return r

发送结果:

3.发送文件

文件路径:

代码如下:

复制代码
SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
GENERATECASE_DIR = os.path.join(SCRIPTS_DIR, "xx")
DATAS_DIR = os.path.join(GENERATECASE_DIR, "xx")


def upload_weixin(key=None, filename=None):
    """
    上传附件到企业微信,获得media_id.然后发送消息通知,可查看文件
    """
    if not key:
        print("key不能为空")
        raise
    # 请求地址
    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={}&type=file".format(key)
    # 请求头
    headers = {"Content-Type": "multipart/form-data"}
    # 请求数据,是rb读取文件流
    data = {"file": open(filename, "rb")}
    # 发送请求
    res = requests.post(url, files=data, headers=headers).json()
    return res.get("media_id")

def sendfile_robotmsg():
    #key
    wx_key = "xx"
    wx_webhookurl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}'.format(wx_key)
    headers = {'Content-Type': 'application/json'}
    filename = os.path.join(DATAS_DIR, "xx.html")
    print(filename)
    medid= upload_weixin(wx_key,filename)
    data = {
        "msgtype": "file",
        "file": {
            "media_id": medid
        }
    }
    print(medid)
    r = requests.post(url=wx_webhookurl,headers=headers, json=data)

    print(r.text)
    return r

发送结果:

4.发送图文

文件路径:

代码如下:

复制代码
SCRIPTS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
GENERATECASE_DIR = os.path.join(SCRIPTS_DIR, "xx")
DATAS_DIR = os.path.join(GENERATECASE_DIR, "xx")


def sendimagetext_robotmsg():
    # 正式key
    wx_key = "xx"
    wx_webhookurl = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}'.format(wx_key)
    headers = {'Content-Type': 'application/json'}
    imgPath = os.path.join(DATAS_DIR, "有福气.png")

    testdata = {
   "msgtype" : "news",
   "agentid" : 1,
   "news" : {
       "articles" : [
           {
               "title" : "测试一下",
               "description" : "测试",
               "url" : "URL",
               "picurl":"",
        	   "pagepath": imgPath
           }
        ]
   },
   "enable_id_trans": 0,
   "enable_duplicate_check": 0,
   "duplicate_check_interval": 1800
}
    r = requests.post(wx_webhookurl, headers=headers, json=testdata)
    print(r.text)
    return r

发送结果:

相关推荐
金色熊族26 分钟前
装饰器模式(c++版)
开发语言·c++·设计模式·装饰器模式
七夜zippoe40 分钟前
仓颉语言核心特性深度解析——现代编程范式的集大成者
开发语言·后端·鸿蒙·鸿蒙系统·仓颉
四谎真好看1 小时前
Java 黑马程序员学习笔记(进阶篇21)
java·开发语言·笔记·学习·学习笔记
初见无风1 小时前
2.7 Lua代码中的可变参数
开发语言·lua·lua5.4
七月稻草人1 小时前
Rust 应用状态(App State)管理:类型安全与并发控制的艺术
开发语言·安全·rust
PONY LEE1 小时前
Flink keyby使用随机数踩坑记
大数据·python·flink
喆星时瑜1 小时前
Windows图标修复--缓存重建教程
windows·缓存
无敌最俊朗@1 小时前
SQLite 约束 (Constraints) 面试核心知识点
java·开发语言·jvm
一只小松许️2 小时前
量化投资从入门到入土:金融基础概念
python·金融