PHP WeChat development text automatic reply

不言
Release: 2023-03-24 11:20:02
Original
3191 people have browsed it

The content of this article is about automatic text reply in PHP WeChat development. It has certain reference value. Now I share it with you. Friends in need can refer to it

Today I would like to share with you the development of the WeChat automatic reply function. This time you need to prepare your own server (which can be accessed from the Internet), and carry out it on the public account

Server authentication and enable server configuration.


When users send messages to the official account, WeChat will send these messages to the development team in xml format url corresponding to the developer's server;

The developer receives xmlAfter the message is sent, it can be parsed, and then the corresponding content is sent back to the user according to the content of the message. The reply message must also end with ## Sent in #xml format

.

<xml> <ToUserName>< ![CDATA[toUser] ]></ToUserName> <FromUserName>< ![CDATA[fromUser] ]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType>< ![CDATA[text] ]></MsgType> <Content>< ![CDATA[你好] ]></Content> </xml>
Copy after login
ParameterIs it requiredDescription
ToUserName is the receiver account (received OpenID)
FromUserName is the developer WeChat ID
CreateTime is the message creation time (integer)
MsgType is text
Content is the message content of (line break: line breaks can be made in content, WeChat The client supports newline display)

One thing to note here is that the waiting time for WeChat to send a request to the developer server is 5 seconds. If the developer If the server 5 cannot reply within seconds,

WeChat will resend the request

(up to three times). After three times, Still cannot reply within 5 seconds or the content of the reply cannot be parsed by WeChat, and it will display "This official account cannot be

" Serve". If

cannot guarantee a reply within 5 seconds, you can reply with an empty string and WeChat will not do any processing on this message.

Because the types of messages sent by WeChat to the developer server are relatively diverse, including ordinary messages, follow events, unfollow events, button click events, etc. Therefore, when designing the automatic reply function, the flexibility, scalability and maintainability of the program must be fully considered.

Here I used the "chain of responsibility design pattern" to define a processing interface and let each message handler implement this interface; when receiving a request, send the request

The request is passed to the first handler class. Each request class contains a reference to the next handler class; if the request can be processed in this class, it will return to processing directly

The result, otherwise flows to the next handler class until the request is processed. The characteristic of this mode is that it decomposes the steps of processing requests and can make complex judgments

条件进行分解,同时每一个处理程序都只有一个单一的职责,对其进行修改不会影响到其他处理程序类。另外,将每一个请求类

xml文件的格式配置好,应用程序启动的时候,使用反射+IOC注入的方式实例化每一个处理程序类。

首先创建一个页面,replyText.html


我们创建两个数据表,

rule表 :用来存储回复数据,id自增长 , mp_id是当前正在使用的公众号,keyword是用户输入的关键字,type在此为text,reply_id与reply_text表建立连接,status为当前状态(是否正在使用)。



reply_text表 : reply_id作为主键,content为回复内容。

(在此注意一点,在页面输入对应值后,要把数据统一添加到两个数据表中,add()方法成功返回主键值,可利用这点进行两表关联添加)



public function replyText(){
        if(IS_GET){
            $this->display(&#39;replytext&#39;);
        }else{
            $mp = $this->mp;
            $mp_id = $mp[&#39;id&#39;];
            $data = I(&#39;post.&#39;);

            $textret = M(&#39;reply_text&#39;)->add($data);

            if($textret){
                $data[&#39;reply_id&#39;] = $textret;
                $data[&#39;mp_id&#39;] = $mp_id;
                $data[&#39;type&#39;] = &#39;text&#39;;
                if ($mp[&#39;is_use&#39;] == 1){
                    $data[&#39;status&#39;] = 1;
                }else{
                    $data[&#39;status&#39;] = 0;
                }
                $ret = M(&#39;rule&#39;)->add($data);
                if ($ret) {
                    $this->ajaxReturn(array(&#39;msg&#39;=>&#39;添加成功!&#39;));
                }else{
                    $this->ajaxReturn(array(&#39;msg&#39;=>$ret));
                }
            }
        }
    }
Copy after login


之前介绍过,我用的laneWeChat包,可以直接调用里边的方法,在wechatrequest.lib.php里的text方法中加入以下代码进行文本回复:

//获取哪个公众号发过来的请求
        $mp_id = $_GET[&#39;id&#39;];

        $content = $request[&#39;content&#39;];
        $where[&#39;mp_id&#39;] = $mp_id;
        $where[&#39;keyword&#39;] = $content;
        $data = M(&#39;rule&#39;)->where($where)->find();
        if ($data) {
            //发送消息中有这个关键字
            $reply_id = $data[&#39;reply_id&#39;];
            $type = $data[&#39;type&#39;];

            if ($type == "text") {
                $reply = M(&#39;reply_text&#39;)->find($reply_id);
                $reply_text = $reply[&#39;content&#39;];
                return ResponsePassive::text($request[&#39;fromusername&#39;], $request[&#39;tousername&#39;], $reply_text);
            }
            
        }else{
            return &#39;success&#39;;
        }
Copy after login

代码要一一写的话就有些多了,在此,只给小伙伴们分享以上代码,如果还有其他问题,欢迎留言提问哦~

请大家多多关注,我会时刻更新的!

相关推荐:

PHP微信开发之翻译功能

PHP WeChat development to obtain city weather

         

The above is the detailed content of PHP WeChat development text automatic reply. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!