企微接口文档
发送应用消息 - 接口文档 - 企业微信开发者中心
发送格式
应用支持推送文本、图片、视频、文件、图文等类型。
~~~以下列举常用格式 示例~~~
1.发送文本
代码如下:
def sendtxt_robotmsg(self):# 正式keywx_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():# 调试keywx_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():#keywx_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():# 正式keywx_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
发送结果: