This article introduces the content of the PHP development of the text of the automatic reply, has a certain reference value, now share to everyone, the need for friends can refer to
today to share with you the Auto-reply function development, in this time need to prepare their own server (can be accessed on the outside web), as well as in the public number
Server authentication is on, and the server configuration is turned on.
when a user sends a message to the public, the message is sent to the XML format is sent to the developer server corresponding to the URL above;
the developer receivesXMLLetterafter the message, you can parse it, and then send the content back to the user based on the content of the messages.XMLformat Send
Out of the.
<xml> <ToUserName><! [Cdata[touser]]></tousername> <FromUserName><! [Cdata[fromuser]]></fromusername> <CreateTime>12345678</CreateTime> <MsgType><! [Cdata[text]]></msgtype> <Content><! [cdata[Hello]]></content> </xml>
Parameters |
whether you must |
Description |
Tousername |
Is |
Recipient's account (OpenID received) |
Fromusername |
Is |
Developer Number |
Createtime |
Is |
Message creation time (integer type) |
Msgtype |
Is |
Text |
Content |
Is |
Reply Message content (newline: can wrap in content, the client supports line-wrapping display) |
there's aIt is important to note that the wait time to send a request to a developer server is5seconds, if the developer server5can not reply in seconds,
The request is resent (up to three times), three times no more 5
" is displayed.
Legal Services ". If you cannot guarantee A reply within 5 seconds, you can reply to an empty string without any processing of the message.
Because the types of messages sent to the developer server are more diverse, there are common messages, attention events, cancellation of attention events, button click events, and so on. So in the set
Take into account the flexibility, scalability, and maintainability of the program when it comes to automatic response.
Here I use the "responsibility chain design Mode", define a processing interface, so that each message handler to implement this interface, when the request is received, please
is passed to the first handler class, and each request class contains a reference to the next handler class, and if the request can be processed within that class, it is returned directly to the processing
As a result, it flows to the next handler class until the request is processed. The characteristic of this pattern is that the process of processing the request is decomposed, and the complex judgment can be
Conditions are decomposed, and each handler has only one single responsibility, and modifying it does not affect other handler classes. In addition, each request class is
with xml +IOC to instantiate each handler class in an injected manner.
First create a page, replytext.html
We create two data tables,
Rule table: Used to store reply data, id from growth, mp_id is currently in use of the public number, keyword is the user input keyword, type here for text,reply_id and Reply_text table to establish a connection, Status is the current state (whether it is in use).
Reply_text table: reply_id as the primary key, content is the reply contents.
(Note that after the page input corresponding value, to unify the data into two data tables, the Add () method successfully returns the primary key value, you can use this to add two tables association)
Public Function Replytext () { if (is_get) { $this->display (' Replytext '); } else{ $mp = $this->mp; $MP _id = $mp [' id ']; $data = I (' post. '); $textret = M (' Reply_text ')->add ($data); if ($textret) { $data [' reply_id '] = $textret; $data [' mp_id '] = $mp _id; $data [' type '] = ' text '; if ($mp [' is_use '] = = 1) { $data [' status '] = 1; } else{ $data [' status '] = 0; } $ret = M (' rule ')->add ($data); if ($ret) { $this->ajaxreturn (The Array (' msg ' = ' + ' is added successfully!) ')); } else{ $this->ajaxreturn (Array (' msg ' = $ret));}}}
Previously introduced, I used the Lanewechat package, you can call the inside of the method, in the text method in the wechatrequest.lib.php to add the following code for a text reply:
Obtain which public number is sent over the request $mp _id = $_get[' id ']; $content = $request [' content ']; $where [' mp_id '] = $mp _id; $where [' keyword '] = $content; $data = M (' rule ')->where ($where)->find (); if ($data) { //Send message has this keyword $reply _id = $data [' reply_id ']; $type = $data [' type ']; if ($type = = "Text") { $reply = M (' Reply_text ')->find ($reply _id); $reply _text = $reply [' content ']; return Responsepassive::text ($request [' Fromusername '], $request [' Tousername '], $reply _text); } } else{ return ' success '; }
Code to write a few words, in this, only to the small partners to share the above code, if there are other questions, welcome message to ask a question oh ~
Please pay more attention, I will be updated at all times!