微信小黄鸡智能陪聊功能实现

来源:互联网 发布:郝斌c语言视频教程mp4 编辑:程序博客网 时间:2024/05/16 19:05

本文一共使用了两个机器人:小黄鸡,小逗比机器人,二者相互调用避免程序出现异常。已经本人调试,完全能用!

第一个(小黄鸡机器人):

package com.web;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class SimsimiTools {

    publicstatic void main(String[] args) {
       try {
           System.out.println(getSimsimiContentByNiuren("你是谁"));
       } catch (Exception e) {
           e.printStackTrace();
       }
    }

   
    publicstatic String getSimsimiContentByNiuren(String params) {
       StringBuffer bufferRes = new StringBuffer();
       try {
           URL realUrl = new URL(
                   "http://www.niurenqushi.com/app/simsimi/ajax.aspx");
           HttpURLConnection conn = (HttpURLConnection) realUrl
                   .openConnection();
           // 连接超时
           conn.setConnectTimeout(25000);
           // 读取超时 --服务器响应比较慢,增大时间
           conn.setReadTimeout(25000);

           HttpURLConnection.setFollowRedirects(true);
           // 请求方式
           conn.setRequestMethod("POST");
           conn.setDoOutput(true);
           conn.setDoInput(true);
           conn.setRequestProperty("User-Agent",
                   "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101Firefox/21.0");
           conn.setRequestProperty("Accept", "*
    publicstatic String removeNews(String sendMsgs) {
       // 去除广告
       if (sendMsgs.indexOf("sim") != -1) {
           sendMsgs ="偶是小黄鸡,女,还木有男友,欢迎亲们调戏  O(∩_∩)O";
       } else if (sendMsgs.indexOf("Database") != -1
               || sendMsgs.indexOf("Failed") != -1) {
           int random = (int) (Math.random() * 5);
           switch (random) {
           case 1:
               sendMsgs = "嗯";
               break;
           case 2:
               sendMsgs = "聊天其它的吧";
               break;
           case 3:
               sendMsgs = "嗯哼";
               break;
           case 4:
               sendMsgs = "哎呀";
               break;
           case 5:
               sendMsgs = "额";
               break;
           default:
               sendMsgs = "嗯";
               break;
           }
       } else if (sendMsgs.indexOf("Unauthorized access") != -1) {
           sendMsgs = "我怎么听不懂你说的什么意思呀[大哭]。咱们能换个话题吗!";
       } else if (sendMsgs.indexOf("你可以教我回答") != -1) {
           sendMsgs = "好吧";
       }
       // 替换部分内容
       sendMsgs = sendMsgs.replaceAll("傻逼", "sb");
       sendMsgs = sendMsgs.replaceAll("人家", "伦家");
       sendMsgs = sendMsgs.replaceAll("小逗比", "小黄鸡");
       
       return sendMsgs;
    }
}

第二个(小逗比机器人):

package com.web;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import weixin.sdk.common.HttpClientConnectionManager;


public class XiaoDouMachine {

    publicstatic void main(String[] args) {
       try {
           System.out.println(getXiaoDouMsg("你是谁"));
       } catch (Exception e) {
           e.printStackTrace();
       }
    }

    publicstatic String getXiaoDouMsg(String msg) {
       // 读取结果网页
       StringBuffer sbRes = new StringBuffer();
       HttpClient httpClient = new DefaultHttpClient();//创建httpClient对象
       try {
           // 请求超时
           httpClient.getParams().setParameter(
                   CoreConnectionPNames.CONNECTION_TIMEOUT, 20000);
           // 读取超时
           httpClient.getParams().setParameter(
                   CoreConnectionPNames.SO_TIMEOUT, 20000);
           
           HttpGet get = HttpClientConnectionManager.getGetMethod("http://www.xiaodoubi.com/bot/api.php?chat="+msg);

           HttpResponse responce = httpClient.execute(get);//得到responce对象
           int resStatu = responce.getStatusLine().getStatusCode();//返回码
           if (resStatu == HttpStatus.SC_OK) {// 200正常 其他就不对
            String txt =EntityUtils.toString(responce.getEntity(), "utf-8");
               sbRes.append(txt);
           }
       } catch (Exception e) {
           System.out.println("小逗比机器人接口调用出错!" + e.getMessage());
           return "小黄鸡被你玩坏了,~~~~(>_<)~~~~ ";
       } finally {
           httpClient.getConnectionManager().shutdown();
       }

       // 调用小九接口
       if (sbRes.toString().equals("") || sbRes.toString().equals("干嘛")){
        return "小黄鸡被你玩坏了,~~~~(>_<)~~~~ ";
       }

       String finalRes = removeNews(sbRes.toString());
       return finalRes;
    }

   
    publicstatic String removeNews(String sendMsgs) {
       if (sendMsgs.indexOf("xiaodoubi") != -1) {
           sendMsgs = "伦家不懂官人的话了啦~";
       } else if (sendMsgs.indexOf("sim") != -1) {
           sendMsgs = "伦家不懂官人的话了啦~";
       } else if (sendMsgs.indexOf("douqq") != -1) {
           sendMsgs = "伦家不懂官人的话了啦~";
       }
       sendMsgs = sendMsgs.replaceAll("傻逼", "sb");
       sendMsgs = sendMsgs.replaceAll("小逗比", "小黄鸡");
       return sendMsgs;
    }
}