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>
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 = $_ge
t["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 Square Times Studio", "Description" => "use method: \n1. Send express number, such as 6367532560, can inquire 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
, "someone Else") {$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 '],
$item [' 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.
This article writes the bad, also hoped Haihan, has the good opinion welcome to share, everybody learns the progress together. At the same time, thank you for your continued support for the cloud-dwelling community website.