分享
 
 
 

JAVA MSN机器人主要代码开放

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

一直想把这个JAVA机器人的主要代码公开(虽然不是什么好东西~~但偶们要坚持开源~~提供给其他刚接触JAVA MSN的人参考)~~虽然偶和WENSI写的比较初级而且方法很多都比较傻,想在完善下在发布,不过昨天我尝到了不开源的弊端~~~我的系统突然坏了,C盘数据都没保留下来,其中就包括了以前MSN机器人的代码备份,如果我要是开源了还能在网上找到~~-_-!!

说明:先将java msn里的lib导入的包里,然后使用下面的代码,这个代码完成MSN机器人的操作。

(可能有比较笨的方法,变量方法的命名也不规范,还请指正,如果哪位会设置头像或建立数据库连接池请说说怎么做,现在这个机器人不能支持很多人,谢谢)

import java.sql.*;

import java.net.*;

import rath.msnm.*;

import rath.msnm.MSNMessenger;

import rath.msnm.SwitchboardSession;

import rath.msnm.UserStatus;

import rath.msnm.entity.Group;

import rath.msnm.entity.MsnFriend;

import rath.msnm.event.MsnAdapter;

import rath.msnm.msg.MimeMessage;

import java.util.*;

import com.google.soap.search.*;

import com.google.soap.search.GoogleSearch;

import com.google.soap.search.GoogleSearchResult;

import java.io.*;

/**

* MSN消息事件处理类

* @author 瞄瞄,wensi

*/

public class MSNAdapter

extends MsnAdapter {

MsnIn msnin = new MsnIn();

static String searchcon;

static String firstfriend;

static String friendlist;

String rewa = "";

String msg3;

MSNMessenger messenger;

String url = "jdbc:odbc:chat"; ////数据源

String login = ""; //用户名

String password = ""; //密码

Connection connection = null; // 声明Connection接口对象connection

ResultSet rSet = null; // 定义数据库查询的结果集

ResultSet keySet = null;

Statement statement = null; // 定义查询数据库的Statement对象

public MSNAdapter(MSNMessenger messenger) {

this.messenger = messenger;

}

public void msnmsg(String msg) {

msg3 = msg;

msnin.textArea2.append(msg3 + "\n");

}

/**

* 某人正在输入信息

*/

public void progressTyping(

SwitchboardSession ss,

MsnFriend friend,

String typingUser) {

System.out.println(friend.getLoginName() + "正在输入信息...");

}

/**

* 收到消息的时候执行该方法

*/

public void instantMessageReceived(

SwitchboardSession ss,

MsnFriend friend,

MimeMessage mime) {

System.out.print("接收到消息:" + friend.getFriendlyName() + "->");

System.out.println(mime.getMessage());

msnmsg(friend.getFriendlyName() + "说:" + mime.getMessage());

int first = 0;

if (firstfriend != null) {

StringTokenizer stt = new StringTokenizer(firstfriend, "$");

String[] subStrt2 = new String[stt.countTokens()];

int a = 0;

for (int i = 0; i < subStrt2.length; i++) {

subStrt2[i] = stt.nextToken();

if (subStrt2[i].equals(friend.getLoginName())) {

a = 1;

break;

}

}

if (a == 0) {

firstfriend = firstfriend + "$" + friend.getLoginName();

first = 1;

try {

mime.setMessage("|0_0| 我是机器人瞄,如果想问我问题请尽管问\r\n(co) 如果你想告诉我些知识请按照: study,问题内容,答案内容 的格式告诉我\r\n(I) 如果想使用google搜索请输入: google,搜索内容\r\n:-D 帮助请输入: help或?\r\n(M) 想和我的开发人员说话请加:icejmx@msn.com,wensi_feng@hotmail.com");

messenger.sendMessage(friend.getLoginName(), mime);

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:帮助");

}

catch (IOException ex1) {}

}

}

else {

firstfriend = firstfriend + "$" + friend.getLoginName();

first = 1;

try {

mime.setMessage("|0_0| 我是机器人瞄,如果想问我问题请尽管问\r\n(co) 如果你想告诉我些知识请按照: study,问题内容,答案内容 的格式告诉我\r\n(I) 如果想使用google搜索请输入: google,搜索内容\r\n:-D 帮助请输入: help或?\r\n(M) 想和我的开发人员说话请加:icejmx@msn.com,wensi_feng@hotmail.com");

messenger.sendMessage(friend.getLoginName(), mime);

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:帮助");

}

catch (IOException ex1) {}

}

if (first == 0) { //对话开始

if (mime.getMessage().equals("help") || mime.getMessage().equals("帮助") ||

mime.getMessage().equals("?")) { //是否帮助

mime.setMessage("|0_0| 我是机器人瞄,如果想问我问题请尽管问\r\n(co) 如果你想告诉我些知识请按照: study,问题内容,答案内容 的格式告诉我\r\n(I) 如果想使用google搜索请输入: google,搜索内容\r\n:-D 帮助请输入: help或?\r\n(M) 想和我的开发人员说话请加:icejmx@msn.com,wensi_feng@hotmail.com");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:帮助");

}

else { //是否帮助

if (mime.getMessage().indexOf(",") > 0) { //判断模式开始

StringTokenizer ms = new StringTokenizer(mime.getMessage(), ",");

int mspart;

mspart = ms.countTokens();

String[] message = new String[ms.countTokens()];

for (int i = 0; i < message.length; i++) {

message[i] = ms.nextToken();

}

if ( (mspart == 3) && (message[0].equals("学习")) ||

(message[0].equals("study")) || (message[0].equals("STUDY"))) { //学习模式

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connection = DriverManager.getConnection(url);

statement = connection.createStatement();

String sql1 =

"insert into holophrase (question,answer) values ('" +

message[1] +

"','" + message[2] + "')";

statement.executeUpdate(sql1);

mime.setMessage("学习完毕,在问问我啊。帮助请输入:? 或 help");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:学习完毕");

}

catch (Exception ex) {

ex.printStackTrace();

}

}

else if ( (mspart == 2) && (message[0].equals("google")) ||

(message[0].equals("GOOGLE"))) { //学习模式结束google搜索模式

String titleall = "";

if (searchcon != null) {

String searchcon2 = searchcon;

searchcon = "";

StringTokenizer stt = new StringTokenizer(searchcon2, "$");

String[] subStrt2 = new String[stt.countTokens()];

int a = 0;

int b = 0;

for (int i = 0; i < subStrt2.length; i++) {

subStrt2[i] = stt.nextToken();

b++;

if (subStrt2[i].equals(friend.getLoginName())) {

a = 1;

break;

}

}

if (a == 0) {

searchcon2 = searchcon2 + "$" + friend.getLoginName() + "$" +

message[1];

searchcon = searchcon2;

}

else {

subStrt2[b + 1] = message[1];

for (int i = 0; i < subStrt2.length; i++) {

searchcon = searchcon + "$" + subStrt2[i];

}

}

}

else {

searchcon = searchcon + "$" + friend.getLoginName() + "$" +

message[1];

}

try {

GoogleSearch search = new GoogleSearch();

search.setKey("AhGBLehQFHICcFbB5lYF9lDTZ4G71AzH");

search.setQueryString(message[1]);

GoogleSearchResult result = search.doSearch();

int numResults = result.getEstimatedTotalResultsCount();

int numResults2 = numResults / 10;

GoogleSearchResultElement[] resultElements = result.

getResultElements();

int startIndex = result.getStartIndex() - 1;

int endIndex = result.getEndIndex() - 1;

for (int i = startIndex; i <= endIndex; i++) {

GoogleSearchResultElement resultElement = resultElements[ (i)];

String title = resultElement.getTitle();

StringTokenizer st2 = new StringTokenizer(title, "<b>");

String[] subStr2 = new String[st2.countTokens()];

String title2 = "";

for (int i2 = 0; i2 < subStr2.length; i2++) {

subStr2[i2] = st2.nextToken();

title2 = title2 + subStr2[i2];

}

StringTokenizer st3 = new StringTokenizer(title2, "</b>");

String[] subStr3 = new String[st3.countTokens()];

String title3 = "";

for (int i3 = 0; i3 < subStr3.length; i3++) {

subStr3[i3] = st3.nextToken();

title3 = title3 + subStr3[i3];

}

String url = resultElement.getURL();

titleall = titleall + title3 + "\r\n" + url + "\r\n";

System.out.println(title);

System.out.println(url);

}

if (startIndex == 0) {

mime.setMessage(titleall + "\r\n搜索结果共" + numResults2 +

"页\r\n当前页:0\r\n看其他纪录输入:go->页数");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:google搜索成功");

}

else {

mime.setMessage(titleall + "\r\n搜索结果共" + numResults2 +

"页\r\n当前页:0\r\n看其他页纪录输入:go->页数;看第一页纪录输入:go->0");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:google搜索成功");

}

}

catch (Exception gsf) {

mime.setMessage("Google Search Fault: " + gsf); //.getMessage());

System.out.println("Google Search Fault: " + gsf); //.getMessage());

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:google搜索失败");

}

}

else { //google搜索模式结束数据库查询开始

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connection = DriverManager.getConnection(url);

statement = connection.createStatement();

String sqlStr1 =

"SELECT question,answer FROM holophrase WHERE question like '%" +

mime.getMessage() + "%'";

rSet = statement.executeQuery(sqlStr1);

if (rSet.next() == false) { //分词开始

int br = 0; //比较有结果为1

String key = mime.getMessage();

for (int i = key.length(); i > 1; i--) {

if (br == 1) {

break;

}

for (int num = 0; num + i <= key.length(); num++) {

if (br == 1) {

break;

}

String subS = key.substring(num, num + i);

String sqlKey = "select * from pivotal order by pivotalid";

rSet = statement.executeQuery(sqlKey);

while (rSet.next()) {

if (br == 1) {

break;

}

String pivotal = rSet.getString("pivotal");

StringTokenizer pivotal1 = new StringTokenizer(pivotal,

"/");

String[] pivotal2 = new String[pivotal1.countTokens()];

for (int i3 = 0; i3 < pivotal2.length; i3++) {

pivotal2[i3] = pivotal1.nextToken();

if (pivotal2[i3].equals(subS)) {

String p = rSet.getString("content");

System.out.println("分词选择:" + p + "\n");

mime.setMessage(p);

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" +

p);

br = 1;

break;

}

}

}

}

}

if (br == 0) { //如果比较没结果

int count = (int) (Math.random() * 78);

String sqlStr2 = "SELECT * FROM cope WHERE copeid =" + count +

"";

ResultSet rs = statement.executeQuery(sqlStr2);

rs.next();

String returnInfo1 = rs.getString(2);

mime.setMessage(returnInfo1 + "\r\n" +

"[如果你想告诉我正确的答案,请按照 study,问题,答案 的格式教我!-_-!]");

System.out.print(returnInfo1 + "\n");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" +

returnInfo1);

}

statement.close();

connection.close();

} //分词结束

else {

String s = "";

String s2 = rSet.getString(2);

int a = 0;

while (rSet.next()) {

s = s + "$" + rSet.getString(2);

a++;

}

if (a != 0) {

StringTokenizer stt = new StringTokenizer(s, "$");

String[] subStrt2 = new String[stt.countTokens()];

for (int i = 0; i < subStrt2.length; i++) {

subStrt2[i] = stt.nextToken();

}

int count;

count = (int) (Math.random() * a);

s = subStrt2[count];

}

else {

s = s2;

}

String returnInfo2 = "";

if (s.indexOf("$n") > 0) { //换行

StringTokenizer st = new StringTokenizer(s,

"$n");

String[] subStr = new String[st.countTokens()];

for (int i = 0; i < subStr.length; i++) {

subStr[i] = st.nextToken();

returnInfo2 = returnInfo2 + subStr[i] + "\r\n";

}

mime.setMessage(returnInfo2);

System.out.print(returnInfo2 + "\n");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" +

returnInfo2);

}

else {

mime.setMessage(s);

System.out.print(s + "\n");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" + s);

}

statement.close();

connection.close();

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

else if (mime.getMessage().indexOf("->") >= 0) { //判断模式结束 google搜索换页开始

String titleall = "";

StringTokenizer st2 = new StringTokenizer(searchcon, "$");

String[] subStr2 = new String[st2.countTokens()];

for (int i = 0; i < subStr2.length; i++) {

subStr2[i] = st2.nextToken();

}

int a = 0;

while (!subStr2[a].equals(friend.getLoginName())) {

a++;

}

StringTokenizer stk = new StringTokenizer(mime.getMessage(), "->");

String[] subStrk = new String[stk.countTokens()];

for (int i = 0; i < subStrk.length; i++) {

subStrk[i] = stk.nextToken();

}

try {

GoogleSearch search = new GoogleSearch();

search.setKey("AhGBLehQFHICcFbB5lYF9lDTZ4G71AzH");

search.setQueryString(subStr2[a + 1]);

int aa = Integer.parseInt(subStrk[1]);

if (aa * 10 < 0) {

mime.setMessage("错误,无法翻页");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:翻页错误");

}

else {

int startResult = aa * 10;

search.setStartResult(startResult);

int maxResult = 10;

search.setMaxResults(maxResult);

GoogleSearchResult result = search.doSearch();

int numResults = result.getEstimatedTotalResultsCount();

int numResults2 = numResults / 10;

GoogleSearchResultElement[] resultElements = result.

getResultElements();

int startIndex = result.getStartIndex() - 1 - startResult;

int endIndex = result.getEndIndex() - 1 - startResult;

for (int i = startIndex; i <= endIndex; i++) {

GoogleSearchResultElement resultElement = resultElements[ (i)];

String title = resultElement.getTitle();

StringTokenizer st3 = new StringTokenizer(title, "<b>");

String[] subStr3 = new String[st3.countTokens()];

String title2 = "";

for (int i2 = 0; i2 < subStr3.length; i2++) {

subStr3[i2] = st3.nextToken();

title2 = title2 + subStr3[i2];

}

StringTokenizer st4 = new StringTokenizer(title2, "</b>");

String[] subStr4 = new String[st4.countTokens()];

String title3 = "";

for (int i3 = 0; i3 < subStr4.length; i3++) {

subStr4[i3] = st4.nextToken();

title3 = title3 + subStr4[i3];

}

String url = resultElement.getURL();

titleall = titleall + title3 + "\r\n" + url + "\r\n";

System.out.println(title);

System.out.println(url);

}

if (startResult == 0) {

mime.setMessage(titleall + "\r\n搜索结果共" + numResults2 +

"页\r\n当前页:" + aa + "\r\n看其他纪录输入:go->页数");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:google搜索成功");

}

else {

mime.setMessage(titleall + "\r\n搜索结果共" + numResults2 +

"页\r\n当前页:" + aa +

"\r\n看其他页纪录输入:go->页数;看第一页纪录输入:go->0");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:google搜索成功");

}

}

}

catch (Exception gsf) {

mime.setMessage("Google Search Fault: " + gsf); //.getMessage());

System.out.println("Google Search Fault: " + gsf); //.getMessage());

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:google搜索失败");

}

}

else { //google搜索换页结束

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connection = DriverManager.getConnection(url);

statement = connection.createStatement();

String sqlStr1 =

"SELECT question,answer FROM holophrase WHERE question like '%" +

mime.getMessage() + "%'";

rSet = statement.executeQuery(sqlStr1);

if (rSet.next() == false) { //分词开始

int br = 0; //比较有结果为1

String key = mime.getMessage();

for (int i = key.length(); i > 1; i--) {

if (br == 1) {

break;

}

for (int num = 0; num + i <= key.length(); num++) {

if (br == 1) {

break;

}

String subS = key.substring(num, num + i);

String sqlKey = "select * from pivotal order by pivotalid";

rSet = statement.executeQuery(sqlKey);

while (rSet.next()) {

if (br == 1) {

break;

}

String pivotal = rSet.getString("pivotal");

StringTokenizer pivotal1 = new StringTokenizer(pivotal, "/");

String[] pivotal2 = new String[pivotal1.countTokens()];

for (int i3 = 0; i3 < pivotal2.length; i3++) {

pivotal2[i3] = pivotal1.nextToken();

if (pivotal2[i3].equals(subS)) {

String p = rSet.getString("content");

System.out.println("分词选择:" + p + "\n");

mime.setMessage(p);

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" +

p);

br = 1;

break;

}

}

}

}

}

if (br == 0) { //如果比较没结果

int count = (int) (Math.random() * 78);

String sqlStr2 = "SELECT * FROM cope WHERE copeid =" + count +

"";

ResultSet rs = statement.executeQuery(sqlStr2);

rs.next();

String returnInfo1 = rs.getString(2);

mime.setMessage(returnInfo1 + "\r\n" +

"[如果你想告诉我正确的答案,请按照 study,问题,答案 的格式教我!-_-!]");

System.out.print(returnInfo1 + "\n");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" +

returnInfo1);

}

statement.close();

connection.close();

} //分词结束

else {

String s = "";

String s2 = rSet.getString(2);

int a = 0;

while (rSet.next()) {

s = s + "$" + rSet.getString(2);

a++;

}

if (a != 0) {

StringTokenizer stt = new StringTokenizer(s, "$");

String[] subStrt2 = new String[stt.countTokens()];

for (int i = 0; i < subStrt2.length; i++) {

subStrt2[i] = stt.nextToken();

}

int count;

count = (int) (Math.random() * a);

s = subStrt2[count];

}

else {

s = s2;

}

String returnInfo2 = "";

if (s.indexOf("$n") > 0) { //换行

StringTokenizer st = new StringTokenizer(s,

"$n");

String[] subStr = new String[st.countTokens()];

for (int i = 0; i < subStr.length; i++) {

subStr[i] = st.nextToken();

returnInfo2 = returnInfo2 + subStr[i] + "\r\n";

}

mime.setMessage(returnInfo2);

System.out.print(returnInfo2 + "\n");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" +

returnInfo2);

}

else {

mime.setMessage(s);

System.out.print(s + "\n");

msnmsg("#机器人回答:" + friend.getFriendlyName() + ",内容:" + s);

}

statement.close();

connection.close();

}

}

