From c6508d899b0cb5327edd33ee5001f79bfb260274 Mon Sep 17 00:00:00 2001 From: Jonathan Chauncey Date: Tue, 16 Jul 2013 11:08:32 -0600 Subject: [PATCH] Plugins will now receive a second param which is a vector of the message content. --- PLUGINS.md | 4 ++-- README.md | 1 + project.clj | 3 ++- src/main/clojure/jarvis/command.clj | 9 +++++---- src/main/clojure/jarvis/plugins/help.clj | 2 +- src/main/clojure/jarvis/util.clj | 20 ++++++++++++++----- src/test/clojure/jarvis/plugins/help_test.clj | 4 ++-- src/test/clojure/jarvis/util_test.clj | 7 ++++++- 8 files changed, 34 insertions(+), 16 deletions(-) diff --git a/PLUGINS.md b/PLUGINS.md index e32d36f..ea1bd7a 100644 --- a/PLUGINS.md +++ b/PLUGINS.md @@ -17,11 +17,11 @@ Bot.addCommand("hello", "Simple Hello from Groovy", "Adam Esterline") ``` ## Message -> String -When jarvis executes a plugin it only passes in 1 parameter and that is the enchanced message received from the flow. +When jarvis executes a plugin it passes in 2 parameters and that is the enchanced message received from the flow and a vector of the message content (double quoted strings will be parsed as 1 item in the vecotr). All plugins must return a string that will be used as the content of the reply message. ```clojure -(defn my-awesome-plugin [message] +(defn my-awesome-plugin [message content-vector] "my awesome reply") ``` diff --git a/README.md b/README.md index aa528f0..af33523 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ See [INTRO](https://github.com/RallySoftware/jarvis-core/blob/master/INTRO.md) f [org.codehaus.groovy/groovy-all "2.1.0"] [org.clojure/java.classpath "0.2.0"] [org.clojure/tools.logging "0.2.6"] +[org.clojure/tools.reader "0.7.5"] [clj-http "0.7.2"] [fs "1.3.2"] [ch.qos.logback/logback-classic "1.0.9"] diff --git a/project.clj b/project.clj index c87eabd..aa4e6bb 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject com.rallydev/jarvis-core "1.3.0" +(defproject com.rallydev/jarvis-core "1.4.0" :description "A robot for FlowDock" :url "http://github.com/RallySoftware/jarvis-core" :license {:name "The MIT License (MIT)" @@ -8,6 +8,7 @@ [org.codehaus.groovy/groovy-all "2.1.0"] [org.clojure/java.classpath "0.2.0"] [org.clojure/tools.logging "0.2.6"] + [org.clojure/tools.reader "0.7.5"] [clj-http "0.7.2"] [fs "1.3.2"] [ch.qos.logback/logback-classic "1.0.9"]] diff --git a/src/main/clojure/jarvis/command.clj b/src/main/clojure/jarvis/command.clj index 5e885d9..478187f 100644 --- a/src/main/clojure/jarvis/command.clj +++ b/src/main/clojure/jarvis/command.clj @@ -1,24 +1,25 @@ (ns jarvis.command - (:require [clj-flowdock.api.message :as m] + (:require [jarvis.util :as util] + [clj-flowdock.api.message :as m] [clojure.string :as s] [clojure.tools.logging :as log])) (declare get-user-id-from-message get-user-ids-from-tags user->id) (defn tell [message plugin] - (when-let [reply (plugin message)] + (when-let [reply (plugin message (util/message-content->vec message))] (let [user-ids (get-user-ids-from-tags message)] (log/info "Plugin:" (m/content message) "replied:" (str "'" reply "'")) (m/send-private-messages user-ids reply)))) (defn private-message [message plugin] - (when-let [reply (plugin message)] + (when-let [reply (plugin message (util/message-content->vec message))] (let [user-id (get-user-id-from-message message)] (log/info "Plugin:" (m/content message) "replied to user:" user-id " with content:" (str "'" reply "'")) (m/send-private-message user-id reply)))) (defn reply [message plugin] - (when-let [reply (plugin message)] + (when-let [reply (plugin message (util/message-content->vec message))] (log/info "Plugin:" (m/content message) "replied:" (str "'" reply "'")) (m/reply message reply))) diff --git a/src/main/clojure/jarvis/plugins/help.clj b/src/main/clojure/jarvis/plugins/help.clj index 6d6e5ff..3ced5c1 100644 --- a/src/main/clojure/jarvis/plugins/help.clj +++ b/src/main/clojure/jarvis/plugins/help.clj @@ -13,7 +13,7 @@ sorted-plugin-meta (sort-by :command plugin-meta) help-text (map #(str "\t" (:command %) ":\t" (:description %)) sorted-plugin-meta)] (str - "\tAll known plugins\n" + "\n\tAll known plugins - \n" (s/join "\n" help-text) "\n\t~leave can be used to have me leave a flow. I will need to be invited back through the flowdock api to join again." "\n\t~ can be used also in private-messages with me." diff --git a/src/main/clojure/jarvis/util.clj b/src/main/clojure/jarvis/util.clj index db815b6..f53e62d 100644 --- a/src/main/clojure/jarvis/util.clj +++ b/src/main/clojure/jarvis/util.clj @@ -1,16 +1,19 @@ (ns jarvis.util - (:require [jarvis.plugins :as plugins] - [jarvis.flowdock.debug :as debug] - [clj-flowdock.api.message :as m] + (:require [jarvis.flowdock.debug :as debug] [clj-flowdock.api.flow :as f] + [clj-flowdock.api.message :as m] [clj-flowdock.api.user :as user] [clojure.string :as s] - [clojure.tools.logging :as log])) + [clojure.tools.logging :as log] + [clojure.tools.reader.edn :as edn])) (defn- env-variable [name] (-> (System/getenv) (get name))) +(defn- build-string [content] + (str "[" content "]")) + (defn config-property ([name] (config-property name nil)) @@ -31,4 +34,11 @@ (log/info (str "Closing flow connection - " (.flow-id flow-connection))) (m/chat (.flow-id flow-connection) "Very well sir, goodbye.") (f/block-user (.flow-id flow-connection) (get (user/me) "id")) - (.close flow-connection)) \ No newline at end of file + (.close flow-connection)) + +(defn message-content->vec [message] + (vec (map str (-> message + (get "content") + (subs 1) + build-string + (edn/read-string))))) \ No newline at end of file diff --git a/src/test/clojure/jarvis/plugins/help_test.clj b/src/test/clojure/jarvis/plugins/help_test.clj index d8b78c4..b3168f7 100644 --- a/src/test/clojure/jarvis/plugins/help_test.clj +++ b/src/test/clojure/jarvis/plugins/help_test.clj @@ -14,14 +14,14 @@ "happy") (deftest help - (is (= (str "\tAll known plugins\n" + (is (= (str "\n\tAll known plugins - \n" "\tbar:\tfoo" "\n\t~leave can be used to have me leave a flow. I will need to be invited back through the flowdock api to join again." "\n\t~ can be used also in private-messages with me." "\n\t~~ @ will private message the people listed the results of the executed command." "\n\tFor more details see https://github.com/RallySoftware/jarvis") (help/help (test-util/message "~help") [#'test-plugin]))) - (is (= (str "\tAll known plugins\n" + (is (= (str "\n\tAll known plugins - \n" "\tbar:\tfoo\n" "\tdawg:\tyo" "\n\t~leave can be used to have me leave a flow. I will need to be invited back through the flowdock api to join again." diff --git a/src/test/clojure/jarvis/util_test.clj b/src/test/clojure/jarvis/util_test.clj index ed87907..059be69 100644 --- a/src/test/clojure/jarvis/util_test.clj +++ b/src/test/clojure/jarvis/util_test.clj @@ -8,4 +8,9 @@ (is (= (System/getenv "PWD") (util/config-property "PWD"))) (System/setProperty "some-foo-property" "some-foo-value") (is (= "some-foo-value" (util/config-property "some-foo-property"))) - (is (= "some-default-value" (util/config-property "some-property" "some-default-value")))) \ No newline at end of file + (is (= "some-default-value" (util/config-property "some-property" "some-default-value")))) + +(deftest message-content->vec + (is (= ["foo"] (util/message-content->vec {"content" "~foo"}))) + (is (= ["foo" "bar"] (util/message-content->vec {"content" "~foo bar"}))) + (is (= ["foo" "bar" "1"] (util/message-content->vec {"content" "~foo bar 1"})))) \ No newline at end of file