php 微信公眾平臺(tái)開(kāi)發(fā)模式實(shí)現(xiàn)多客服的實(shí)例代碼
其實(shí)微信公眾平臺(tái)的多客服功能已經(jīng)出來(lái)好久了,并且一出來(lái)的時(shí)候我就已經(jīng)為自己的公眾號(hào)實(shí)現(xiàn)了,原本以為大家都已經(jīng)會(huì)了,但是今天還是有人問(wèn)起這個(gè)多客服功能怎么使用,我找了下網(wǎng)上也沒(méi)什么好的教程,今天我就給大家發(fā)一篇比較簡(jiǎn)單易懂的教程吧
在這篇微信公眾平臺(tái)開(kāi)發(fā)教程中,我們將介紹如何使用開(kāi)發(fā)模式實(shí)現(xiàn)多客服系統(tǒng)。
一、回復(fù)多客服消息
在新的微信協(xié)議中,開(kāi)發(fā)模式也可以接入客服系統(tǒng)。 開(kāi)發(fā)者如果需要讓用戶使用客服系統(tǒng),需要在接收到用戶發(fā)送的消息時(shí),返回一個(gè)MsgType為transfer_customer_service的消息,微信服務(wù)器在收到這條消息時(shí),會(huì)把用戶這次發(fā)送的和以后一段時(shí)間內(nèi)發(fā)送的消息轉(zhuǎn)發(fā)客服系統(tǒng)。
返回的消息舉例如下
<xml> <ToUserName><![CDATA[touser]]></ToUserName> <FromUserName><![CDATA[fromuser]]></FromUserName> <CreateTime>1399197672</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>
該消息的實(shí)現(xiàn)如下(以方倍工作室的微信公眾平臺(tái)PHP SDK為基礎(chǔ))
//回復(fù)多客服消息 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; }
二、觸發(fā)多客服會(huì)話
一般情況下,用戶想要咨詢問(wèn)題是,經(jīng)常會(huì)問(wèn)“你好”,“在嗎”,這樣的問(wèn)題。
我們以這些詞為觸發(fā)關(guān)鍵詞,當(dāng)用戶發(fā)送的文本消息內(nèi)容中包含這些詞的時(shí)候,就返回多客服消息給用戶(用戶在微信端感覺(jué)不到任何內(nèi)容,但微信公眾賬號(hào)會(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; }
三、完整代碼
<?php /* 方倍工作室 CopyRight 2014 All Rights Reserved */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->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ā)送快遞單號(hào),例如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 == "時(shí)間" || $keyword == "測(cè)試"){ $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 = "<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; } //回復(fù)多客服消息 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); } } } ?>
本段代碼經(jīng)過(guò)測(cè)試,在自定義菜單中返回多客服消息,無(wú)法讓用戶進(jìn)入多客服狀態(tài),使用多客服消息后,后續(xù)所有消息在一段時(shí)間內(nèi)都將作為客服消息轉(zhuǎn)發(fā),原來(lái)的開(kāi)發(fā)模式下的自動(dòng)回復(fù)都將失效。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- 反向Ajax 30分鐘快速掌握
- 基于PHP技術(shù)開(kāi)發(fā)客服工單系統(tǒng)
- PHP+Mysql+Ajax實(shí)現(xiàn)淘寶客服或阿里旺旺聊天功能(前臺(tái)頁(yè)面)
- PHP+jquery+ajax實(shí)現(xiàn)即時(shí)聊天功能實(shí)例
- 發(fā)布一個(gè)迷你php+AJAX聊天程序[聊天室]提供下載
- Ajax PHP JavaScript MySQL實(shí)現(xiàn)簡(jiǎn)易無(wú)刷新在線聊天室
- 值得分享的php+ajax實(shí)時(shí)聊天室
- PHP+mysql+ajax輕量級(jí)聊天室實(shí)現(xiàn)方法詳解
- 基于javascript、ajax、memcache和PHP實(shí)現(xiàn)的簡(jiǎn)易在線聊天室
- PHP使用反向Ajax技術(shù)實(shí)現(xiàn)在線客服系統(tǒng)詳解
相關(guān)文章
destoon實(shí)現(xiàn)資訊信息前面調(diào)用它所屬分類的方法
這篇文章主要介紹了destoon實(shí)現(xiàn)資訊信息前面調(diào)用它所屬分類的方法,在模板制作中非常實(shí)用,需要的朋友可以參考下2014-07-07Laravel框架模型的創(chuàng)建及模型對(duì)數(shù)據(jù)操作示例
這篇文章主要介紹了Laravel框架模型的創(chuàng)建及模型對(duì)數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了Laravel框架創(chuàng)建模型及使用模型進(jìn)行數(shù)據(jù)的增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2019-05-05Laravel中獲取路由參數(shù)Route Parameters的五種方法示例
這篇文章主要給大家介紹了關(guān)于Laravel中獲取路由參數(shù)Route Parameters的五種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Laravel具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-09-09彈出模態(tài)框modal的實(shí)現(xiàn)方法及實(shí)例
這篇文章主要介紹了彈出模態(tài)框modal的實(shí)現(xiàn)方法及實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09利用PHP fsockopen 模擬POST/GET傳送數(shù)據(jù)的方法
使用php可以模擬post和get傳送數(shù)據(jù)到別的網(wǎng)頁(yè)或者是站點(diǎn),那么怎么傳送數(shù)據(jù)呢?下面由小編給大家介紹利用PHP fsockopen 模擬POST/GET傳送數(shù)據(jù)的方法,需要的朋友一起看看吧2015-09-09