国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 微信小程式 微信開發(fā) 微信公眾平臺開發(fā) 多客服

微信公眾平臺開發(fā) 多客服

Feb 24, 2017 pm 05:05 PM

在這篇微信大眾平臺開發(fā)教學中,我們將介紹如何使用開發(fā)模式實現(xiàn)多客服系統(tǒng)。

本文分為以下三個部分:

回覆多客服訊息

觸發(fā)多客服會話

其他說明

?

一、回覆多客服訊息

在新的微信協(xié)定中,開發(fā)模式也可以連接到客服系統(tǒng)。 開發(fā)者如果需要讓用戶使用客服系統(tǒng),需要在接收到用戶發(fā)送的訊息時,回傳一個MsgType為transfer_customer_service的訊息,微信伺服器在收到這則訊息時,會把用戶這次發(fā)送的和以後一段時間內(nèi)發(fā)送的訊息轉發(fā)客服系統(tǒng)。

傳回的訊息舉例如下

<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;
    }

二、觸發(fā)多客服會話

一般情況下,使用者想要諮詢問題是,經(jīng)常會問“你好”,“在嗎”,這樣的問題。

我們以這些詞為觸發(fā)關鍵字,當用戶發(fā)送的文字訊息內(nèi)容中包含這些詞的時候,就返回多客服訊息給用戶(用戶在微信端感覺不到任何內(nèi)容,但微信公眾帳號會將用戶本次及以後一段時間的訊息轉寄到客服)。

實作程式碼如下:

//接收文本消息
    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.發(fā)送快遞單號,例如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);
        }
        //觸發(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 = "


%s


";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }

    private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return;
        }
        $itemTpl = "    
        <![CDATA[%s]]>
        
        
        
    
";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $newsTpl = "


%s


%s

$item_str
";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        return $result;
    }

    private function transmitMusic($object, $musicArray)
    {
        $itemTpl = "
    <![CDATA[%s]]>
    
    
    
";

        $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);

        $textTpl = "


%s

$item_str
";

        $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);
        }
    }
}
?>

四、其他注意事項

1. 經(jīng)測試,在自訂選單中傳回多客服訊息,無法讓用戶進入多客服狀態(tài)。?

2. 使用多客服訊息後,後續(xù)所有訊息在一段時間內(nèi)都會作為客服訊息轉發(fā),原來的開發(fā)模式下的自動回覆都會失效! ! !

更多微信大眾平臺開發(fā) 多客服相關文章請關注PHP中文網(wǎng)!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權的內(nèi)容,請聯(lián)絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)