微信平臺添加小豆聊天機器人

  最近看小黃雞挺有意思的,就在網上找了一份代碼,也跟小黃雞差很少,後來將其加入了微信公衆平臺上效果很好。很少說了代碼以下: php


package com.xiaodoupp.com;



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 ChatRobot {


    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/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36");
            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 "奧 ,沒聽清呀 ,再說一遍唄";
            // 調用小九接口
        } finally {
            httpClient.getConnectionManager().shutdown();
        }


        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("傻逼", "大笨蛋").replaceAll("小九", "小泉")
                .replaceAll("小豆", "小泉").replaceAll("小賤豆", "小泉不是純一郎")
                .replaceAll("小賤", "小泉");
        return sendMsgs;
    }
}


小豆機器人網頁版:http://xiao.douqq.com/ html

進入這個網站輸入對話內容後點擊發送,抓包工具捕獲以下的內容(抓包工具用的fiddler2): java

除了第一行 其實都是httpHeader的內容了,上面的代碼Header裏面寫了 User-Agent 和 Referer ,還有一個參數用的 Content-type 這些屬性能夠去查看Http Header相關的資料,我想解釋,但我太清楚。最底下的 chat就是 post方法的參數了key 是chat ,valuess是馬丹(原本想寫Md)。 apache

這裏用的方法其實就是間接的用了小豆機器人的接口,是經過模擬網頁版post的內容,去向網頁請求返回數據。 微信

微信平臺中的效果圖: app

鳴謝: 微信公衆平臺

http://blog.csdn.net/lizhengnanhua/article/details/10033041 工具

相關文章
相關標籤/搜索