項目說明:
本項目是一個簡單微信小程序客服消息類,實現客服消息相關功能。官方給的php示例有誤,這裏就不再吐槽了。
本示例是採用開發者服務器,沒有采用雲調用的形式。
官方文檔:
適用場景
客戶消息流程圖
使用步驟
1、開啓客服消息
https://mp.weixin.qq.com/wxam...
登錄-開發-開發設置-消息推送
[](https://raw.githubusercontent...
點擊“啓動”
[](https://raw.githubusercontent...
URL(服務器地址):填開發者服務器對應的url,如 https://xxxxxx/demo.php
Token(令牌):這個隨便填,要求3-32位。
EncodingAESKey(消息加密密鑰):這個點擊“隨機生成”即可。
消息加密方式:可以根據自己需要選擇,本例選擇”兼容模式“。
數據格式:json相對於xml來說,從壓縮效率及傳輸效率更具優勢,這裏我們選json。
注意:以上操作完後先不要提交,等配置好開發者服務端後再提交。
2、配置開發者服務端
檢驗signature的PHP示例代碼:
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$echostr=$_GET["echostr"];
$token = TOKEN;//這裏改成你第一步操作時填寫的token
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if ($tmpStr == $signature ) {
return $echostr;
} else {
return false;
}
官方示例沒有返回 $echostr ,這個檢驗開發者服務端是否成功的關鍵,必須返回。
3、提交消息推送配置
如果沒有報錯,證明配置成功。
4、開發者服務端demo
<?php
//驗證signature
//$signature = $_GET["signature"];
//$timestamp = $_GET["timestamp"];
//$nonce = $_GET["nonce"];
//$echostr=$_GET["echostr"];
//
//$token = TOKEN;//這裏改成你第一步操作時填寫的token
//$tmpArr = array($token, $timestamp, $nonce);
//sort($tmpArr, SORT_STRING);
//$tmpStr = implode( $tmpArr );
//$tmpStr = sha1( $tmpStr );
//
//if ($tmpStr == $signature ) {
// return $echostr;
//} else {
// return false;
//}
include_once './Xcxmsg.php';
$xcxmsg = new Xcxmsg();
$postStr = file_get_contents('php://input');
if (!$postStr)
return false;
$postArr = json_decode($postStr, true);
if (!isset($postArr['MsgType']) || !isset($postArr['FromUserName']))
return false;
$data = ["touser" => $postArr['FromUserName']];
$accessToken = $xcxmsg->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $accessToken;
switch ($postArr['MsgType']) {
case "text":
//如用戶發送的是文字信息,這裏處理
//回覆圖文鏈接,也可以回覆別的類型,根據需要
$data['msgtype'] = "link";
$data['link'] = [
"title" => "hello",
"description" => "Is Really A Happy Day",
"url" => "LINK_URL",//連接url
"thumb_url" =>"THUMB_URL" //圖片url
];
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
$xcxmsg->curl($json, $url);
break;
case "image": //如用戶發送圖片消息,進入這裏
//服務端回覆 圖片,也可以回覆別的類型,根據需要
$data['msgtype'] = "image";
$data['image'] = ['media_id' => 'media_id值']; // 執行 $xcxmsg->upload($accessToken)返回的 media_id
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
$xcxmsg->curl($json, $url);
case "miniprogrampage":
//如用戶發送小程序卡片,進入這裏
//這裏服務端回覆小卡片,也可以回覆別的類型,根據需要
$data['msgtype'] = "miniprogrampage";
$data['miniprogrampage'] = [
"title" => "title",
"pagepath" => "pages/index/index",
"thumb_media_id" => "media_id值"];// 執行 $xcxmsg->upload($accessToken)返回的 media_id
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
$xcxmsg->curl($json, $url);
break;
case "event":
//如用戶進入會話事件
//這裏可以回覆文本
$data['msgtype'] = "text";
$data['text'] = [
"content" => "Hello World",
];
$json = json_encode($data, JSON_UNESCAPED_UNICODE);
$xcxmsg->curl($json, $url);
break;
default:
}
5、小程序前端
在需要的地方添加以下代碼:
<button open-type="contact" >客服消息</button>
用微信開發工具的預覽,生成二維碼,掃描測試是否成功。
項目地址:https://github.com/guyan0319/...
在使用中如有任何問題,請回復指正,謝謝!