WeChat public platform development (12) Sending customer service messages_PHP tutorial

WBOY
Release: 2016-07-13 10:30:26
Original
1446 people have browsed it

When a user actively sends a message to the official account (including sending information, clicking on a custom menu, subscribing to events, scanning QR code events, payment success events, and user rights protection), WeChat will push the message data For developers, developers can call the customer service message interface within a period of time (currently modified to 48 hours) and send messages to ordinary users by POSTing a JSON data packet. There is no limit on the number of sending times within 48 hours. This interface is mainly used for functions such as customer service that require manual message processing, so that developers can provide users with better services.

The official documentation only provides an interface for sending customer service messages. Developers only need to POST a specific JSON data packet to reply to the message. Here, we plan to make a simple platform that can record user messages and display them in the form of a web page, and then respond to the messages.

First, we use the database to record the messages actively sent by users, and then extract them and display them on the page, and respond to the messages. Here we only discuss text messages. You can study other types of messages by yourself.

3.1 Create data table

Create a data table tbl_customer to record user messages.

<span --
--</span><span  表的结构 `tbl_customer`</span><span 
--
</span>
<span CREATE</span> <span TABLE</span><span  `tbl_customer` (
  `id` </span><span bigint</span>(<span 20</span>) unsigned <span NOT</span> <span NULL</span> AUTO_INCREMENT COMMENT <span '</span><span //消息ID</span><span '</span><span ,
  `from_user` </span><span char</span>(<span 50</span>) <span NOT</span> <span NULL</span> COMMENT <span '</span><span //消息发送者</span><span '</span><span ,
  `message` </span><span varchar</span>(<span 200</span>) <span NOT</span> <span NULL</span> COMMENT <span '</span><span //消息体</span><span '</span><span ,
  `time_stamp` </span><span datetime</span> <span NOT</span> <span NULL</span> COMMENT <span '</span><span //消息发送时间</span><span '</span><span ,
  </span><span PRIMARY</span> <span KEY</span><span  (`id`),
  </span><span KEY</span><span  `from_user` (`from_user`)
) ENGINE</span><span =</span>MyISAM  <span DEFAULT</span> CHARSET<span =</span>utf8 ;
Copy after login

3.2 Create sql.func.php file

Create the _query($_sql) {} function to perform INSERT operations.

<span function</span> _query(<span $_sql</span><span ){
    </span><span if</span>(!<span $_result</span> = <span mysql_query</span>(<span $_sql</span><span )){
        </span><span exit</span>('SQL执行失败'.<span mysql_error</span><span ());
    }
    </span><span return</span> <span $_result</span><span ;
}</span>
Copy after login

<span //</span><span 引入数据库处理函数</span>
<span require_once</span> 'sql.func.php'<span ;

</span><span function</span> _record_message(<span $fromusername</span>,<span $keyword</span>,<span $date_stamp</span><span ){
    </span><span //</span><span 调用_query()函数</span>
    _query("INSERT INTO tbl_customer(from_user,message,time_stamp) VALUES('<span $fromusername</span>','<span $keyword</span>','<span $date_stamp</span>')"<span );
}</span>
Copy after login

3.4 Processing and recording text messages

A. Introduce function files for reply text and function files for recording messages

<span //</span><span 引入回复文本的函数文件</span>
<span require_once</span> 'responseText.func.inc.php'<span ;
</span><span //</span><span 引入记录消息的函数文件</span>
<span require_once</span> 'record_message.func.inc.php';
Copy after login

B. Record the message into the database and return the message just sent to the user. Here, you can modify it to other text, such as: "Hello, the message has been received, we will reply to you as soon as possible!" etc. .

    <span //</span><span 处理文本消息函数</span>
    <span public</span> <span function</span> handleText(<span $postObj</span><span )
    {
        </span><span //</span><span 获取消息发送者,消息体,时间戳</span>
        <span $fromusername</span> = <span $postObj</span>-><span FromUserName;
        </span><span $keyword</span> = <span trim</span>(<span $postObj</span>-><span Content);
        </span><span $date_stamp</span> = <span date</span>('Y-m-d H:i:s'<span );

        </span><span if</span>(!<span empty</span>( <span $keyword</span><span  ))
        {
            </span><span //</span><span 调用_record_message()函数,记录消息入数据库</span>
            _record_message(<span $fromusername</span>,<span $keyword</span>,<span $date_stamp</span><span );
            
            </span><span $contentStr</span> = <span $keyword</span><span ;
            </span><span //</span><span 调用_response_text()函数,回复发送者消息</span>
            <span $resultStr</span> = _response_text(<span $postObj</span>,<span $contentStr</span><span );
            </span><span echo</span> <span $resultStr</span><span ;
        }</span><span else</span><span {
            </span><span echo</span> "Input something..."<span ;
        }
    }</span>
Copy after login

