2016-10-28 30 views
0

我試圖做出openfire.But聊天機器人插件,當我運行Openfire的插件,然後我得到下面的錯誤是: -創建聊天機器人,但得到的錯誤

2016.10.28 18: 04: 21 org.jivesoftware.openfire.container.PluginManager - Error loading plugin: C: \Program Files(x86)\ Openfire\ plugins\ hospitalbot 
 
java.lang.NoSuchMethodError: org.jivesoftware.openfire.XMPPServerInfo.getName() Ljava/lang/String; 
 
at org.jivesoftware.openfire.botz.BotzConnection.login(BotzConnection.java: 319) 
 
at org.jivesoftware.openfire.botz.BotzConnection.login(BotzConnection.java: 272) 
 
at org.jivesoftware.openfire.plugin.ChatBot.initializePlugin(ChatBot.java: 75) 
 
at org.jivesoftware.openfire.container.PluginManager.loadPlugin(PluginManager.java: 447) 
 
at org.jivesoftware.openfire.container.PluginManager.access$300(PluginManager.java: 68) 
 
at org.jivesoftware.openfire.container.PluginManager$PluginMonitor.run(PluginManager.java: 1037) 
 
at org.jivesoftware.openfire.container.PluginManager.installPlugin(PluginManager.java: 176) 
 
at org.jivesoftware.openfire.admin.plugin_002dadmin_jsp._jspService(plugin_002dadmin_jsp.java: 180)

我有從下面的鏈接複製此插件: - 在以下文件 https://community.igniterealtime.org/docs/DOC-1130

我收到錯誤: -

package org.jivesoftware.openfire.botz; 
 

 
import java.net.InetAddress; 
 
import java.net.UnknownHostException; 
 

 
import org.jivesoftware.openfire.Connection; 
 
import org.jivesoftware.openfire.SessionManager; 
 
import org.jivesoftware.openfire.SessionPacketRouter; 
 
import org.jivesoftware.openfire.XMPPServer; 
 
import org.jivesoftware.openfire.roster.Roster; 
 
import org.jivesoftware.openfire.auth.AuthToken; 
 
import org.jivesoftware.openfire.auth.UnauthorizedException; 
 
import org.jivesoftware.openfire.net.VirtualConnection; 
 
import org.jivesoftware.openfire.session.ClientSession; 
 
import org.jivesoftware.openfire.session.LocalClientSession; 
 
import org.jivesoftware.openfire.spi.ConnectionConfiguration; 
 
import org.jivesoftware.openfire.user.UserAlreadyExistsException; 
 
import org.jivesoftware.openfire.user.UserNotFoundException; 
 
import org.jivesoftware.util.Log; 
 
import org.jivesoftware.util.StringUtils; 
 
import org.xmpp.packet.JID; 
 
import org.xmpp.packet.Packet; 
 
