您好,登錄后才能下訂單哦!
利用微信公眾平臺開發模式怎么實現一個多客服功能?相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
一、回復多客服消息
在新的微信協議中,開發模式也可以接入客服系統。 開發者如果需要讓用戶使用客服系統,需要在接收到用戶發送的消息時,返回一個MsgType為transfer_customer_service的消息,微信服務器在收到這條消息時,會把用戶這次發送的和以后一段時間內發送的消息轉發客服系統。
返回的消息舉例如下
<xml> <ToUserName><![CDATA[touser]]></ToUserName> <FromUserName><![CDATA[fromuser]]></FromUserName> <CreateTime>1399197672</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>
該消息的實現如下(以方倍工作室的微信公眾平臺PHP SDK為基礎)
//回復多客服消息 private function transmitService($object) { $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; }
二、觸發多客服會話
一般情況下,用戶想要咨詢問題是,經常會問“你好”,“在嗎”,這樣的問題。
我們以這些詞為觸發關鍵詞,當用戶發送的文本消息內容中包含這些詞的時候,就返回多客服消息給用戶(用戶在微信端感覺不到任何內容,但微信公眾賬號會將用戶本次及以后一段時間的消息都轉發到客服)。
實現代碼如下:
//接收文本消息 private function receiveText($object) { $keyword = trim($object->Content); if (strstr($keyword, "投訴") || strstr($keyword, "你好") || strstr($keyword, "在嗎")){ $result = $this->transmitService($object); } return $result; }
三、完整代碼
<?php /* 方倍工作室 CopyRight 2014 All Rights Reserved */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { //驗證消息 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; } } //響應消息 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" =>"歡迎關注方倍工作室", "Description" =>"使用方法:\n1.發送快遞單號,例如6367532560,可查詢快遞詳情", "PicUrl" =>"https://cache.yisu.com/upload/information/20201209/266/41102.jpg", "Url" =>""); 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 == "時間" || $keyword == "測試"){ $content = date("Y-m-d H:i:s",time()); $result = $this->transmitText($object, $content); } //觸發多客服模式 else if (strstr($keyword, "您好") || strstr($keyword, "你好") || strstr($keyword, "在嗎") || strstr($keyword, "有人嗎")){ $result = $this->transmitService($object); return $result; } return $result; } private function transmitText($object, $content) { $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $newsTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <Content><![CDATA[]]></Content> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> </xml>"; $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } private function transmitMusic($object, $musicArray) { $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str </xml>"; $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回復多客服消息 private function transmitService($object) { $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>"; $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); } } } ?>
看完上述內容,你們掌握利用微信公眾平臺開發模式怎么實現一個多客服功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。