Php WeChat public platform development mode implements Multi-Customer Service instance code and Public instances

Source: Internet
Author: User

Php public platform development mode implements Multi-Customer Service instance code and Public instances

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, however, someone asked me how to use this multi-customer service function today. 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:

<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 public platform php sdk of fangtimes studio)

// 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 ;}

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 functi On receiveEvent ($ object) {switch ($ object-> Event) {case "subscribe ": $ content [] = array ("Title" => "Welcome to fantimes Studio", "Description" => "Usage: \ n1. send a waybill number, such as 6367532560, you can query the parcel 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;} // receives the text message private function receiveText ($ 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, "hello") | strstr ($ keyword, "") | strstr ($ keyword, "") {$ 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> <HQMusicUrl> <! [CDATA [% s]> </HQMusicUrl> </Music> "; $ item_str = sprintf ($ itemTpl, $ musicArray ['title'], $ musicArray ['description'], $ musicArray ['musicurl'], $ musicArray ['hqmusicurl']); $ textTpl = "<xml> <ToUserName> <! [CDATA [% s]> </ToUserName> <FromUserName> <! [CDATA [% s]> </FromUserName> <CreateTime> % s </CreateTime> <MsgType> <! [CDATA [music]> </MsgType> $ item_str </xml> "; $ result = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (); return $ result;} // 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;} 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.

Thank you for reading this article. I hope it will 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.