java實現小豆聊天機器人介面

來源:互聯網
上載者:User

package com.iask.webchat.chatmachine;

import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

/**
 * 小豆機器人
 */
public class XiaoDouMachine {

    public static void main(String[] args) {
        try {
            System.out.println(getXiaoDouMsg("哈哈"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static 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);

            HttpPost httppost = new HttpPost(
                    "http://xiao.douqq.com/bot/chat.php");
            String parmaStr = "chat=" + URLEncoder.encode(msg, "UTF-8");
            // System.out.println(parmaStr);
            StringEntity reqEntity = new StringEntity(parmaStr);
            reqEntity.setContentType("application/x-www-form-urlencoded");
            httppost.setHeader("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            httppost.setHeader("Referer", "http://xiao.douqq.com/");
            httppost.setEntity(reqEntity);

            HttpResponse responce = httpClient.execute(httppost);// 得到responce對象
            int resStatu = responce.getStatusLine().getStatusCode();// 返回碼
            if (resStatu == HttpStatus.SC_OK) {// 200正常 其他就不對
                // 獲得相應實體
                HttpEntity entity = responce.getEntity();
                String html = new String(EntityUtils.toString(entity).getBytes(
                        "ISO-8859-1"), "UTF-8");// 獲得html原始碼
                sbRes.append(html.trim());
            }
        } catch (Exception e) {
            System.out.println("小豆機器人介面調用出錯!" + e.getMessage());
            // 調用小九介面
            return XiaojiuMachine.getXiaojiuMsg(msg);
        } finally {
            httpClient.getConnectionManager().shutdown();
        }

        // 調用小九介面
        if (sbRes.toString().equals("") || sbRes.toString().equals("幹嘛")) {
            return XiaojiuMachine.getXiaojiuMsg(msg);
        }

        String finalRes = removeNews(sbRes.toString());
        System.out.println("小豆機器人回複:" + finalRes);
        return finalRes;
    }

    /**
     * 屏蔽當前介面中的廣告
     *
     * @return
     */
    public static String removeNews(String sendMsgs) {
        if (sendMsgs.indexOf("xiaodouqqcom") != -1) {
            sendMsgs = "倫家不懂官人的話了啦~";
        } else if (sendMsgs.indexOf("simsimi2") != -1) {
            sendMsgs = "倫家不懂官人的話了啦~";
        } else if (sendMsgs.indexOf("douqq") != -1) {
            sendMsgs = "倫家不懂官人的話了啦~";
        }
        sendMsgs = sendMsgs.replaceAll("傻逼", "sb").replaceAll("小九", "毛小驢")
                .replaceAll("小豆", "毛小驢").replaceAll("小賤豆", "毛小驢")
                .replaceAll("小賤", "毛小驢");
        return sendMsgs;
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.