PHP Micro-trust public platform development model to achieve multiple customer service instance Code _php instance

Source: Internet
Author: User
Tags cdata sprintf trim

In fact, micro-trust public platform for many customer service function has been out for a long time, and when I came out of the time for their own public number has been realized, originally thought that everyone would have, but today or someone asked how to use the multi-customer service function, I looked for the internet there is no good tutorials, Today, I will send you a more simple and understandable tutorial

In this micro-credit public Platform development tutorial, we will describe how to use development mode to implement a multiple customer service system.

One, reply more customer service message

In the new micro-trust 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, need to receive the message which the user sends, returns a msgtype to transfer_customer_service the message, the micro-trust server when receives this message, will be sent to the user this and a period of time to send messages forwarded customer service system.

The message returned is as follows

<xml>
 <tousername><![ Cdata[touser]]></tousername>
 <fromusername><![ cdata[fromuser]]></fromusername>
 <CreateTime>1399197672</CreateTime>
 <msgtype ><! [cdata[transfer_customer_service]]></msgtype>
</xml>

The message is implemented as follows (based on the square-times studio's micro-public platform PHP SDK)

Reply to multiple customer service messages
 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;
 }

Second, triggering multiple customer service sessions

Under normal circumstances, users want to consult the question is, often will ask "Hello", "in", such a question.

We use these words to trigger the keyword, when the user sends the text message content contains these words, will return the multiple customer service message to the user (the user does not feel any content in the micro-letter terminal, but the micro-letter public account will be the user this time and the later time 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, complete code

<?php/* Square Times Studio CopyRight 2014 All Rights Reserved * * Define ("TOKEN", "Weixin");
$WECHATOBJ = new Wechatcallbackapitest (); if (!isset ($_get[' echostr ')) {$WECHATOBJ->responsemsg ();} else{$wechatObj->valid ();} class Wechatcallbackapitest {//Verify message Public function valid () {$echoStr = $_get["Ech
  Ostr "];
   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": $conte Nt[] = Array ("Title" => "Welcome to Square Times Studio", "Description =>" use method: \n1. Send express number, such as 6367532560, can query Express details "," Picurl "=>" Yun_
    Qi_img/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; //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 multiple customer service mode else if (Strstr ($keyword, "Hello") | | strstr ($keyword, "Hello") | | strstr ($keyword, "in") | | strstr ($keyword, "there
   Person ") {$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 '], $i
  tem[' 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>  

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

Thank you for reading, I hope to help you, thank you for your support for this site!

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.