小編給大家分享一下微信公眾平臺(tái)開發(fā)之多客服的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
目前創(chuàng)新互聯(lián)已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、滴道網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。一、回復(fù)多客服消息
在新的微信協(xié)議中,開發(fā)模式也可以接入客服系統(tǒng)。 開發(fā)者如果需要讓用戶使用客服系統(tǒng),需要在接收到用戶發(fā)送的消息時(shí),返回一個(gè)MsgType為transfer_customer_service的消息,微信服務(wù)器在收到這條消息時(shí),會(huì)把用戶這次發(fā)送的和以后一段時(shí)間內(nèi)發(fā)送的消息轉(zhuǎn)發(fā)客服系統(tǒng)。
返回的消息舉例如下
1399197672
該消息的實(shí)現(xiàn)如下
//回復(fù)多客服消息 private function transmitService($object) { $xmlTpl = ""; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } %s
二、觸發(fā)多客服會(huì)話
一般情況下,用戶想要咨詢問題是,經(jīng)常會(huì)問“你好”,“在嗎”,這樣的問題。
我們以這些詞為觸發(fā)關(guān)鍵詞,當(dāng)用戶發(fā)送的文本消息內(nèi)容中包含這些詞的時(shí)候,就返回多客服消息給用戶(用戶在微信端感覺不到任何內(nèi)容,但微信公眾賬號會(huì)將用戶本次及以后一段時(shí)間的消息都轉(zhuǎn)發(fā)到客服)。
實(shí)現(xiàn)代碼如下:
//接收文本消息 private function receiveText($object) { $keyword = trim($object->Content); if (strstr($keyword, "投訴") || strstr($keyword, "你好") || strstr($keyword, "在嗎")){ $result = $this->transmitService($object); } return $result; }
三、完整代碼
responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { //驗(yàn)證消息 public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } //檢查簽名 private 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; } } //響應(yīng)消息 public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $this->logger("R ".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; } $this->logger("T ".$result); echo $result; }else { echo ""; exit; } } //接收事件消息 private function receiveEvent($object) { switch ($object->Event) { case "subscribe": $content[] = array("Title" =>"歡迎關(guān)注方倍工作室", "Description" =>"使用方法:\n1.發(fā)送快遞單號,例如6367532560,可查詢快遞詳情", "PicUrl" =>"/tupian/20230522/404.html break; default: $content = "receive a new event: ".$object->Event; break; } if(is_array($content)){ if (isset($content[0])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } //接收文本消息 private function receiveText($object) { $keyword = trim($object->Content); if($keyword == "時(shí)間" || $keyword == "測試"){ $content = date("Y-m-d H:i:s",time()); $result = $this->transmitText($object, $content); } //觸發(fā)多客服模式 else if (strstr($keyword, "您好") || strstr($keyword, "你好") || strstr($keyword, "在嗎") || strstr($keyword, "有人嗎")){ $result = $this->transmitService($object); return $result; } return $result; } private function transmitText($object, $content) { $textTpl = ""; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = " %s - "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $newsTpl = "
"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } private function transmitMusic($object, $musicArray) { $itemTpl = " %s %s $item_str "; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $textTpl = " "; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回復(fù)多客服消息 private function transmitService($object) { $xmlTpl = " %s $item_str "; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } private function logger($log_content) { if(isset($_SERVER['HTTP_APPNAME'])){ //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL $max_size = 10000; $log_filename = "log.xml"; if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); } } } ?> %s
四、其他注意事項(xiàng)
1. 經(jīng)測試,在自定義菜單中返回多客服消息,無法讓用戶進(jìn)入多客服狀態(tài)。
2. 使用多客服消息后,后續(xù)所有消息在一段時(shí)間內(nèi)都將作為客服消息轉(zhuǎn)發(fā),原來的開發(fā)模式下的自動(dòng)回復(fù)都將失效?。?!
看完了這篇文章,相信你對“微信公眾平臺(tái)開發(fā)之多客服的示例分析”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!