再也不用花錢使用XX小客服了!
最近在做小程序,涉及到小程序引流到公眾號的問題,
這一塊微信做得極不友好,方法很多但都有局限性,
相對靠譜的方案就是通過小程序客服給用戶發送二維碼,引導其長按識別關注。
但是網上的代碼試了好多都沒有成功,我這里給出一套確實可用的,希望可以幫助大家。
注意幾點:
1.現在客服沒法主動發消息,需要用戶先主動發送,然后我們才能做出回復;
2.使用了自動回復后,人工客服將收不到消息;
3.回復的圖片都必須上傳到微信服務器,獲取media_id后發送。
我的小程序客服回復邏輯如下:
1.頁面上的按鈕引導點擊,點擊后引導回復;
2.用戶點擊回復按鈕跳到客服對話界面;
3.回復設定(文末的代碼比這全):
回復“1”,返回公眾號二維碼;
回復“2”,返回圖文鏈接;
回復其他任何消息,返回客服微信二維碼。
大家可以微信搜索“最火壁紙”小程序,體驗該效果,也可以直接掃碼:
下面我給出相應的完整代碼(PHP實現),你可以根據需要隨意調整。
可以實現:
自動回復圖片;
自動回復文字;
自動回復圖文鏈接;
可以判斷用戶是否發送圖片/文字并回復;
可以在用戶進入客服界面后馬上回復(需要用戶之前一段時間內回復過);
可以在用戶發送任意消息時都回復;
一、小程序進入客服按鈕
wxml代碼,例:
<button open-type="contact" class='follow_mp'>回復“1”,關注我們</button>
注意:這里的樣式需要自己根據需要修改class。
二、配置“消息推送”
進入小程序平臺,選擇左邊欄“開發”->“開發設置”->“消息推送”->點擊“啟用”
填寫配置,數據格式選擇json
三、token驗證
直接使用第四步的代碼即可通過驗證,通過后也不需要像網上說的那樣需要注釋掉驗證代碼,因為注釋掉后用戶回復消息時可能會出現提示:“該小程序提供的服務出現故障,請稍后再試”。
四、自動回復代碼(完整)
修改第2行的TOKEN為自己的、修改第165行的小程序id和secret、修改47行的二維碼地址等,其他的按需修改和注釋掉。
<?php
define("TOKEN", "k8h5qu8znxxxxxxjazm76"); //填寫自己的token,跟小程序后臺填的一樣即可
if (isset($_GET['echostr'])) { //校驗服務器地址URL
valid();
}else{
responseMsg();
}
function valid()
{
$echoStr = $_GET["echostr"];
if(checkSignature()){
header('content-type:text');
echo $echoStr;
exit;
}else{
echo $echoStr.'+++'.TOKEN;
exit;
}
}
function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
function responseMsg()
{
$postStr = file_get_contents('php://input'); //此處推薦使用file_get_contents('php://input')獲取后臺post過來的數據
if (!empty($postStr) && is_string($postStr)){
$postArr = json_decode($postStr,true);
if(!empty($postArr['MsgType']) && $postArr['Content'] == "1"){ //用戶發送1,回復公眾號二維碼
$fromUsername = $postArr['FromUserName']; //發送者openid
$imgurl = "/300-300.png"; //公眾號二維碼,相對路徑,修改為自己的
$media_id = getMediaId($imgurl); //獲取圖片消息的media_id
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"image",
"image"=>array("media_id"=>$media_id)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+
requestAPI($json);
}
elseif(!empty($postArr['MsgType']) && $postArr['Content'] == "2"){ //用戶發送2,回復圖文鏈接
$fromUsername = $postArr['FromUserName']; //發送者openid
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"link",
"link"=>array( //修改下面幾項為自己的
"title"=>'最火壁紙',
"description"=>'2020最火手機壁紙',
"url"=>'https://bizhi.tarfar.com/',
"thumb_url"=>'https://bizhi.tarfar.com/wp-content/uploads/2020/03/logo.png',
)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+
requestAPI($json);
}
elseif(!empty($postArr['MsgType']) && $postArr['Content'] == "3"){ //用戶發送3,回復文字
$fromUsername = $postArr['FromUserName']; //發送者openid
$content = '你好,回復1關注公眾號,回復2獲取官網鏈接'; //修改為自己需要的文字
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"text",
"text"=>array("content"=>$content)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+
requestAPI($json);
}
elseif(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'image'){ //用戶發送圖片,這里示例為回復他公眾號二維碼
$fromUsername = $postArr['FromUserName']; //發送者openid
$imgurl = "/300-300.png"; //公眾號二維碼,相對路徑,修改為自己的
$media_id = getMediaId($imgurl); //獲取圖片消息的media_id
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"image",
"image"=>array("media_id"=>$media_id)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4以上版本才可使用
requestAPI($json);
}
elseif($postArr['MsgType'] == 'event' && $postArr['Event']=='user_enter_tempsession'){ //用戶進入客服后馬上回復,現在已失效,需要用戶先發過消息
$fromUsername = $postArr['FromUserName'];
$content = '你好,回復1關注公眾號,回復2獲取官網鏈接';
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"text",
"text"=>array("content"=>$content)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+
requestAPI($json);
}
elseif($postArr['MsgType'] !== 'event'){ //用戶發送其他內容,引導加客服
$fromUsername = $postArr['FromUserName']; //發送者openid
$imgurl = "/miniapp300-300.png"; //客服微信二維碼,相對路徑
$media_id = getMediaId($imgurl); //獲取圖片消息的media_id
$data=array(
"touser"=>$fromUsername,
"msgtype"=>"image",
"image"=>array("media_id"=>$media_id)
);
$json = json_encode($data,JSON_UNESCAPED_UNICODE); //php5.4+
requestAPI($json);
}
else{
exit;
}
}else{
echo "empty";
exit;
}
}
function requestAPI($json){
$access_token = get_accessToken();
/*
* POST發送https請求客服接口api
*/
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
//以'json'格式發送post的https請求
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1); // 發送一個常規的Post請求
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($json)){
curl_setopt($curl, CURLOPT_POSTFIELDS,$json);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
$output = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓異常
}
curl_close($curl);
if($output == 0){
echo 'success';exit;
}
}
/* 調用微信api,獲取access_token,有效期7200s*/
function get_accessToken(){
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4c629xxxx7853c&secret=c3e2axxxxxxxxxxxx09f'; //替換成自己的小程序id和secret
@$weixin = file_get_contents($url);
@$jsondecode = json_decode($weixin);
@$array = get_object_vars($jsondecode);
$token = $array['access_token'];
return $token;
}
//獲取上傳圖片的media_id
function getMediaId($imgurl){
$token=get_accessToken();
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type=image";
// echo $url;
$ch1 = curl_init();
$timeout = 10;
$real_path = "{$_SERVER['DOCUMENT_ROOT']}$imgurl";//自動轉成圖片文件絕對路徑,如果圖片發送失敗,檢查PHP的$_SERVER['DOCUMENT_ROOT'的配置
// echo $real_path;
$data = array("media" => new CURLFile("{$real_path}"));//php5.6(含)以上版本使用此方法
// var_dump($data);
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch1);
// echo $result;
curl_close($ch1);
if($result){
$result = json_decode($result, true);
return $result['media_id'];
} else{
return null;
}
}
?>
PHP文件下載:
鏈接:https://pan.baidu.com/s/1-F64mQjHf62lOH0RWjAqzw
提取碼:3vjd