import org.xmpp.packet.StreamError; 
 

 
/** 
 
* The objective of BotzConnection class is to create a robot/bot application as 
 
* an internal user of the main XMPP server. The class's login methods performs 
 
* the necessary (virtual) connection to the server. The bot can login as an 
 
* anonymous or a real user. 
 
* 
 
* <p> 
 
* The class's object uses a BotzPacketReceiver object passed to it via one of 
 
* it's constructors or via calls to 
 
* {@link #setPacketReceiver(BotzPacketReceiver)} method to receive packets from 
 
* other XMPP entities to the bot. The bot can reply to these packets with 
 
* {@link #sendPacket(Packet)} method. Thus, a class that wants to handle bot 
 
* packets must implement {@link BotzPacketReceiver} class. 
 
* 
 
* <p> 
 
* Below is a sample parrot bot code snippet illustrating how to use 
 
* BotzConnection and BotzPacketReceiver: 
 
* 
 
* <blockquote> 
 
* 
 
* <pre> 
 
* * 
 
* * \t BotzPacketReceiver packetReceiver = new BotzPacketReceiver() { 
 
* * \t  BotzConnection bot; 
 
* * \t  public void initialize(BotzConnection bot) { this.bot = bot; } 
 
* * \t  public void processIncoming(Packet packet) { 
 
* * \t \t \t if (packet instanceof Message) { 
 
* * \t \t  \t packet.setTo(packet.getFrom()); 
 
* * \t \t  \t bot.sendPacket(packet); 
 
* * \t \t \t } 
 
* * \t \t } 
 
* * \t  public void processIncomingRaw(String rawText) {}; 
 
* * \t \t public void terminate() {}; 
 
* * \t }; 
 
* * 
 
* * \t BotzConnection bot = new BotzConnection(packetReceiver); 
 
* * \t try { 
 
* * \t  bot.login(&quot;MyUsername&quot;); 
 
* * \t  Presence presence = new Presence(); 
 
* * \t  presence.setStatus(&quot;Online&quot;); 
 
* * \t  bot.sendPacket(presence); 
 
* * \t } catch (Exception e) { 
 
* * \t } 
 
* * 
 
* </pre> 
 
* 
 
* </blockquote> 
 
* 
 
* @author Aznidin Zainuddin 
 
* @see BotzPacketReceiver 
 
*/ 
 
public class BotzConnection extends VirtualConnection { 
 
    /** 
 
    * The packet receiver object that will handle receiving of packets. 
 
    */ 
 
    private BotzPacketReceiver packetReceiver; 
 
    /** 
 
    * Holds the initialization state of the packet receiver. 
 
    */ 
 
    private boolean initPacketReceiver; 
 
    /** 
 
    * Holds the session for the bot. 
 
    */ 
 
    private LocalClientSession localClientSession; 
 

 
    private Roster roster; 
 

 
    private JID jid; 
 
    /** 
 
    * Creates a new instance of BotzConnection. 
 
    */ 
 
    public BotzConnection() {} 
 

 
    /** 
 
    * Creates a new instance of BotzConnection with the specified packet 
 
    * receiver. 
 
    * 
 
    * <p> 
 
    * When login is attempted with an instance created with this constructor, 
 
    * the packetReceiver traps incoming packets and texts as soon as the bot 
 
    * logs on. 
 
    * 
 
    * @param packetReceiver 
 
    *   BotzConnection packetReceiver 
 
    */ 
 
    public BotzConnection(BotzPacketReceiver packetReceiver) { 
 
    this.packetReceiver = packetReceiver; 
 
    } 
 

 

 
    public LocalClientSession getLocalClientSession() { 
 
    return localClientSession; 
 
    } 
 

 
    public Roster getRoster() { 
 
    return roster; 
 
    } 
 

 
    /** 
 
    * The method will be implicitly called by the server when the bot's 
 
    * connection is (virtually) closed. The method terminates the packet 
 
    * receiver. 
 
    */ 
 
    @ 
 
    Override 
 
    public void closeVirtualConnection() { 
 
    if (packetReceiver != null && initPacketReceiver) { 
 
     packetReceiver.terminate(); 
 
     initPacketReceiver = false; 
 
    } 
 
    } 
 

 
    /** 
 
    * Calls to this method is made by the server to deliver packets to the bot. 
 
    * This method will in turn call 
 
    * {@link BotzPacketReceiver#processIncoming(Packet)} of the packet receiver 
 
    * associated with the bot. 
 
    * 
 
    * @param packet 
 
    *   XMPP packet 
 
    * @throws UnauthorizedException 
 
    *    When packets could not be delivered due to authorization 
 
    *    problem. 
 
    */ 
 
    public void deliver(Packet packet) throws UnauthorizedException { 
 
    if (packetReceiver == null) 
 
     return; 
 
    packetReceiver.processIncoming(packet); 
 
    } 
 

 
    /** 
 
    * Calls to this method is made by the server to deliver raw text to the 
 
    * bot. This method will in turn call 
 
    * {@link BotzPacketReceiver#processIncomingRaw(String)} of the packet 
 
    * receiver associated with the bot. 
 
    * 
 
    * @param text 
 
    *   The text string delivered to the bot. 
 
    */ 
 
