Java 机器人配置错误
我在做一个GTalk机器人。我还没做完,但到目前为止,我的机器人工作正常。自从我第一次运行机器人以来,有一件事让我很恼火。当我向我的gtalk bot发送IM消息时,它总是响应“bot配置错误”消息。我不明白为什么会这样。即使在我收到配置错误消息后,它仍然会像正常一样继续运行,并且运行正常。如果在收到错误消息几秒钟后发送另一条IM消息,则不会收到bot配置错误响应。如果我等几分钟让它“复位”。“重置”机器人后,如果我再次向机器人发送另一条IM消息。我将得到配置错误响应。我完全不知道为什么我一直收到这些错误消息。我的代码中甚至没有“Bot配置错误”字符串。它必须来自Smack API或GTalk服务器或其他东西 下面是我的代码片段:Java 机器人配置错误,java,xmpp,bots,google-talk,Java,Xmpp,Bots,Google Talk,我在做一个GTalk机器人。我还没做完,但到目前为止,我的机器人工作正常。自从我第一次运行机器人以来,有一件事让我很恼火。当我向我的gtalk bot发送IM消息时,它总是响应“bot配置错误”消息。我不明白为什么会这样。即使在我收到配置错误消息后,它仍然会像正常一样继续运行,并且运行正常。如果在收到错误消息几秒钟后发送另一条IM消息,则不会收到bot配置错误响应。如果我等几分钟让它“复位”。“重置”机器人后,如果我再次向机器人发送另一条IM消息。我将得到配置错误响应。我完全不知道为什么我一直收
import java.io.IOException;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.filter.*;
import org.apache.log4j.*;
public class GtalkBot {
private static final String username = "test@test.com"; // fake login info
private static final String password = "testpassword"; // fake password
private static final String domain = "gmail.com";
private static final int MAX_SLEEP_COUNT = 8;
private int sleepCount = 0;
private int sleepSec = 1000;
private XMPPConnection conn;
private GChatBufferProcessor chatProcessor;
private Presence presence;
private static final Logger logger = LoggerFactory.make();
public GtalkBot(){
presence = null;
ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222, domain);
//ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", 5222);
connConfig.setReconnectionAllowed(true);
conn = new XMPPConnection(connConfig);
chatProcessor = new GChatBufferProcessor(conn);
logger.info(this.getClass().getSimpleName()+" created");
} /* Gtalkbot */
public void run() {
while(true){
try {
login();
} catch (XMPPException xe){
logger.error("failed to connect to "+conn.getHost(), xe);
this.incrementalNap();
continue; // skip the rest of the loop and retry from the top
}
// start the chat message processor thread
chatProcessor.start();
// create gtalk chat listener
this.createChatListener();
while(conn.isConnected()){
/* stuck in this loop while connected, not sure if
this is the right way to do it. */
try {
Thread.sleep(500);
} catch (InterruptedException e){
//e.printStackTrace();
}
}
logger.warn("Lost connection, retrying the login process...");
}
} /* run */
public void login() throws XMPPException {
// connect to gtalk server
conn.connect();
logger.info("connected to "+conn.getHost());
// login to gtalk server
conn.login(username, password);
logger.info("logged in as: "+conn.getUser());
// set the presence status
presence = new Presence(Presence.Type.available);
logger.info("Set presence as: "+Presence.Type.available);
conn.sendPacket(presence);
} /* login */
public void createChatListener(){
GtalkMessageListener msgListener = new GtalkMessageListener();
//PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
PacketFilter filter = new PacketTypeFilter(Message.class);
conn.addPacketListener(msgListener, filter);
} /* createChatListener */
public void incrementalNap(){
if(sleepCount < MAX_SLEEP_COUNT){
sleepSec = sleepSec << 1; // multiply by 2
sleepCount++;
}
logger.debug("Sleeping for "+(sleepSec/1000)+" seconds ...");
try {
Thread.sleep(sleepSec);
} catch (InterruptedException e){
e.printStackTrace();
}
} /* incrementalNap */
}
import java.io.IOException;
导入org.jivesoftware.smack.*;
导入org.jivesoftware.smack.packet.*;
导入org.jivesoftware.smack.filter.*;
导入org.apache.log4j.*;
公共类GtalkBot{
私有静态最终字符串用户名=”test@test.com“;//假登录信息
私有静态最终字符串password=“testpassword”//假密码
私有静态最终字符串domain=“gmail.com”;
私有静态最终整数最大睡眠计数=8;
私有int sleepCount=0;
私有整数秒=1000;
专用XMPP连接连接器;
专用GChatBufferProcessor;
私人存在;
私有静态最终记录器Logger=LoggerFactory.make();
公共GtalkBot(){
存在=空;
ConnectionConfiguration ConnconConfig=新的ConnectionConfiguration(“talk.google.com”,5222,domain);
//ConnectionConfiguration ConnconConfig=新的ConnectionConfiguration(“talk.google.com”,5222);
connConfig.setReconnectionAllowed(true);
conn=新的XMPPConnection(connconConfig);
chatProcessor=新的GChatBufferProcessor(conn);
logger.info(this.getClass().getSimpleName()+“已创建”);
}/*Gtalkbot*/
公开募捐{
while(true){
试一试{
登录();
}捕获(XMPPException xe){
logger.error(“未能连接到”+conn.getHost(),xe);
这个.incrementalNap();
continue;//跳过循环的其余部分,然后从顶部重试
}
//启动聊天信息处理器线程
chatProcessor.start();
//创建gtalk聊天侦听器
this.createChatListener();
而(连接断开()){
/*连接时卡在该回路中,不确定是否
这是正确的做法*/
试一试{
睡眠(500);
}捕捉(中断异常e){
//e、 printStackTrace();
}
}
logger.warn(“连接丢失,正在重试登录过程…”);
}
}/*运行*/
public void login()抛出XMPPException{
//连接到gtalk服务器
连接();
logger.info(“连接到”+conn.getHost());
//登录到gtalk服务器
conn.login(用户名、密码);
logger.info(“登录身份:”+conn.getUser());
//设置状态
状态=新状态(状态.类型.可用);
logger.info(“将状态设置为:“+presence.Type.available”);
连接发送包(存在);
}/*登录*/
public void createChatListener(){
GtalkMessageListener msgListener=新的GtalkMessageListener();
//PacketFilter=newmessagetypefilter(Message.Type.chat);
PacketFilter过滤器=新的PacketTypeFilter(Message.class);
连接addPacketListener(msgListener,过滤器);
}/*createChatListener*/
public void incrementalNap(){
if(睡眠计数<最大睡眠计数){
睡眠秒