开发者

Bot Configuration Error

开发者 https://www.devze.com 2023-01-29 15:13 出处:网络
I\'m working on a GTalk bot. I\'m not done with it, but so far, my bot are working okay. There\'s one thing that annoys me ever since I first ran the bot. When I send an IM message to my gtalk bot, it

I'm working on a GTalk bot. I'm not done with it, but so far, my bot are working okay. There's one thing that annoys me ever since I first ran the bot. When I send an IM message to my gtalk bot, it always responds the "Bot configuration error" message. I can't figure out why it's like that. Even after I get the config error message, it will still proceed like normal and runs okay. If I send another IM message few seconds after getting the error message, I will not get the bot configuration error response. If I wait few minutes to allow it to "reset". After "resetting" the bot, if I send another IM message to the bot again. I will get the config error response. I have absolutely no idea why I keep getting these error messages. I don't even have the "Bot configuration error" string anywhere in my code. It has to be from Smack API or from the GTalk server or something.

Here's the snippet of my code:

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 {
            开发者_如何学JAVA    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 */

}

Thanks in advance, guys.

0

精彩评论

暂无评论...
验证码 换一张
取 消