    public void deliverRawText(String text) { 
 
    if (packetReceiver == null) 
 
     return; 
 
    packetReceiver.processIncomingRaw(text); 
 
    } 
 

 
    /* 
 
    * (non-Javadoc) 
 
    * 
 
    * @see org.jivesoftware.openfire.Connection#getAddress() 
 
    */ 
 
    //@Override 
 
    public byte[] getAddress() throws UnknownHostException { 
 
    return InetAddress.getLocalHost().getAddress(); 
 
    } 
 

 
    /* 
 
    * (non-Javadoc) 
 
    * 
 
    * @see org.jivesoftware.openfire.Connection#getHostAddress() 
 
    */ 
 
    //@Override 
 
    public String getHostAddress() throws UnknownHostException { 
 
    return InetAddress.getLocalHost().getHostAddress(); 
 
    } 
 

 
    /* 
 
    * (non-Javadoc) 
 
    * 
 
    * @see org.jivesoftware.openfire.Connection#getHostName() 
 
    */ 
 
    //@Override 
 
    public String getHostName() throws UnknownHostException { 
 
    return InetAddress.getLocalHost().getHostName(); 
 
    } 
 

 
    /** 
 
    * Get the bot's packet receiver 
 
    * 
 
    * @return BotzPacketReceiver packetReceiver 
 
    */ 
 
    public BotzPacketReceiver getPacketReceiver() { 
 
    return packetReceiver; 
 
    } 
 

 
    /** 
 
    * Get the resource portion of the bot's JID. 
 
    * 
 
    * @return Resource portion of the bot's JID. 
 
    */ 
 
    public String getResource() { 
 
    if (localClientSession == null) 
 
     return null; 
 
    return localClientSession.getAddress().getResource(); 
 
    } 
 

 
    /** 
 
    * Get the node's portion of the bot's JID. 
 
    * 
 
    * @return Node portion of the bot's JID. 
 
    */ 
 
    public String getUsername() { 
 
    if (localClientSession == null) 
 
     return null; 
 
    return localClientSession.getAddress().getNode(); 
 
    } 
 

 
    /** 
 
    * Get the node's portion of the bot's JID. 
 
    * 
 
    * @return Node portion of the bot's JID. 
 
    */ 
 
    public JID getIdentity() { 
 
    if (localClientSession == null) 
 
     return null; 
 
    return localClientSession.getAddress(); 
 
    } 
 

 
    /** 
 
    * Check whether the bot session is still active. 
 
    * 
 
    * @return <tt>true</tt> if the bot is still active, <tt>false</tt> 
 
    *   otherwise. 
 
    */ 
 
    public boolean isLoggedOn() { 
 
    return !isClosed(); 
 
    } 
 

 
    /** 
 
    * Login to the XMPP server as an anonymous user. This method creates a 
 
    * virtual connection to the XMPP server and establish a user session. If 
 
    * the packet receiver is already defined, initialize it. 
 
    * 
 
    * @throws BotzSessionAlreadyExistsException 
 
    *    If the users session already exists. 
 
    */ 
 
    public void login() throws BotzSessionAlreadyExistsException { 
 
    if (isClosed()) 
 
     throw new BotzSessionAlreadyExistsException(); 
 
    localClientSession = (LocalClientSession) SessionManager.getInstance().getSession(jid); 
 
    localClientSession.setAnonymousAuth(); 
 
    if (packetReceiver != null) { 
 
     packetReceiver.initialize(this); 
 
     initPacketReceiver = true; 
 
    } 
 
    return; 
 
    } 
 

 
    /** 
 
    * A convenient way to login. It uses the default "Botz" as the JID resource 
 
    * and auto create the user if it doesn't exist. 
 
    * 
 
    * @param username 
 
    *   The username to login with. 
 
    * @throws BotzSessionAlreadyExistsException 
 
    *    If the bot's session already exists. 
 
    * @throws UserNotFoundException 
 
    *    If it fails to create the user. 
 
    * 
 
    * @see #login(String, String, boolean) 
 
    */ 
 
