微信公众号开发(七)发送客服消息

微信公众号开发(七)发送客服消息


当用户和公众号产生特定动做的交互时(具体动做列表请见下方说明),微信将会把消息数据推送给开发者,开发者能够在一段时间内(目前修改成48小时)调用客服接口,经过POST一个JSON数据包来发送消息给普通用户。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
容许的动做以下:
  1. 用户发送信息
  2. 点击自定义菜单(仅有点击推事件、扫码推事件、扫码推事件且弹出“消息接收中”提示框这3种菜单类型是会触发客服接口的)
  3. 关注公众号
  4. 扫描二维码
  5. 支付成功
  6. 用户维权
如今客服接口可使用永久media_id了。

一、发送客服消息

发送接口: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;


发送图文消息(点击跳转到外链) 图文消息条数限制在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;



二、客服输入状态

有如下限制:
  1. 若是不知足发送客服消息的触发条件,则没法下发输入状态。
  2. 下发输入状态,须要客服以前30秒内跟用户有过消息交互。
  3. 在输入状态中(持续15s),不可重复下发输入态。
  4. 在输入状态中,若是向用户下发消息,会同时取消输入状态。
<?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;