zoukankan      html  css  js  c++  java
  • 微信多业务

             他做了一个微信开发个月,最近推出的微信客户服务能力.由于公司的公众微信号码认证,因此,有一个接口的权限.就在这个整合项目,本文仅适用于微通道的基础上发展.

    见微信开发文档

    假设公众号处于开发模式,须要在接收到用户发送的消息时,返回一个MsgType为transfer_customer_service的消息。微信server在收到这条消息时。会把这次发送的消息转到多客服系统。

    用户被客服接入以后。客服关闭会话曾经。处于会话过程中,用户发送的消息均会被直接转发至客服系统。

    上文中提到的消息数据详细举例为:

    <xml>
    <ToUserName><![CDATA[touser]]></ToUserName>
    <FromUserName><![CDATA[fromuser]]></FromUserName>
    <CreateTime>1399197672</CreateTime>
    <MsgType><![CDATA[transfer_customer_service]]></MsgType>
    </xml>
    
    參数 描写叙述
    ToUserName 开发人员微信号
    FromUserName 发送方帐号(一个OpenID)
    CreateTime 消息创建时间的时间戳 (整型)
    MsgType transfer_customer_service。表明这是一条多客服消息

    文档说得已经非常清楚了,直接上代码

    /**
     * 微信多客服操作
     * 
     * @author xuyw
     * @email xyw10000@163.com
     * @date 2014-06-12
     */


    1 构造xml发起客服请求,触发客服能够使用自己定义菜单或者keyword

    /**
         * 生成消息转发到多客服
         * @param touser
         * @param fromuser
         * @return
         */
    	public static String CreateRelayCustomMsg(String touser, String fromuser) {
    		StringBuilder relayCustomMsg = new StringBuilder();
    		relayCustomMsg.append("<xml>");
    		relayCustomMsg.append("<ToUserName><![CDATA["+touser+"]]></ToUserName>");
    		relayCustomMsg.append("<FromUserName><![CDATA["+fromuser+"]]></FromUserName>");
    		relayCustomMsg.append("<CreateTime>"+new Date().getTime()+"</CreateTime>");
    		relayCustomMsg.append("<MsgType><![CDATA[transfer_customer_service]]></MsgType>");
    		relayCustomMsg.append("</xml>");
    		return relayCustomMsg.toString();
    	}
    2 为了代码的简单演示将微信上随意操作都运行客服操作,ps实际开发则须要多重推断

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		request.setCharacterEncoding("UTF-8");
    		response.setContentType("text/html;charset=utf-8");
    		Map<String, String> requestMap = MessageUtil.parseXml(request);
    
    			// 发送方帐号(open_id)
    			String fromUserName = requestMap.get("FromUserName");
    			// 公众帐号
    			String toUserName = requestMap.get("ToUserName");
                            String    message = CustomMsgUtil.CreateRelayCustomMsg(fromUserName,toUserName);
    		
    		
    		PrintWriter out = response.getWriter();
    		out.print(message);
    		out.close();
    	}

    public static Map<String, String> parseXml(HttpServletRequest request) throws Exception {
    		// 将解析结果存储在HashMap中
    		Map<String, String> map = new HashMap<String, String>();
    
    		// 从request中取得输入流
    		InputStream inputStream = request.getInputStream();
    		// 读取输入流
    		SAXReader reader = new SAXReader();
    		Document document = reader.read(inputStream);
    		// 得到xml根元素
    		Element root = document.getRootElement();
    		// 得到根元素的全部子节点
    		List<Element> elementList = root.elements();
    
    		// 遍历全部子节点
    		for (Element e : elementList)
    			map.put(e.getName(), e.getText());
    
    		// 释放资源
    		inputStream.close();
    		inputStream = null;
    
    		return map;
    	}

    ok 代码编写完了 来看看执行效果吧

    先登录多客服系统

    打开微信 点击在线咨询

    多客服就收到一条请求,点击接入




    微信收到回复


  • 相关阅读:
    ThinkPHP 3.1.2 查询方式的一般使用1
    ThinkPHP 3.1.2 查询方式的一般使用1
    php 前台数据显示
    php 前台数据显示
    CURD 例子
    CURD 例子
    ThinkPHP 3 的CURD介绍
    华为云服务器实战 之 Gitlab安装与配置使用
    【Python3网络爬虫开发实战】1.3.4-tesserocr的安装
    【Python3网络爬虫开发实战】1.3.3-pyquery的安装
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4570037.html
Copyright © 2011-2022 走看看