微信公众号开发(七)发送客服消息
当用户和公众号产生特定动作的交互时(具体动作列表请见下方说明),微信将会把消息数据推送给开发者,开发者可以在一段时间内(目前修改为48小时)调用客服接口,通过POST一个JSON数据包来发送消息给普通用户。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
允许的动作如下:
- 用户发送信息
- 点击自定义菜单(仅有点击推事件、扫码推事件、扫码推事件且弹出“消息接收中”提示框这3种菜单类型是会触发客服接口的)
- 关注公众号
- 扫描二维码
- 支付成功
- 用户维权
现在客服接口可以使用永久media_id了。
1、发送客服消息
发送接口:https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
发送文本
<?php @header(\'Content-type: text/plain;charset=UTF-8\'); require_once("Utils.php"); $data = \'{ "touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c", "msgtype":"text", "text": { "content":"客服消息" } }\'; $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;返回结果如下:
{"errcode":0,"errmsg":"ok"}
发送图片
<?php @header(\'Content-type: text/plain;charset=UTF-8\'); require_once("Utils.php"); $data = \'{ "touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c", "msgtype":"image", "image": { "media_id":"FrsRJ3g3BHR-pIkuFLARnHjI9Cq9lDFas4Kp8otlAUQ" } }\'; $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;返回结果:
{"errcode":0,"errmsg":"ok"}
发送语音和这类似,不过结构为:
{ "touser":"OPENID", "msgtype":"voice", "voice": { "media_id":"MEDIA_ID" } }
发送音乐
<?php @header(\'Content-type: text/plain;charset=UTF-8\'); require_once("Utils.php"); $data = \'{ "touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c", "msgtype":"music", "music": { "title":"泡沫", "description":"邓紫棋", "musicurl":"http://weiweiyi.duapp.com/music/missyou.mp3", "hqmusicurl":"http://weiweiyi.duapp.com/music/missyou.mp3", "thumb_media_id":"FrsRJ3g3BHR-pIkuFLARnLApulXtdIVuSDOZVUMF4I8" } }\'; $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;
注:发送视频和发送音乐都没有显示thumb_media_id设置的缩略图,有知道的小伙伴麻烦留下言。
发送视频
<?php
@header(\'Content-type: text/plain;charset=UTF-8\');
require_once("Utils.php");
$data = \'{
"touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c",
"msgtype":"video",
"video":
{
"media_id":"FrsRJ3g3BHR-pIkuFLARnBcTeZTOOEh5acdetFMw1Xw",
"thumb_media_id":"bnahO7BqolsaJgQI_TsailL3OkztloUhG-xYealG2phqBpgid8kWcncVm_3ks8oT",
"title":"客服视频",
"description":"一个自拍小饰品"
}
}\';
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
<?php
@header(\'Content-type: text/plain;charset=UTF-8\');
require_once("Utils.php");
$data = \'{
"touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c",
"msgtype":"video",
"video":
{
"media_id":"FrsRJ3g3BHR-pIkuFLARnBcTeZTOOEh5acdetFMw1Xw",
"thumb_media_id":"bnahO7BqolsaJgQI_TsailL3OkztloUhG-xYealG2phqBpgid8kWcncVm_3ks8oT",
"title":"客服视频",
"description":"一个自拍小饰品"
}
}\';
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
发送图文消息(点击跳转到外链) 图文消息条数限制在8条以内
<?php
@header(\'Content-type: text/plain;charset=UTF-8\');
require_once("Utils.php");
$data = \'{
"touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c",
"msgtype":"news",
"news":{
"articles": [
{
"title":"第一项",
"description":"第一项描述",
"url":"http://www.baidu.com",
"picurl":"http://weiweiyi.duapp.com/images/img1.jpg"
},
{
"title":"第二项",
"description":"第二项描述",
"url":"http://www.baidu.com",
"picurl":"http://weiweiyi.duapp.com/images/img2.jpg"
}
]
}
}\';
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
发送图文消息(点击跳转到图文消息页面) 图文消息条数限制在8条以内
这里的图文就是指我们上传的永久图文消息,点击之后图文消息页面。
<?php
@header(\'Content-type: text/plain;charset=UTF-8\');
require_once("Utils.php");
$data = \'{
"touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c",
"msgtype":"mpnews",
"mpnews":
{
"media_id":"FrsRJ3g3BHR-pIkuFLARnAwGsFjf8Rckbd63rFBsE4o"
}
}\';
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?"
."access_token=".Utils::get_access_token();
$result = Utils::https_request($url, $data);
echo $result;
2、客服输入状态
有以下限制:
- 如果不满足发送客服消息的触发条件,则无法下发输入状态。
- 下发输入状态,需要客服之前30秒内跟用户有过消息交互。
- 在输入状态中(持续15s),不可重复下发输入态。
- 在输入状态中,如果向用户下发消息,会同时取消输入状态。
<?php @header(\'Content-type: text/plain;charset=UTF-8\'); require_once("Utils.php"); $data = \'{ "touser":"o4WmZ0h-4huBUVQUczx2ezaxIL9c", "command":"Typing"}\'; $url = "https://api.weixin.qq.com/cgi-bin/message/custom/typing?" ."access_token=".Utils::get_access_token(); $result = Utils::https_request($url, $data); echo $result;