/**
* 聊天机器人,使用图灵机器人,也可以几个进行综合
* @author 熊诗言
*
*/
public class TalkRobot extends DefaultMessageHandler {
@Override
public BaseMessage handleByMe(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
String response = TulingService.talkResponse(content);
String str = "----";
/*
ResultSet rs = new SqlHelper().query("select * from message limit 0,5", (String[])null);
try {
rs.first();
str = rs.getString("content");
} catch (SQLException e) {
e.printStackTrace();
}//*/
return MessageFactory.createTextMessage(fromUserName, toUserName,content+"——>"+str+":"+response);
}
@Override
public boolean canDo(Map<String, String> requestMap) {
String msgType = requestMap.get("MsgType");
return MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType);
}
}
/**
* 图灵机器人Gson解析
* @author 熊诗言
*
*/
public class TulingService {
public static String talkResponse(String from) {
//http://www.tuling123.com/openapi/cloud/home.jsp注册后获取
String APIKEY = "08c70cb2cd195032412080a3d3f4866d";
HttpURLConnection connection = null;
StringBuffer sb = null;
try {
String INFO = URLEncoder.encode(from, "utf-8");
String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
URL getUrl = new URL(getURL);
connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));
sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
// 断开连接
connection.disconnect();
return text(sb.toString());
}
private static String text(String jsonStr){
// 通过Gson 工具将json 转换成TranslateResult 对象
TulingResult tulingResult = new Gson().fromJson(jsonStr, TulingResult.class);
// 取出tulingResult 中的回复
return tulingResult.getText();
}
public static void main(String[] args) {
System.out.println(talkResponse("梁启超的儿子的太太的情人的太太是谁"));
}
}
public class TulingResult {
private int code;
String text;
。。。。
}
* 聊天机器人,使用图灵机器人,也可以几个进行综合
* @author 熊诗言
*
*/
public class TalkRobot extends DefaultMessageHandler {
@Override
public BaseMessage handleByMe(Map<String, String> requestMap) {
String content = requestMap.get("Content").trim();
String response = TulingService.talkResponse(content);
String str = "----";
/*
ResultSet rs = new SqlHelper().query("select * from message limit 0,5", (String[])null);
try {
rs.first();
str = rs.getString("content");
} catch (SQLException e) {
e.printStackTrace();
}//*/
return MessageFactory.createTextMessage(fromUserName, toUserName,content+"——>"+str+":"+response);
}
@Override
public boolean canDo(Map<String, String> requestMap) {
String msgType = requestMap.get("MsgType");
return MessageUtil.REQ_MESSAGE_TYPE_TEXT.equals(msgType);
}
}
/**
* 图灵机器人Gson解析
* @author 熊诗言
*
*/
public class TulingService {
public static String talkResponse(String from) {
//http://www.tuling123.com/openapi/cloud/home.jsp注册后获取
String APIKEY = "08c70cb2cd195032412080a3d3f4866d";
HttpURLConnection connection = null;
StringBuffer sb = null;
try {
String INFO = URLEncoder.encode(from, "utf-8");
String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
URL getUrl = new URL(getURL);
connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));
sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
// 断开连接
connection.disconnect();
return text(sb.toString());
}
private static String text(String jsonStr){
// 通过Gson 工具将json 转换成TranslateResult 对象
TulingResult tulingResult = new Gson().fromJson(jsonStr, TulingResult.class);
// 取出tulingResult 中的回复
return tulingResult.getText();
}
public static void main(String[] args) {
System.out.println(talkResponse("梁启超的儿子的太太的情人的太太是谁"));
}
}
public class TulingResult {
private int code;
String text;
。。。。
}