微信公衆號開發(七)發送客服消息

微信公衆號開發(七)發送客服消息


當用戶和公衆號產生特定動做的交互時(具體動做列表請見下方說明),微信將會把消息數據推送給開發者,開發者能夠在一段時間內(目前修改成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;

相關文章
相關標籤/搜索