1

我有一些简单的工作 java 代码,它使用 SMACK xmmp 库来响应传入的消息并接受多用户聊天邀请。我正在尝试转换这个clojure。机器人登录但不响应消息。

public class bot {

public static void main(String[] args) {

    System.out.println("Starting session...");
    try {
        String server = "chat.hipchat.com";         
        XMPPConnection con = new XMPPConnection(server);
        con.connect();
        String username = "username";
        String password = "password";
        con.login(username, password,"bot");
        System.out.println("Connected");

        ChatManager chatManager = con.getChatManager();
        final MyMessageListener messageListener = new MyMessageListener();

        ChatManagerListener  chatManagerListener = new ChatManagerListener() {
            public void chatCreated(Chat chat, boolean createdLocally) {
                chat.addMessageListener(messageListener);
            }
        };
        chatManager.addChatListener(chatManagerListener);


        MultiUserChat.addInvitationListener(con, new InvitationListener() {

            public void invitationReceived(XMPPConnection con,
                    String room, String arg2, String arg3, String arg4,
                    Message arg5) {

                MultiUserChat muc2 = new MultiUserChat(con, room);
                  try {
                    muc2.join("Bot");
                } catch (XMPPException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        // Thread.currentThread();
        Thread.sleep(10000);
        // Disconnect from the server
        con.disconnect();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Ended session...");
}
}

public class MyMessageListener implements MessageListener {

public void processMessage(Chat chat, Message message) {
    System.out.println("Received message: " + message.getBody());
    try {
        chat.sendMessage("Smack> Message sent via API.");
    } catch (XMPPException e) {
        e.printStackTrace();
    }
}

}


(ns mybot.core
(:import [org.jivesoftware.smack
        Chat ChatManager MessageListener ChatManagerListener XMPPConnection XMPPException]
       [org.jivesoftware.smack.packet
        Message Presence]
       [org.jivesoftware.smackx.muc InvitationListener MultiUserChat] ))

(def con (XMPPConnection. "chat.hipchat.com"))


(defrecord MyMessageListener []
   MessageListener
   (processMessage [this chat message] ( println "Received Message" )))


 (def myMessageListner ( MyMessageListener. ))



 (defn add-chatManagerListener
  [connection]
  (println "Adding a chat manager lister")
  (.addChatListener (.getChatManager connection)
                (proxy [ChatManagerListener] []
                  (chatCreated [chat locally]
                    (.addMessageListner chat myMessageListner )
                    )
                  )))




(defn -main [& args]
  (println "Starting bot")
  (.connect con)
  (.login con "username" "pwd" "bot")
 (println (.isAuthenticated con))
 (add-chatManagerListener con))
4

2 回答 2

0

(def ...)如果不需要,我强烈建议不要使用来维护全局状态。这总是适得其反。但也许这是某种个人品味。

Clojure 中的记录用作 bean,我很确定您不能使用 then 来实现接口上的方法。定义是:(defrecord name [& fields] & opts+specs)wherefields是字段名称列表,而不是方法名称。

要实例化 Java 接口或 Clojure 协议,您应该使用reify. proxy宏应该用于扩展抽象类或覆盖类上的函数。但是MessageListenerChatManagerListener只是接口,所以从我的角度来看reify应该是首选。

我附上了我的源代码版本,但我无法正确测试,因此请小心处理。

 (ns mybot.core
   (:import [org.jivesoftware.smack
        Chat ChatManager MessageListener ChatManagerListener XMPPConnection XMPPException]
        [org.jivesoftware.smack.packet
         Message Presence]
        [org.jivesoftware.smackx.muc InvitationListener MultiUserChat] ))

 (defn connect [host username password]
   (doto (XMPPConnection. "chat.hipchat.com")
    (.login username password "bot")))

 (defn add-listener [connection callback]
   (let [chat-manager (.getChatManager connection)
         message-listener (reify
           MessageListener
             (processMessage [_ chat message] (callback chat message))
           )
         chat-listener (reify 
           ChatManagerListener 
             (chatCreated [_ chat _]
               (.addMessageListner chat message-listener)))
     ]
      (.addChatListener chat-manager chat-listener)      
      connection
   ))

 (def -main [& args]
   (let [con (connect "chat.hipchat.com" "username" "pwd")]
     (println (.isAuthenticated con))
     (add-listener con (fn [_ message] (println "Received Message:" message)))

      (Thread/sleep 10000)))

一个常见的 Clojure 模式是将回调函数作为侦听器传递,我是为add-listener函数做的。这提供了很好的解耦并隐藏了所有讨厌的 OO 东西。

于 2015-09-17T12:14:28.507 回答
0

尽管不是您要的,但我建议您为此使用库。

一个不要脸的插件,有clj-xmpp,其中我做了一个fork https://github.com/slipset/xmpp-clj/大概可以满足你的需求。

于 2016-05-14T20:09:11.987 回答