介绍:使用图灵机器人来自动回复文本类的消息
(一):图灵官网注册、登录,然后获取key值(key是调用图灵回复接口的必须参数)
官网网址:http://www.tuling123.com/
登录后进入机器人设置,找到APIkey
(二):编写实现回复的代码
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mote.weixin.utils.CommonUtils;
public class TuLinService {
/**
* 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果
*
* @param content
* @return
*/
//对应刚才获取的APIkey
private static final String KEY = "cc11ec7832854becabfd7a6fd7c271a6";
private static final ObjectMapper MAPPER = new ObjectMapper();
public static String tulinResult(String content) {
String result = null;
try {
//图灵回复地址,content是用户发送的文本消息
String apiUrl = "http://www.tuling123.com/openapi/api?key=" + KEY + "&info=" + content;
//发送get请求,CommonUtils是前几篇中的内容
String resq = CommonUtils.Get(apiUrl);
//如果返回为空
if (null == resq) {
return "你的言语理解起来好难啊……";
}
//获取返回码
String code = MAPPER.readTree(resq).get("code").toString();
if (code.equals("100000")) {
//获取回复内容
result = MAPPER.readTree(resq).get("text").toString().replaceAll("\"", "");
}
return result;
} catch (Exception e) {
System.out.println("图灵自动回复失败");
e.printStackTrace();
}
return result;
}
}
(三):在文本消息处理中,调用图灵
在CoreService的文本消息处理中,调用图灵
// 文本消息
if (msgType.equals(MessageUtils.REQ_MESSAGE_TYPE_TEXT)) {
//调用图灵机器人
resqContext = TuLinService.tulinResult(map.get("Content"));
}