package com.siyehuazhilian.control;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import org.json.JSONException;
import org.json.JSONObject;
import com.siyehuazhilian.message.resp.TextMessage;
import com.siyehuazhilian.service.BaiduTranslateService;
import com.siyehuazhilian.util.MessageUtil;
/**
* 其他功能:自动接入智能机器人接口
*
* @author Administrator
*
*/
public class OthersChoice {
/**
* 和用户聊天
*
* @param fromUserName
* 聊天用户
* @param toUserName
* 公众账号
* @param content
* 用户发送的聊天内容
* @return 机器人回复用户的聊天内容
*/
public static String getIMToUser(String fromUserName, String toUserName,
String content) {
// 回复文本消息
TextMessage textMessage = new TextMessage();
textMessage.setToUserName(fromUserName);
textMessage.setFromUserName(toUserName);
textMessage.setCreateTime(new Date().getTime());
textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
textMessage.setFuncFlag(0);
textMessage.setContent(xhj(content));
return MessageUtil.textMessageToXml(textMessage);
}
/**
* 发送http请求
*
* @param requestUrl
* 请求地址
* @return String
*/
private static String httpRequest(String requestUrl) {
StringBuffer buffer = new StringBuffer();
try {
URL url = new URL(requestUrl);
HttpURLConnection httpUrlConn = (HttpURLConnection) url
.openConnection();
httpUrlConn.setRequestMethod("GET");
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn
.setRequestProperty(
"Cookie",
"isFirst=1; isFirst=1; simsimi_uid=50731618; simsimi_uid=50731618; Filtering=0.0; Filtering=0.0; selected_nc=ch; selected_nc=ch; __utma=119922954.1552465493.1396404044.1396404044.1396404044.1; __utmz=119922954.1396404044.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); sid=s%3AJZ2jhsQmJwhHd0wtcvXrgGIL.c2ef9IQFKd96qSEweHm7BAi1FP425PUZ4NPjbWqb5pM");
httpUrlConn.connect();
// 将返回的输入流转换成字符串
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}
/**
* 小黄鸡API http://www.simsimi.com/func/reqN?lc=ch&ft=0.0&req=你是谁
*
* @param content
* @return
*/
public static String xhj(String content) {
String requestUrl = "http://www.simsimi.com/func/reqN?lc=ch&ft=0.0&req={content}";
// 对地区进行编码
requestUrl = requestUrl.replace("{content}",
BaiduTranslateService.urlEncodeUTF8(content));
// 查询并获取返回的结果
String json = httpRequest(requestUrl);
try {
JSONObject jsonObject = new JSONObject(json);
return jsonObject.getString("sentence_resp");
} catch (JSONException e) {
e.printStackTrace();
}
return "我了个去,我又脑残了,快告诉开发我的大帅哥";
}
public static void main(String[] args) {
System.out.println(xhj("苍井空"));
}
}