    public void login(String username) 
 
    throws BotzSessionAlreadyExistsException, UserNotFoundException { 
 
    login(username, "Botz", true); 
 
    } 
 

 
    /** 
 
    * A convenient way to login. It auto create the user if it doesn't exist. 
 
    * 
 
    * @param username 
 
    *   The username to login with. 
 
    * @param resource 
 
    *   The resource the user will bind to. 
 
    * @throws BotzSessionAlreadyExistsException 
 
    *    If the bot's session already exists. 
 
    * @throws UserNotFoundException 
 
    *    If it fails to create the user. 
 
    * 
 
    * @see #login(String, String, boolean) 
 
    */ 
 
    public void login(String username, String resource) 
 
    throws BotzSessionAlreadyExistsException, UserNotFoundException { 
 
    login(username, resource, true); 
 
    } 
 

 
    /** 
 
    * Login to the XMPP server and establish a non-anonymous user session using 
 
    * the given username and resource. When <tt>createIfNotExist</tt> is 
 
    * <tt>true</tt>, a new user with the username will be created and stored 
 
    * in the database if it does not exist. When <tt>false</tt>, and the 
 
    * user does not exist, the method will not attempt the login. Whenever 
 
    * there's an error, the bot will not login. 
 
    * 
 
    * @param username 
 
    *   Username to login with. 
 
    * @param resource 
 
    *   The resource the user will bind to. 
 
    * @param createIfNotExist 
 
    *   When specified as <tt>true</tt>, a new user will be created 
 
    *   and stored in the database if it does not exist. 
 
    * @throws BotzSessionAlreadyExistsException 
 
    *    If the bot's session already exists. 
 
    * @throws UserNotFoundException 
 
    *    If it fails to create the user. 
 
    */ 
 
    public void login(String username, String resource, boolean createIfNotExist) 
 
    throws BotzSessionAlreadyExistsException, UserNotFoundException { 
 
    if (isClosed()) 
 
     throw new BotzSessionAlreadyExistsException(); 
 

 
    jid = new JID(username.toLowerCase(), XMPPServer.getInstance().getServerInfo().getXMPPDomain(), resource); 
 
    ClientSession oldSession = XMPPServer.getInstance().getRoutingTable() 
 
     .getClientRoute(jid); 
 

 
    // Check for session conflict 
 
    if (oldSession != null) { 
 
     try { 
 
     oldSession.incrementConflictCount(); 
 
     int conflictLimit = SessionManager.getInstance() 
 
      .getConflictKickLimit(); 
 
     if (conflictLimit != SessionManager.NEVER_KICK) { 
 
      // Kick out the old connection that is conflicting with the 
 
      // new one 
 
      StreamError error = new StreamError(
 
      StreamError.Condition.conflict); 
 
      oldSession.deliverRawText(error.toXML()); 
 
      oldSession.close(); 
 
     } else 
 
      throw new BotzSessionAlreadyExistsException(); 
 
     } catch (Exception e) { 
 
     Log.error("Error during login", e); 
 
     } 
 
    } 
 

 
    if (!XMPPServer.getInstance().getUserManager().isRegisteredUser(
 
     jid.getNode())) { 
 
     if (createIfNotExist) { 
 
     try { 
 
      // Bot doesn't care of whatever password it is. 
 
      XMPPServer.getInstance().getUserManager().createUser(
 
      jid.getNode(), StringUtils.randomString(15), null, 
 
      null); 
 
     } catch (UserAlreadyExistsException e) { 
 
      // Ignore 
 
     } 
 
     } else { 
 
     throw new UserNotFoundException(); 
 
     } 
 
    } 
 

 
    localClientSession = (LocalClientSession) SessionManager.getInstance().getSession(jid); 
 
    localClientSession.setAuthToken(new AuthToken(jid.getNode()), jid 
 
     .getResource()); 
 
    if (packetReceiver != null) { 
 
     packetReceiver.initialize(this); 
 
     initPacketReceiver = true; 
 
    } 
 

 
    this.roster = XMPPServer.getInstance().getRosterManager().getRoster(username); 
 

 
    } 
 

 
    /** 
 
    * Logout the bot and destroy the active session. This method need not be 
 
    * called explicitly unless, for example, when callers need to refresh the 
 
    * assign a different username or resource (re-login). 
 
    */ 
 
