JAVA public micro-channel number Website development - to forward the received message carrying the micro-channel customer service system

 

If the number is in the public development model, common user message to the public micro-channel number, the micro-channel server will first POST message to the developers fill url, can not be directly pushed to the micro-channel built-in customer service functions. If the user needs to push common message pushed to the customer service function, the code will need to be modified.

 

The official document: https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Forwarding_of_messages_to_service_center.html

 

Code:

 public void customer(HttpServletResponse response) {
        String msgType = "Get MsgType push message data";
        /**
         * Micro-channel is determined in the push message whether the event type MsgType
         * Not the type of event, we carried forward, because it only needs to be forwarded for the micro-channel message sent by the user, and for any other events (such as menu click, location reporting, etc.) should not transfer, or customer service in the customer service system on we'll see some meaningless news.
         * FromUsername to push messages fromUsername
         * ToUsername to push messages toUsername
         */
        String time = System.currentTimeMillis()+"";
        if (!"event".equalsIgnoreCase(msgType)) {
            String textTpl=customerText(fromUsername, toUsername, time);
            send(textTpl, response);
        }


    }

    /**
     * Send package xml
     * @Param fromUsername
     * @param toUsername
     * @param time
     * @return
     */
    private String customerText(String fromUsername,String toUsername,String time){
        String textTpls = "<xml>"+
                "<ToUserName><![CDATA["+fromUsername+"]]></ToUserName>"+
                "<FromUserName><![CDATA["+toUsername+"]]></FromUserName>"+
                "<CreateTime>"+time+"</CreateTime>"+
                "<MsgType><![CDATA[transfer_customer_service]]></MsgType>"+
                "</xml>";

        return textTpls;
    }

    private void send(String textTpl,HttpServletResponse response) throws IOException {
        String type="text/xml;charset=UTF-8";
        response.setContentType(type);
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.getWriter().write(textTpl);
    }

  

Guess you like

Origin www.cnblogs.com/pxblog/p/12610036.html