How to Use the WeChat public platform development mode to implement multi-Customer Service _ php instances

Source: Internet
Author: User
In fact, the multiple customer service functions of the public platform have been available for a long time, and I have already implemented them for my own public account. I thought everyone would have done it, but today someone asked me how to use this multi-customer service function. I found no good tutorials on the offline network, today, I will send you a simple and easy-to-understand tutorial. In fact, many customer service functions of the public platform have been available for a long time, when I came out, I had already implemented my own public account. I thought everyone would have done it. But today someone asked me how to use this multi-customer service function, I found no good tutorials on the offline network. Today I will send you a simple and easy-to-understand tutorial!

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

I. Reply to multiple customer service messages

In the new protocol, the development mode can also be connected to the customer service system. If the developer wants the user to use the customer service system, a message with the MsgType transfer_customer_service is returned when the user receives the message. When the server receives the message, the messages sent by the user this time and later will be forwarded to the customer service system.

The returned message is as follows:

   
  touser
    
  fromuser
    
  
   1399197672
    
  transfer_customer_service
  
  

The message is implemented as follows (based on the public platform php sdk of fangtimes studio)

// Reply to multiple customer service messages private function transmitService ($ object) {$ xmlTpl ="
 
  %s
  
  %s
  
  
   
% S
  
  transfer_customer_service
  
 "; $ Result = sprintf ($ xmlTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;}

Ii. Trigger multi-Customer Service Sessions

Generally, the user may ask "hello" or "Are you there" questions.

We use these words as trigger keywords. When the text message content sent by the user contains these words, multiple customer service messages are returned to the user (the user does not feel any content at the end, however, the public account will forward the user's messages for this time and a period later to the customer service ).

The implementation code is as follows:

// Receive text message private function receiveText ($ object) {$ keyword = trim ($ object-> Content); if (strstr ($ keyword, "complaint ") | strstr ($ keyword, "") | strstr ($ keyword, "") {$ result = $ this-> transmitService ($ object );} return $ result ;}

Iii. complete code

<? Php/* Fang Times studio 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 ;}// check the signature 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 ;}// Response Message 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-> deleetext ($ postObj ); break;} $ this-> logger ("T ". $ result); echo $ result;} else {echo ""; exit ;}// receives the Event message private function receiveEvent ($ object) {switch ($ object-> Event) {case "subscribe": $ content [] = array ("Title" => "Welcome to fanwe Studio", "Description" => "Usage: \ n1. send the express waybill number, such as 6367532560. You can query the express delivery details "," 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 for Receiving text messages etext ($ object) {$ keyword = trim ($ object-> Content ); if ($ keyword = "time" | $ keyword = "test") {$ content = date ("Y-m-d H: I: s ", time (); $ result = $ this-> transmitText ($ object, $ content);} // trigger multi-Customer Service Mode else if (strstr ($ keyword, "Hello") | strstr ($ keyword, "") | strstr ($ keyword, "") | strstr ($ keyword, "anyone") {$ result = $ this-> transmitService ($ object); return $ result;} private function transmitText ($ object, $ content) {$ textTpl ="
 
  %s
  
  %s
  
  
   
% S
  
  text
  
  %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]>    
  %s
      
  %s
      
  %s
    
 "; $ Item_str =" "; foreach ($ newsArray as $ item) {$ item_str. = sprintf ($ itemTpl, $ item ['title'], $ item ['description'], $ item ['picurl'], $ item ['url']);} $ newsTpl ="
 
  %s
  
  %s
  
  
   
% S
  
  news
  
  
  % S$ Item_str
 "; $ Result = sprintf ($ newsTpl, $ object-> FromUserName, $ object-> ToUserName, time (), count ($ newsArray); return $ result ;} private function transmitMusic ($ object, $ musicArray) {$ itemTpl ="
   
  <! [CDATA [% s]>  
  %s
    
  %s
    
  %s
  
 "; $ Item_str = sprintf ($ itemTpl, $ musicArray ['title'], $ musicArray ['description'], $ musicArray ['musicurl'], $ musicArray ['hqmusicurl']); $ textTpl ="
 
  %s
  
  %s
  
  
   
% S
  
  music
  $ Item_str
 "; $ Result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result ;} // reply to multiple customer service messages private function transmitService ($ object) {$ xmlTpl ="
 
  %s
  
  %s
  
  
   
% S
  
  transfer_customer_service
  
 "; $ 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) ;}}?>

After this code is tested, multiple customer service messages are returned in the custom menu, so that users cannot enter the multi-Customer Service Status. After Multiple customer service messages are used, all subsequent messages will be forwarded as customer service messages within a period of time, and automatic replies in the original development mode will become invalid.

This article is not well written. I hope Hai Han can share some good comments. Everyone can learn and make progress together. At the same time, I would like to thank you for your constant support for the home of scripts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.