    public void logout() { 
 
    close(); 
 
    } 
 

 
    /** 
 
    * Send a packet out to an XMPP entity. The packet must be one of 
 
    * <message/>, <iq/> or <presence/>. Callers need not specify the 
 
    * <tt>from</tt> attribute inside the packet because it will be 
 
    * automatically inserted with/replaced by the bot's real JID. 
 
    * 
 
    * @param packet 
 
    *   The packet to send. 
 
    */ 
 
    public void sendPacket(Packet packet) { 
 
    if (isClosed()) 
 
     throw new IllegalStateException("No valid session"); 
 
    SessionPacketRouter router = new SessionPacketRouter(localClientSession); 
 
    router.route(packet); 
 
    } 
 

 
    /** 
 
    * Assign a packet receiver ({@link BotzPacketReceiver}) object that will 
 
    * receive packets to the bot. The method can be called repeatedly if 
 
    * necessary to dynamically change different packet receivers during a 
 
    * login. If the previous packet receiver is in an initialized state during 
 
    * this call, it will be terminated; and the new packet receiver will be 
 
    * initialized. 
 
    * 
 
    * <p> 
 
    * If the previous packetReceiver is the same with the new one, this method 
 
    * will ignore the assignment. 
 
    * 
 
    * @param packetReceiver 
 
    *   The packetReceiver object 
 
    */ 
 
    public void setPacketReceiver(BotzPacketReceiver packetReceiver) { 
 
    if (this.packetReceiver == packetReceiver) 
 
     return; 
 
    if (this.packetReceiver != null && initPacketReceiver) { 
 
     this.packetReceiver.terminate(); 
 
     initPacketReceiver = false; 
 
    } 
 
    this.packetReceiver = packetReceiver; 
 
    if (!isClosed()) { 
 
     this.packetReceiver.initialize(this); 
 
     initPacketReceiver = true; 
 
    } 
 
    } 
 

 
    /** 
 
    * Calls to this method is made by the server to notify about server 
 
    * shutdown to the bot. 
 
    */ 
 
    public void systemShutdown() { 
 
    close(); 
 
    } 
 

 
    @ 
 
    Override 
 
    public ConnectionConfiguration getConfiguration() { 
 
    // TODO Auto-generated method stub 
 
    return null; 
 
    } 
 
}

任何一個可以請幫我,因爲我在我的Openfire福利局。

+0

哪個版本的Openfire當前正在使用中,您嘗試安裝該插件?其次是從上面的鏈接,你是否下載了插件的jar文件,或者你拿了源代碼並自己構建它? –

+0

我使用的是openfire 4.0.3版本。從上面的鏈接我已經下載了源代碼並構建自己。 –

+0

我認爲bot尚未針對較新版本進行定製,因爲您可以在討論中看到最後發佈的帖子中未提及任何人。但是,您總是可以使用smack api來創建自己的bot。 – dev

回答

0

我認爲bot尚未針對較新版本進行定製,因爲您可以在討論中查看末尾帖子中看到它不適用於任何人。但是您始終可以使用smack api創建自己的bot。

  1. 創建一個bot ID。
  2. 創建一個Java應用程序並使用Smack連接到Openfire
  3. 一旦您收到聊天並回復相同的消息,就使用業務邏輯。
+0

我現在正在做同樣的事情。但我堅持第二點。我無法使用Smack連接到openfire。 –

+0

謝謝Bro我的工作已經完成。 –

+0

很高興知道。玩機器人玩得開心。 – dev