在這篇微信大眾平台開發教學中,我們將介紹如何使用開發模式實現多客服系統。
本文分為以下三個部分:
回覆多客服訊息
觸發多客服會話
其他說明
在新的微信協定中,開發模式也可接入客服系統。 開發者如果需要讓用戶使用客服系統,需要在接收到用戶發送的訊息時,回傳一個MsgType為transfer_customer_service的訊息,微信伺服器在收到這則訊息時,會把用戶這次發送的和以後一段時間內發送的訊息轉發客服系統。
傳回的訊息舉例如下
<xml> <ToUserName><![CDATA[touser]]></ToUserName> <FromUserName><![CDATA[fromuser]]></FromUserName> <CreateTime>1399197672</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>
該訊息的實作如下
//回复多客服消息 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; }
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" =>"http://www.3856.cc/weixin/weixin/logo.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 = ""; $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; } //回复多客服消息 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); } } } ?> %s $item_str
1. 經測試,在自訂選單中傳回多客服訊息,無法讓用戶進入多客服狀態。
2. 使用多客服訊息後,後續所有訊息在一段時間內都會作為客服訊息轉發,原來的開發模式下的自動回覆都會失效! ! !
以上是微信公眾平台開發多客服的詳細內容。更多資訊請關注PHP中文網其他相關文章!