[TOC]
## 客服消息接口
微信提供客服接口,用户给用户发送消息。在 `wetchat.py `中封装成`WeChatMessage`。
*注意:未认证的订阅号和公众号,是没有该接口权限的*
### 发送文本消息
发送消息前,先获取粉丝的open_id,这里取巧,直接在公众号后台拿
![](https://box.kancloud.cn/03db3329bda22f758b02d1651ce2cf29_1041x309.png)
```
wx.message.send_text(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",content="我不在我不在我不在.....")
Out[25]: {'errcode': 0, 'errmsg': 'ok'}
wx.message.send_text(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",content="我不在我不在我不在.....")
Out[26]: {'errcode': 0, 'errmsg': 'ok'}
```
效果如下:
![](https://box.kancloud.cn/58c3d5276f75ad2e64fcb66574d31094_446x196.png)
>[danger] 注意,会话需要由用户先发起,否则会出现45015:回复时间超过限制的错误。
### 发送图片消息
```
wx.message.send_image(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",media_id="BzzIfj9NcYYTuaPR_j133jbk4KxYuPvhoZx68usaDi0")
Out[28]: {'errcode': 0, 'errmsg': 'ok'}
```
效果如下:
![](https://box.kancloud.cn/2e2d85640b5dda9797b7671dacdcebcc_462x327.png)
### 发送图文消息
* 通过永久图文素材media发送
```
wx.message.send_articles(
user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
articles="BzzIfj9NcYYTuaPR_j133mW-veyn3sNRANnduEtdnss"
)
Out[29]: {'errcode': 0, 'errmsg': 'ok'}
```
![](https://box.kancloud.cn/74f5938b2f0f167c15d122e9715c8435_450x603.png)
* 点击跳转到外链
```
wx.message.send_articles(
user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
articles=[
{
"title": "Happy Day",
"description": "Is Really A Happy Day",
"url": "https://www.kancloud.cn/@guanfuchang",
"picurl": "http://mmbiz.qpic.cn/mmbiz_png/QuFmLYzPJ2DK93uoeUAibJSqQBmgeR1uhdicrQKxT3QzZVdfWUIHa6fmFfuaibLNibqYGVHQcwyzCbsbPWTN82Vs9g/0?wx_fmt=png"
}
]
)
Out[30]: {'errcode': 0, 'errmsg': 'ok'}
```
![](https://box.kancloud.cn/5347a4f5c0f2cea060336792180895f9_452x690.png)
* 通过临时图文素材media_id发送
```
wx.message.send_articles(user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
articles=wx.media.upload_articles(
articles=[
{
"thumb_media_id": wx.media.upload("image", open("D://666.png", "rb")).get("media_id"),
"author": "观小鱼",
"title": "图文标题",
"content_source_url": "https://www.kancloud.cn/@guanfuchang",
"content": "图文详情内容",
"digest": "图文概要描述",
"show_cover_pic": 1,
"need_open_comment": 1,
"only_fans_can_comment": 1
}
]).get("media_id"))
Out[32]: {'errcode': 0, 'errmsg': 'ok'}
```
![](https://box.kancloud.cn/7e5578a47bb66a3c79b632d3831ca49b_466x735.png)
### 发送音乐消息
```
wx.message.send_music(
user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
url="https://od.qingting.fm/m4a/5a8e82757cb89146f20a287b_8762416_64.m4a",
hq_url="https://od.qingting.fm/m4a/5a8e82757cb89146f20a287b_8762416_64.m4a",
thumb_media_id=wx.media.upload("thumb", open("D://music.jpg","rb")).get("thumb_media_id"),
title="梦想明月曲",
description="崔子格",
)
Out[35]: {'errcode': 0, 'errmsg': 'ok'}
```
### 发送视频消息
```
wx.message.send_video(
user_id="oiQF0tzMZ5Md5HaAlKvrZo9OIGBw",
media_id="BzzIfj9NcYYTuaPR_j133ietkr1Oy2009whI6vdTLR8",
)
Out[36]: {'errcode': 0, 'errmsg': 'ok'}
```
## 群发消息接口
### 群发文本
```
wx.message.send_mass_text(
group_or_users=None,
content="群发消息",
is_to_all=True
)
Out[37]: {'errcode': 0, 'errmsg': 'send job submission success', 'msg_id': 1000000002}
```
>[info] 更多群发接口就不一一举例,参考源代码 `message` 类中,send_mass_xxx 方法即可。
<hr style="margin-top:100px">
:-: ![](https://box.kancloud.cn/331f659e8e6cddb0d9f182e00e32803f_258x258.jpg)
***微信扫一扫,关注“python测试开发圈”,获取更多测试开发分享!***