您的位置:首页 > 移动开发 > 微信开发

微信企业号_开通企业客服功能实现

2016-06-14 14:15 676 查看

参考资料及文档

微信企业号开发者文档

http://qydev.weixin.qq.com/wiki/index.php?title=%E5%AE%A2%E6%9C%8D%E6%9C%8D%E5%8A%A1

需求

完成外部客服服务场景,主要完成客服系统与客服人员微信端的通讯功能。即下图中红框部分。



具体实现

微信企业号后台配置

登录微信企业号后台,点击—服务中心—>企业客服,选择外部企业客服,对相应项进行配置。

主要需要配置客服人员,即从企业号通讯录中选择作为客服的员工。还需配置回调URL,这个URL是当客服人员在他的手机微信端回复消息给客户时,将把消息发送到这个URL上,客服系统接收这个消息,并转发给相应的客户。

配置URL时涉及3个参数:

回调URL:配置的URL需要能够外网访问,只支持80端口。就是客服系统中接收客服人员通过手机微信端回复给客户的消息的接口。

Token:任意填写,可点击随机获取得到。

EncodingAESKey:任意填写,可点击随机获取得到。

以上两个参数用于对消息加解密用。后面的客服系统接收消息接口会用到。

接收消息接口的实现

package com.weixin.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.jfinal.core.Controller;
import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;

/**
* 企业客服-->微信被动调用
* @author JavaDev
*
*/
public class PassiveCallController extends Controller{
//企业号corpid
String sCorpID = "xxxxxxxxxxxxx";
//以下两项参数为在微信企业号后台  --> 企业客服 --> 外部客服 --> 回调接口中设置
String sToken = "xxxxxxxxxxxxxxxxxx";
String sEncodingAESKey = "xxxxxxxxxxxxxxxxxxxxx";

/**
* 接收企业号客服发送的消息,此方法为当企业号客服回复客户消息时微信企业号回调
* @throws IOException
* @throws AesException
*/
public void receiveMsg() throws IOException, AesException{
//获取到url上的参数值
String sReqMsgSig = getPara("msg_signature");
String sReqTimeStamp = getPara("timestamp");
String sReqNonce = getPara("nonce");
Stri
4000
ng sReqData = "";
BufferedReader in = null;
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(getRequest().getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sReqData += "\n" + line;
}
System.out.println("sReqData:" + sReqData);
//使用微信企业号提供的加解密库解析密文消息
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(sToken, sEncodingAESKey, sCorpID);
try {
//使用加解密库获取到明文xml格式信息
String sMsg = wxcpt.DecryptMsg(sReqMsgSig, sReqTimeStamp, sReqNonce, sReqData);
System.out.println("after decrypt msg: " + sMsg);
//解析出明文xml的内容
//获取到明文信息中packageId节点的值,并返回给微信企业号
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
StringReader sr = new StringReader(sMsg);
InputSource is = new InputSource(sr);
Document document = db.parse(is);
Element root = document.getDocumentElement();
NodeList nodelist1 = root.getElementsByTagName("PackageId");
//获得packageId节点的值,返回给微信企业号
String packageId = nodelist1.item(0).getTextContent();
System.out.println("packageId:" + packageId);
renderText(packageId);
} catch (Exception e) {
// 解密失败,失败原因请查看异常
System.out.println("解密失败,失败原因请查看异常");
e.printStackTrace();
}
}
}


接收消息接口的实现

package com.weixin.controller;

import org.eclipse.jetty.util.ajax.JSONObjectConvertor;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.util.HttpRequest;

/**
* 企业客服-->主动调用微信公众号接口
* @author JavaDev
*/
public class ActiveCallController extends Controller{

/**
* 企业号corpid 和 corpsecret,用于换取 AccessToken
* 由于这里需调用企业号客服系统接口,所以此处的参数值应来自微信企业号后台-->企业客服-->消息发送接口
*/
private String corpid = "xxxxxxxxxxxxxxxxxx";
private String corpsecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

/**
* 获取微信企业号access_token
* URL: https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=id&corpsecret=secrect */
public String getAccessToken(){
String accessToken = "";
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
String param = "corpid="+ corpid +"&corpsecret="+ corpsecret;
try {
String result = HttpRequest.sendGet(url, param);
System.out.println("result:"+result);
JSONObject jObject = JSONObject.parseObject(result);
String errcode = jObject.getString("errcode");
if(errcode != null || !"".equals(errcode)){
accessToken = jObject.getString("access_token");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("获取 AccessToken 失败!");
}
return accessToken;
}

/**
* 发送消息到指定客服,此方法为当客户向客服发起消息时,经过客服系统的本方法转发到指定客服的手机微信端
* URL: https://qyapi.weixin.qq.com/cgi-bin/kf/send?access_token=ACCESS_TOKEN */
public void sendMsg(){
String accessToken = getAccessToken();
String url = "https://qyapi.weixin.qq.com/cgi-bin/kf/send";
url += "?access_token="+ accessToken;
if(!"".equals(accessToken) || accessToken == null){
String param = "access_token="+ accessToken;
}
String msg = "{\"sender\":{\"type\": \"openid\",\"id\": \"xxxxxxxxxxxxxxxxx\"},"+
"\"receiver\":{\"type\": \"kf\",\"id\": \"xxxxxxxx\"},\"msgtype\": \"text\","+
"\"text\":{\"content\":\"hello!-------------\"}}";
String result = HttpRequest.sendPost(url, msg);
System.out.println("result:" + result);
renderText("hello------------");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: