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

发送结果:

相关推荐
2601_9622932420 小时前
Python自动化统计团队工作量并生成可视化仪表盘的脚本方案【指导】
python·数据分析·自动化·可视化·仪表盘
点心的游戏开发世界21 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
2601_9622937921 小时前
OCRmyPDF批处理脚本:使用Python自动化复杂OCR任务
python·自动化·批处理脚本·ocrmypdf·ocr任务
事圆则缓1 天前
Kotlin 协程完全指南
开发语言·kotlin
我爱写代码i1 天前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
Java后端的Ai之路1 天前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
嘿嘿-661 天前
Windows 一键使用 GPT-6 Astra:Codex CLI 配置教程
java·人工智能·windows·gpt·chatgpt·web
2601_962077711 天前
基于Python与NLP的新闻事件信息抽取实战:从NER到时空标准化
python·nlp·transformers·spacy·新闻事件抽取
JavaPub-rodert1 天前
LangChain 从入门到 Agent 实战:用 Python 搭建一个真正能调用工具和知识库的 AI 助手
人工智能·python·langchain
统计学小王子1 天前
数学建模国赛倒计时4天——《统计模型精讲(混合效应模型R语言实现)》
开发语言·数学建模·r语言