微信公众号接入图灵机器人实现自动回复消息
2018-03-26 17:16
513 查看
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); } }结果:
相关文章推荐
- 微信公众号接入图灵机器人实现自动回复消息
- JAVA实现 springMVC方式的微信接入、实现消息自动回复实例
- JAVA实现 springMVC方式的微信接入、实现消息自动回复
- 【chrome插件】web版微信接入图灵机器人API实现自动回复
- [.NET] 使用 Senparc.Weixin 接入微信公众号开发:简单实现自动回复
- 实现微信公众号聊天机器人,自动回复——nodejs+图灵机器人
- 微信接入机器人实现对别人消息和群at消息的自动回复
- JAVA实现 springMVC方式的微信接入、实现消息自动回复
- JAVA实现 springMVC方式的微信接入、实现消息自动回复
- 微信公众号开发(一、接收用户消息自动回复)
- 微信公众号开发之——第三方自动回复机器人接入
- Python-快速实现微信公众号自动回复(聊天机器人)
- 微信公众号-增加智能自动回复的功能--使用图灵机器人
- 微信公众号实现回复图文消息
- 利用微信公众平台实现自动回复消息—java版
- C# asp.net 搭建微信公众平台(可实现关注消息与消息自动回复)的代码以及我所遇到的问题
- springMVC调微信接口实现关注时消息回复和自动回复功能
- NodeJS实现微信公众号关注后自动回复功能
- 详解nodejs微信公众号开发――4.自动回复各种消息
- 通过itchat创建图灵机器人实现微信自动回复