Our final effect is roughly as follows. The main work is in the "Information Management Center", other page layouts, etc. I will not go into details here. I will only explain the message display.

4.1 Specific implementation

Introduce the database operation file, execute the paging module, execute the database query, and assign the query results to $_result for use below.

<span //</span><span 引入数据库操作文件</span>
<span require_once</span> 'includes/sql.func.php'<span ;

</span><span //</span><span 分页模块</span>
<span global</span> <span $_pagesize</span>,<span $_pagenum</span><span ;
_page(</span>"SELECT id FROM tbl_customer",15);        <span //</span><span 第一个参数获取总条数,第二个参数,指定每页多少条</span>
<span $_result</span> = _query("SELECT * FROM tbl_customer ORDER BY id DESC LIMIT <span $_pagenum</span>,<span $_pagesize</span>");
Copy after login

Traverse $_result and insert it into the table one by one.

<span <</span><span form</span><span ></span>
    <span <</span><span table </span><span cellspacing</span><span ="1"</span><span ></span>
        <span <</span><span tr</span><span ><</span><span th</span><span ></span>消息ID<span </</span><span th</span><span ><</span><span th</span><span ></span>发送者<span </</span><span th</span><span ><</span><span th</span><span ></span>消息体<span </</span><span th</span><span ><</span><span th</span><span ></span>消息时间<span </</span><span th</span><span ><</span><span th</span><span ></span>操作<span </</span><span th</span><span ></</span><span tr</span><span ></span>
        <span <?</span><span php 
            while(!!$_rows = _fetch_array_list($_result)){
                $_html = array();
                $_html['id'] = $_rows['id'];
                $_html['from_user'] = $_rows['from_user'];
                $_html['message'] = $_rows['message'];
                $_html['time_stamp'] = $_rows['time_stamp'];
        </span><span ?></span>
        <span <</span><span tr</span><span ><</span><span td</span><span ></span><span <?</span><span php echo $_html['id']</span><span ?></span><span </</span><span td</span><span ><</span><span td</span><span ></span><span <?</span><span php echo $_html['from_user']</span><span ?></span><span </</span><span td</span><span ><</span><span td</span><span ></span><span <?</span><span php echo $_html['message']</span><span ?></span><span </</span><span td</span><span ><</span><span td</span><span ></span><span <?</span><span php echo $_html['time_stamp']</span><span ?></span><span </</span><span td</span><span ><</span><span td</span><span ><</span><span a </span><span href</span><span ="reply.php?fromusername=<?php echo $_html['from_user']?>&message=<?php echo $_html['message']?>"</span><span ><</span><span input </span><span type</span><span ="button"</span><span  value</span><span ="回复"</span> <span /></</span><span a</span><span ></</span><span td</span><span ></</span><span tr</span><span ></span>
        <span <?</span><span php 
            }
            _free_result($_result);
        </span><span ?></span>
    <span </</span><span table</span><span ></span>
<span </</span><span form</span><span ></span>
Copy after login

Note: After each message, there is a "reply" operation. Click this button to pass fromusername and the message sent by the user to the reply.php file to prepare for replying to the user's message. .

5.1 Create customer service message reply function file customer.php

The interface URL for sending customer service messages on WeChat is as follows:

https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
Copy after login

The JSON data packet format that requires POST is as follows:

<span {
    "touser":"OPENID",
    "msgtype":"text",
    "text":
    {
         "content":"Hello World"
    }
}</span>
Copy after login

So, according to the above tips, we write the processing function _reply_customer($touser,$content). When calling, pass in touser and the content to be replied, and then the customer service message can be sent.

<span function</span> _reply_customer(<span $touser</span>,<span $content</span><span ){
    
    </span><span //</span><span 更换成自己的APPID和APPSECRET</span>
    <span $APPID</span>="wxef78f22f877db4c2"<span ;
    </span><span $APPSECRET</span>="3f3aa6ea961b6284057b8170d50e2048"<span ;
    
    </span><span $TOKEN_URL</span>="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".<span $APPID</span>."&secret=".<span $APPSECRET</span><span ;
    
    </span><span $json</span>=<span file_get_contents</span>(<span $TOKEN_URL</span><span );
    </span><span $result</span>=json_decode(<span $json</span><span );
    
    </span><span $ACC_TOKEN</span>=<span $result</span>-><span access_token;
    
    </span><span $data</span> = '<span {
        "touser":"</span>'.<span $touser</span>.'<span ",
        "msgtype":"text",
        "text":
        {
             "content":"</span>'.<span $content</span>.'<span "
        }
    }</span>'<span ;
    
    </span><span $url</span> = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".<span $ACC_TOKEN</span><span ;
    
    </span><span $result</span> = https_post(<span $url</span>,<span $data</span><span );
    </span><span $final</span> = json_decode(<span $result</span><span );
    </span><span return</span> <span $final</span><span ;
}

</span><span function</span> https_post(<span $url</span>,<span $data</span><span )
{
    </span><span $curl</span> =<span  curl_init();
    curl_setopt(</span><span $curl</span>, CURLOPT_URL, <span $url</span><span ); 
    curl_setopt(</span><span $curl</span>, CURLOPT_SSL_VERIFYPEER, <span FALSE</span><span );
    curl_setopt(</span><span $curl</span>, CURLOPT_SSL_VERIFYHOST, <span FALSE</span><span );
    curl_setopt(</span><span $curl</span>, CURLOPT_POST, 1<span );
    curl_setopt(</span><span $curl</span>, CURLOPT_POSTFIELDS, <span $data</span><span );
    curl_setopt(</span><span $curl</span>, CURLOPT_RETURNTRANSFER, 1<span );
    </span><span $result</span> = curl_exec(<span $curl</span><span );
    </span><span if</span> (curl_errno(<span $curl</span><span )) {
       </span><span return</span> 'Errno'.curl_error(<span $curl</span><span );
    }
    curl_close(</span><span $curl</span><span );
    </span><span return</span> <span $result</span><span ;
}</span>
Copy after login

Next, we will introduce the function written above into the message reply page to implement the function of sending customer service messages.

5.2 Click the "Reply" button and jump to reply.php with the fromusername and message parameters.

5.3 reply.php page displays

5.4 reply.php file analysis

<span //</span><span 引入回复消息的函数文件</span>
<span require_once</span> '../customer.php';
Copy after login

The form is submitted to relpy.php itself, with action=relpy.

<span <</span><span form </span><span method</span><span ="post"</span><span  action</span><span ="reply.php?action=reply"</span><span ></span>
    <span <</span><span dl</span><span ></span>
        <span <</span><span dd</span><span ><</span><span strong</span><span ></span>收件人:<span </</span><span strong</span><span ><</span><span input </span><span type</span><span ="text"</span><span  name</span><span ="tousername"</span><span  class</span><span ="text"</span><span  value</span><span ="<?php echo $from_username?>"</span> <span /></</span><span dd</span><span ></span>
        <span <</span><span dd</span><span ><</span><span strong</span><span ></span>原消息:<span </</span><span strong</span><span ><</span><span input </span><span type</span><span ="text"</span><span  name</span><span ="message"</span><span  class</span><span ="text"</span><span  value</span><span ="<?php echo $message?>"</span> <span /></</span><span dd</span><span ></span>
        <span <</span><span dd</span><span ><</span><span span</span><span ><</span><span strong</span><span ></span>内 容:<span </</span><span strong</span><span ></</span><span span</span><span ><</span><span textarea </span><span rows</span><span ="5"</span><span  cols</span><span ="34"</span><span  name</span><span ="content"</span><span ></</span><span textarea</span><span ></</span><span dd</span><span ></span>
        <span <</span><span dd</span><span ><</span><span input </span><span type</span><span ="submit"</span><span  class</span><span ="submit"</span><span  value</span><span ="回复消息"</span> <span /></</span><span dd</span><span ></span>
    <span </</span><span dl</span><span ></span>
<span </</span><span form</span><span ></span>
Copy after login

action=reply action processing.

<span if</span>(<span $_GET</span>['action'] == "reply"<span ){
    </span><span $touser</span> = <span $_POST</span>['tousername'<span ];
    </span><span $content</span> = <span $_POST</span>['content'<span ];
    </span><span $result</span> = _reply_customer(<span $touser</span>, <span $content</span><span );
    
    </span><span if</span>(<span $result</span>->errcode == "0"<span ){
        _location(</span>'消息回复成功!', 'index.php'<span );
    }
}</span>
Copy after login

Note: POST method to obtain touser, content, and then call the _reply_customer($touser, $content) method to process. If the processing is successful, "Message reply successful!" will pop up, and then jump to index. php page to complete the process of sending customer service messages.

6.1 WeChat users send messages

6.2 Platform message management

6.3 Send customer service message

Send customer service message again

The test of sending customer service message was successful!

http://files.cnblogs.com/mchina/customer.rar

Sending customer service messages on WeChat is very simple. You only need to POST a JSON data packet to the specified interface URL. Here we have expanded it and written it into a simple platform to facilitate enterprise management. There are still many areas that need to be added and improved, such as recording messages sent by customer service; recording messages from the same user into a collection; implementing message replies in other formats, etc., which are left to the readers to think about and develop on their own.


David Camp

  • For business cooperation, please contact the author QQ: 562866602
  • My WeChat ID: mchina_tang
  • Write to me: mchina_tang@qq.com

We always believe that sharing is a virtue | We Believe, Great People Share Knowledge...

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/765825.htmlTechArticleWhen a user actively sends a message to the official account (including sending a message, clicking a custom menu, subscribing to an event, scanning QR code events, payment success events, user rights protection), WeChat will...
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