search
HomeWeChat AppletWeChat DevelopmentWeChat public platform develops multiple customer services

In this WeChat public platform development tutorial, we will introduce how to use the development mode to implement a multi-customer service system.

This article is divided into the following three parts:

  1. Reply to multiple customer service messages

  2. Trigger multiple customer service sessions

  3. Other instructions

1. Reply to multiple customer service messages

In the new WeChat protocol, the development mode also Can access customer service system. If the developer needs to allow users to use the customer service system, they need to return a message with MsgType of transfer_customer_service when receiving the message sent by the user. When the WeChat server receives this message, it will transfer the message sent by the user this time and within a certain period of time in the future. The sent message is forwarded to the customer service system.

An example of the returned message is as follows

<xml>
    <tousername></tousername>
    <fromusername></fromusername>
    <createtime>1399197672</createtime>
    <msgtype></msgtype>
</xml>

The implementation of this message is as follows

//回复多客服消息
    private function transmitService($object)
    {
        $xmlTpl = "<xml>
<tousername></tousername>
<fromusername></fromusername>
<createtime>%s</createtime>
<msgtype></msgtype>
</xml>";
        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }

2. Triggering multiple customer service sessions

General situation Next, when users want to ask questions, they often ask questions such as "Hello" and "Are you there?"

We use these words as trigger keywords. When the content of the text message sent by the user contains these words, multiple customer service messages will be returned to the user (the user cannot feel any content on WeChat, but the WeChat public The account will forward all the user's messages this time and in the future to customer service).

The implementation code is as follows:

//接收文本消息
    private function receiveText($object)
    {
        $keyword = trim($object->Content);
        if (strstr($keyword, "投诉") || strstr($keyword, "你好") || strstr($keyword, "在吗")){
            $result = $this->transmitService($object);
        }

        return $result;
    }

3. Complete code

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 = "


%s


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

    private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return;
        }
        $itemTpl = "    
        
        
        
        
    
";
        $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 = "
    
    
    
    
";

        $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></tousername>
<fromusername></fromusername>
<createtime>%s</createtime>
<msgtype></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);
        }
    }
}
?>

4. Other notes

1. After testing, return in the custom menu Multiple customer service messages cannot allow users to enter multiple customer service states.

2. After using multiple customer service messages, all subsequent messages will be forwarded as customer service messages for a period of time, and the automatic replies in the original development mode will be invalid! ! !


The above is the detailed content of WeChat public platform develops multiple customer services. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function