1、创建图灵机器人
进入图灵机器人网址:http://www.tuling123.com/,.......->点击创建机器人
接下来选择或填写机器人的相关属性,这里我选择的是聊天社交,模拟真人聊天的机器人,应用终端由于是微信公众号接入,机器人设置里面,我们可以拿到接口api相关的信息。直接上图:
2、后端代码开始接入
常量类
public final class Constants {
/**
* GET或POST必须大写,不可更改
*/
public static final String GET = "GET";
public static final String POST = "POST";
/* 微信请求消息类型(由微信方规定,不可更改) */
/**
* 文本
*/
public static final String REQ_TEXT_TYPE = "text";
public static final String REQ_EVENT_TYPE = "event";
public static final String REQ_SUBSCRIBE_TYPE = "subscribe";
public static final String REQ_UNSUBSCRIBE_TYPE = "unsubscribe";
public static final String RESP_TEXT_TYPE = "text";
/**
* 图文
*/
public static final String RESP_NEWS_TYPE = "news";
/* 图灵机器人返回数据类型状态码(官方固定) */
/**
* 文本
*/
public static final Integer TEXT_CODE = 100000;
public static final Integer TRAIN_CODE = 305000;
public static final Integer FLIGHT_CODE = 306000;
public static final Integer LINK_CODE = 200000;
public static final Integer NEWS_CODE = 302000;
public static final Integer MENU_CODE = 308000;
/**
* key的长度错误(32位)
*/
public static final Integer LENGTH_WRONG_CODE = 40001;
/**
* 请求内容为空
*/
public static final Integer EMPTY_CONTENT_CODE = 40002;
/**
* key错误或帐号未激活
*/
public static final Integer KEY_WRONG_CODE = 40003;
/**
* 当天请求次数已用完
*/
public static final Integer NUMBER_DONE_CODE = 40004;
/**
* 暂不支持该功能
*/
public static final Integer NOT_SUPPORT_CODE = 40005;
/**
* 服务器升级中
*/
public static final Integer UPGRADE_CODE = 40006;
/**
* 服务器数据格式异常
*/
public static final Integer DATA_EXCEPTION_CODE = 40007;
/**
* 获取access_token的接口地址
*/
public final static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=";
/**
* 图灵机器人接口地址
*/
public final static String TURING_API_URL = "http://www.tuling123.com/openapi/api";
private Constants() {
}
}
服务层处理文本消息
@Service
public class TextMessageHandle {
/**
* 处理文本消息
*
* @param customName 用户
* @param severName 微信服务器
* @param textContent 文本内容
* @return
* @throws Exception
*/
public String processMessage(String customName, String severName, String textContent) throws Exception {
String fromUserName = customName;
String toUserName = severName;
String content = textContent;
String info = URLEncoder.encode(content, "utf-8");
String requestUrl = Constants.TURING_API_URL + "?key=" + AppConstants.API_KEY
+ "&info=" + info + "&userid=" + fromUserName;
String result = HttpUtil.get(requestUrl);
Object obj = MessageUtil.processTuRingResult(result, toUserName,
fromUserName);
return MessageUtil.ObjectToXml(obj);
}
}
文本消息类
@XStreamAlias("xml")
public class TextMessage extends BaseMessage{
@XStreamAlias("Content")
@XStreamCDATA
private String Content;
public TextMessage() {
}
public TextMessage(String fromUserName, String toUserName, String content) {
super(fromUserName, toUserName);
super.setMsgType(Constants.RESP_TEXT_TYPE);
this.Content = content;
}
public String getContent() {
return Content;
}
public void setContent(String content) {
Content = content;
}
}
基础消息
public class BaseMessage implements Serializable {
@XStreamAlias("ToUserName")
@XStreamCDATA
private String ToUserName;
@XStreamAlias("FromUserName")
@XStreamCDATA
private String FromUserName;
@XStreamAlias("CreateTime")
private Long CreateTime;
@XStreamAlias("MsgType")
@XStreamCDATA
private String MsgType;
public BaseMessage() {
super();
}
public BaseMessage(String fromUserName, String toUserName) {
super();
FromUserName = fromUserName;
ToUserName = toUserName;
CreateTime = System.currentTimeMillis();
}
public String getToUserName() {
return ToUserName;
}
public void setToUserName(String toUserName) {
ToUserName = toUserName;
}
public String getFromUserName() {
return FromUserName;
}
public void setFromUserName(String fromUserName) {
FromUserName = fromUserName;
}
public Long getCreateTime() {
return CreateTime;
}
public void setCreateTime(Long createTime) {
CreateTime = createTime;
}
public String getMsgType() {
return MsgType;
}
public void setMsgType(String msgType) {
MsgType = msgType;
}
}
应用常量
public final class AppConstants {
/**
* 应用id
*/
public static String APP_ID = "";
/**
* 应用秘钥
*/
public static String APP_SECRET = "";
/**
* 令牌
*/
public static String TOKEN = "";
/**
* 图灵机器人应用key
*/
public static String API_KEY = "";
}
最后服务层处理来自文本消息
else if (MsgType.TEXT.getValue().equals(msgType)) {
//点击菜单
//回复微信服务器成功
try {
String result;
result = textMessageHandle.processMessage(custermname, servername, content);
writeText(result, response);
}
} catch (Exception e) {
logger.error("接收来至微信服务器的消息出现错误", e);
writeText(MessageUtil.ObjectToXml(new TextMessage(custermname,
servername, "我竟无言以对!")), response);
e.printStackTrace();
}
private void writeText(String content, HttpServletResponse response) {
Writer writer = null;
try {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
writer = response.getWriter();
writer.write(content);
writer.flush();
} catch (IOException e) {
logger.error("响应客户端文本内容出现异常", e);
} finally {
IOUtils.close(writer);
}
}
结果: