How to use WeChat public platform development mode to realize multi-service, public platform _php tutorial

Source: Internet
Author: User
Tags cdata sprintf

How to use public platform development mode to realize multi-service, public platform


In fact, the public platform of the multi-customer service has been out for a long time, and a out when I have been for their own public number has been realized, originally thought that everyone will have, but today or someone asked how this multi-customer service function How to use, I found the next online there is no good tutorial, Today, I will send you a more simple and understandable tutorial!

In this public platform development tutorial, we will show how to use the development model to implement multi-service systems.

First, reply to the multi-service message

In the new protocol, the development model can also be connected to the customer service system. If the developer needs to let the user use the customer service system, it is necessary to return a message msgtype to Transfer_customer_service when the message is received by the user, and when the server receives the message, Will send the user this time and the next period of time to send the message forwarding customer service system.

Examples of returned messages are as follows

 
    
  
   touser
  
     
  
   fromuser
  
     
  
   
    
   1399197672
  
     
  
   transfer_customer_service
  
   
 
  

The message is implemented as follows (based on the PHP SDK of the public platform of the double studio)

   Reply to multi-service message  Private function Transmitservice ($object)  {    $xmlTpl = "
 
  
  
    %s
  
   
  
   
    
   %s
  
   ";
  
    transfer_customer_service
  
   
 
    %s
  
    
      $result = sprintf ($XMLTPL, $object->fromusername, $object->tousername, Time ());    return $result;  

Second, trigger multi-service sessions

In general, users who want to ask questions are often asked "hello", "yes", and so on.

We use these words to trigger the keyword, when the user sends the text message content contains these words, returns the multi-customer service message to the user (the user at the end does not feel any content, but the public account will send the user this and later period of time the message all forwards 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, "Hello") | | strstr ($keyword, "in")) {      $result = $this->transmitservice ($object);    }    return $result;  }

Three, the complete code

<?php/* Double Studio CopyRight All rights Reserved*/define ("TOKEN", "Weixin"); $wechatObj = new Wechatcallbackapitest (); if (!isset ($_get[' echostr ')) {$WECHATOBJ->responsemsg ();} else{$wechatObj->valid ();}    Class wechatcallbackapitest{//Authentication message 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->receivetext ($POSTOBJ);      Break      } $this->logger ("T". $result);    echo $result;      }else {echo "";    Exit        }}//Receive event message Private function Receiveevent ($object) {switch ($object->event) {case "subscribe": $content [] = Array ("Title" = "Welcome to the double studio", "Description" and "=") How to use: \n1. Send a courier number, for example 6367532560, can check the courier Details "," picurl "=        > "Http://www.3856.cc/weixin/weixin/logo.jpg", "Url" and "=" ");      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;    }//Receive text message Private function Receivetext ($object) {$keyword = Trim ($object->content);      if ($keyword = = "Time" | | $keyword = = "Test") {$content = Date ("y-m-d h:i:s", Times ());    $result = $this->transmittext ($object, $content); }//Trigger multi-service mode else if (Strstr ($keyword, "Hello") | | strstr ($keyword, "Hello") | | strstr ($keyword, "in") | | strstr ($KEYWORD,      "Someone") {$result = $this->transmitservice ($object);    return $result;  } 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$item_str %s 
 
    
    news 
 
    
    
    
    %s 
 
    
     
 
   
 
 
 
 
 
  
 ";    $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 $item 
   
   
    _str music 
 
    
    
    
    %s 
 
    
    %s 
 
    
 
 
 
 
  
 ";    $result = sprintf ($TEXTTPL, $object->fromusername, $object->tousername, Time ());  return $result; }//Reply to multi-service message Private function Transmitservice ($object) {$XMLTPL = "
 
   
   
    %s transfer_customer_service 
 
    
    
    %s 
 
    
    %s 
 
    
 
 
 
 

  
 ";    $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 (FAL      SE);      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); }}}?>

This code has been tested in the custom menu to return multi-customer service messages, can not allow users to enter the multi-customer service status, the use of multi-customer service messages, all subsequent messages in a period of time will be as customer service message forwarding, the original development mode of the automatic response will be invalidated.

This article is not good, also hope Haihan, have a good opinion welcome to share, everyone together to learn progress. At the same time, thank you all have always been to help the home site support.

Articles you may be interested in:

    • Python implements a custom menu instance for the public platform
    • PHP Public Platform Development Class example
    • PHP version of the public platform Red Envelope API
    • Express Query function Usage example of public platform
    • Public platform to realize the method of accessing user OpenID
    • An introductory example of public platform development based on PHP
    • Basic information for users of PHP public platform development

http://www.bkjia.com/PHPjc/1089201.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089201.html techarticle How to use the public platform development mode to achieve multi-customer service, public platform in fact, the public platform of the multi-service function has been out for a long time, and a out of the times I have for their own ...

  • Related Article

    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.