Skip to content
This repository has been archived by the owner on Apr 20, 2018. It is now read-only.

Commit

Permalink
Fixing issue where private messaging jarvis would not reply with the …
Browse files Browse the repository at this point in the history
…executed command
  • Loading branch information
Chauncey committed Jun 11, 2013
1 parent 34077d0 commit 841e775
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(defproject com.rallydev/jarvis-core "1.0.0"
(defproject com.rallydev/jarvis-core "1.1.0"
:description "A robot for FlowDock"
:url "http://github.com/RallySoftware/jarvis-core"
:license {:name "The MIT License (MIT)"
:url "http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.5.0"]
[com.rallydev/clj-flowdock "1.1.0"]
[com.rallydev/clj-flowdock "1.1.1"]
[org.codehaus.groovy/groovy-all "2.1.0"]
[org.clojure/java.classpath "0.2.0"]
[org.clojure/tools.logging "0.2.6"]
Expand All @@ -15,5 +15,6 @@
:java-source-paths ["src/main/java"]
:test-paths ["src/test/clojure"]
:resource-paths ["src/main/resources"]
:main jarvis.bot
:plugins [[lein-clojars "0.9.1"]]
:jvm-opts ["-Xmx1G" "-DLOG_DIR=./logs"])
17 changes: 11 additions & 6 deletions src/main/clojure/jarvis/bot.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns jarvis.bot
(:require [jarvis.command :as command]
[jarvis.bot :as jarvis]
[jarvis.plugins :as plugins]
[jarvis.util :as util]
[clj-flowdock.api.flow :as flow]
Expand All @@ -13,7 +14,7 @@
(def threadpool (atom (Executors/newFixedThreadPool (util/config-property "THREAD_POOL_SIZE" 100))))

(defmacro listen [[flow message-sym flow-con-sym] & body]
`(with-open [~flow-con-sym (streaming/open (flow/flow->flow-id ~flow))]
`(with-open [~flow-con-sym (streaming/open ~flow)]
(loop []
(when-let [~message-sym (.read ~flow-con-sym)]
~@body)
Expand All @@ -37,12 +38,13 @@

(defn user-stream [plugins]
(listen ["" msg flow-connection]
(cond
(command/join-command? msg) (init-flow-thread (get msg "content") plugins)
(command/private-message? msg) (command/private-message msg (command/command->plugin msg plugins)))))
(let [enhanced-message (util/enhance-message msg)]
(cond
(command/join-command? enhanced-message) (init-flow-thread (get enhanced-message "content") plugins)
(command/private-message? enhanced-message) (command/private-message enhanced-message (command/command->plugin enhanced-message plugins))))))

(defn init-flow-thread [flow plugins]
(.submit @threadpool #(flow-stream flow plugins)))
(.submit @threadpool #(flow-stream (flow/flow->flow-id flow) plugins)))

(defn init-user-thread [plugins]
(.submit @threadpool #(user-stream plugins)))
Expand All @@ -56,4 +58,7 @@
(log/info "Starting Jarvis...")
(when-let [plugins (plugins/load-plugins)]
(log/info "Starting to read from flowdock streams")
(init-threads plugins)))
(init-threads plugins)))

(defn -main []
(jarvis/init))
17 changes: 11 additions & 6 deletions src/main/clojure/jarvis/command.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@
(.startsWith content "~~")))

(defn private-message? [message]
(contains? message "to"))
(and
(contains? message "to")
(= "message" (get message "event"))))

(defn leave-command? [message]
(when-let [content (m/content message)]
(and
(not (map? content))
(.equals content "~leave"))))

(defn join-command? [msg]
(= "flow-add" (get msg "event")))
(defn join-command? [message]
(= "flow-add" (get message "event")))

(defn parse-command [message-content]
(let [content-vec (s/split message-content #" ")
Expand All @@ -54,9 +56,12 @@
(get-in message ["user" "id"]))

(defn get-user-ids-from-tags [message]
(->> (message "tags")
(filter #(.startsWith % ":user:"))
(map #(user->id %))))
(if (< 0 (count (message "tags")))
(->> message
(message "tags")
(filter #(.startsWith % ":user:"))
(map #(user->id %)))
(get-user-id-from-message message)))

(defn- user->id [user]
(re-find #"\d+" user))
14 changes: 13 additions & 1 deletion src/main/clojure/jarvis/test_util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@
"content" {"id" "rally-software:devellopers",
"name" "developers",
"organization" "Rally Software",
"open" true}})
"open" true}})

(defn flow-user-typing-message []
{"app" nil,
"user" 29988,
"attachments" [],
"sent" 1370968225230,
"tags" [],
"content" {"typing" "29988-35899-chat-input"},
"event" "activity.user",
"id" 283833720,
"to" 35899,
"uuid" nil})
14 changes: 9 additions & 5 deletions src/test/clojure/jarvis/command_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@

(deftest get-user-id-from-message
(is (= :29983
(command/get-user-id-from-message (test-util/private-message "~help")))))
(command/get-user-id-from-message (test-util/private-message "~help"))))
(is (= :29983
(command/get-user-id-from-message (test-util/message "~help")))))

(deftest leave-command?
(is (= true
Expand All @@ -47,14 +49,16 @@
(is (= true
(command/join-command? (test-util/flow-add-message))))
(is (= false
(command/leave-command? (test-util/message "test"))))
(command/join-command? (test-util/message "test"))))
(is (= false
(command/leave-command? (test-util/private-message "foo")))))
(command/join-command? (test-util/private-message "foo")))))

(deftest private-message?
(is (= true
(command/private-message? (test-util/private-message "private message"))))
(is (= false
(command/leave-command? (test-util/message "test"))))
(command/private-message? (test-util/message "test"))))
(is (= false
(command/private-message? (test-util/flow-add-message))))
(is (= false
(command/leave-command? (test-util/flow-add-message)))))
(command/private-message? (test-util/flow-user-typing-message)))))
6 changes: 6 additions & 0 deletions src/test/clojure/jarvis/plugins/join_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns jarvis.plugins.join-test
(:use clojure.test)
(:require [jarvis.plugins.join :as join]
[jarvis.test-util :as test-util]))

; test stuff

0 comments on commit 841e775

Please sign in to comment.