小黄鸡源码 java_java 实现小黄鸡智能聊天

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("苍井空"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值