开发文档
官方开发文档
文本类型
直接发送消息及通知人即可
def send_text_msg(bot_key, msg, mentioned_list=None, mentioned_mobile_list=None, at_all=False):'''向指定机器人发送文本消息:param bot_key: 企微机器人 key:param msg: 文本消息:param mentioned_list: 通知人:param mentioned_mobile_list::param at_all::return:'''if mentioned_list is None:mentioned_list = []if mentioned_mobile_list is None:mentioned_mobile_list = []if at_all:mentioned_mobile_list.append('@all')url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send'params = {'key': bot_key}data = {'msgtype': 'text','text': {'content': msg,'mentioned_list': mentioned_list,'mentioned_mobile_list': mentioned_mobile_list}}resp = requests.post(url, params=params, json=data)return resp.json()
markdown类型
看了一下和文本类型没啥区别,除了 msgtype
, 其他参数基本一致,完全可以复用之前的文本消息,这里对文本消息简单处理一下,以便可以发送 markdown类型 消息
# 添加一个 msg_type 表示消息类型
def send_msg(bot_key, msg_type, msg=None, mentioned_list=None, mentioned_mobile_list=None, at_all=False):'''向指定机器人发送文本消息:param bot_key: 企微机器人 key:param msg: 消息内容:param mentioned_list: 通知人:param mentioned_mobile_list::param at_all::return:'''if not bot_key:raise ValueError('bot_key must be provided')if msg_type not in ['text', 'markdown']:raise ValueError('msg_type must be text or markdown')if not msg:raise ValueError('msg must be provided')if mentioned_list is None:mentioned_list = []if mentioned_mobile_list is None:mentioned_mobile_list = []if at_all:mentioned_mobile_list.append('@all')url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send'params = {'key': bot_key}data = {'msgtype': msg_type,msg_type: {'content': msg,'mentioned_list': mentioned_list,'mentioned_mobile_list': mentioned_mobile_list}}resp = requests.post(url, params=params, json=data)return resp.json()
图片类型
直接获取图片字节流,然后进行base64编码和md5加密即可
def send_image_msg(bot_key, file_path):url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send'params = {'key': bot_key}with open(file_path, 'rb') as f:f_read = f.read()img_md5 = hashlib.md5(f_read).hexdigest()img_base64 = base64.b64encode(f_read).decode('utf-8')data = {"msgtype": "image","image": {"base64": img_base64,"md5": img_md5}}resp = requests.post(url, params=params, json=data)return resp.json()
文件类型
文件类型需要先把文件上传到服务器上才能发送
def send_file_msg(bot_key, file_path, show_name=None):'''上传并发送文件消息:param bot_key: 机器人 key:param file_path: 文件路径:param show_name: 上传后在消息中的显示名称'''if not os.path.isfile(file_path):raise ValueError('File does not exist')if show_name is None:show_name = os.path.basename(file_path)wx_upload_url = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media'wx_upload_params = {'key': bot_key,'type': 'file'}response = requests.post(wx_upload_url,params=wx_upload_params,files={'file': (show_name, open(file_path, 'rb'))})upload_response = response.json()if upload_response['errcode'] != 0:return upload_responseelse:media_id = upload_response.get('media_id')url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send'params = {'key': bot_key}response = requests.post(url, params=params, json={'msgtype': 'file', 'file': {'media_id': media_id}})return response.json()
语音类型
语音和文件类似,简单修改下就好了,修改的方式和上面的一致
pypi 包
pip install ics-utils
调用方法极其简单
from ics_utils import WxWorkwx_work = WxWork(bot_key='')
wx_work.send_text_msg(msg='hello, world!')
wx_work.send_markdown_msg(msg='# 大标题 \n## 小标题\n* 列表项1\n* 列表项2')
wx_work.send_image_msg(file_path='image.jpg')
wx_work.send_file_msg(file_path='file.pdf', show_name='示例.pdf')