catch (Exception ex) {

ex.printStackTrace();

}

}

}

try { //输出

messenger.sendMessage(friend.getLoginName(), mime);

}

catch (IOException ex1) {} //输出

} //对话结束

}

/**

* 登录成功后执行该方法

*/

public void loginComplete(MsnFriend own) {

System.out.println(own.getLoginName() + " Login OK");

msnmsg("*系统消息:" + own.getLoginName() + "登陆成功");

}

/**

* 登录失败后执行该方法

*/

public void loginError(String header) {

System.out.println("Login Failed: " + header);

msnmsg("*系统消息:登陆失败" + header);

}

/**

* 好友离线时执行该方法

*/

public void userOffline(String loginName) {

System.out.println("USER " + loginName + " Logout.");

msnmsg("*系统消息:" + loginName + "下线");

}

/**

* 好友上线时执行该方法

*/

public void userOnline(MsnFriend friend) {

System.out.println("USER " + friend.getFriendlyName() + " Login.");

msnmsg("*系统消息:" + friend.getFriendlyName() + "上线");

}

/**

* 有人加我为好友时执行

*/

public void whoAddedMe(MsnFriend friend) {

System.out.println("USER " + friend.getLoginName() + " Addme.");

msnmsg("*系统消息:" + friend.getLoginName() + " 将我加入好友");

try {

messenger.addFriend(friend.getLoginName());

msnmsg("*系统消息:" + friend.getLoginName() + " 加入我的好友");

}

catch (Exception e) {

e.printStackTrace();

}

}

/**

* 有人把我从好友列表中删除时执行

*/

public void whoRemovedMe(MsnFriend friend) {

System.out.println("USER " + friend.getLoginName() + " Remove me.");

msnmsg("*系统消息:" + friend.getLoginName() + " 将我删除");

try {

messenger.removeFriend(friend.getLoginName());

msnmsg("*系统消息:" + friend.getLoginName() + " 被我删除");

}

catch (Exception e) {

e.printStackTrace();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有