From 149df627ee88569e07dbe8ee50ac63ccec8d5129 Mon Sep 17 00:00:00 2001 From: "Jose V. Trigueros" Date: Mon, 31 Oct 2016 21:36:33 -0600 Subject: [PATCH 1/9] Remove non-existent namespace from REPL init This causes a nasty error while loading up the REPL. I was a bit confused at first but then I realized this ns didn't exist. --- project.clj | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/project.clj b/project.clj index 42fcc13..40bf0f5 100644 --- a/project.clj +++ b/project.clj @@ -16,7 +16,6 @@ :uberjar-name "clj-slackbot.jar" :main clj-slackbot.core :profiles - {:dev {:repl-options {:init-ns clj-slackbot.core.handler} - :dependencies [[javax.servlet/servlet-api "2.5"] - [ring-mock "0.1.5"]]} + {:dev {:dependencies [[javax.servlet/servlet-api "2.5"] + [ring-mock "0.1.5"]]} :uberjar {:aot :all}}) From a07fda392328e93c64f71cbd730a965a2cdcfa64 Mon Sep 17 00:00:00 2001 From: "Jose V. Trigueros" Date: Mon, 31 Oct 2016 21:42:27 -0600 Subject: [PATCH 2/9] Add response-url and user to the meta key Both of these keys are used by post-to-slack and format-result-for-slack --- src/clj_slackbot/comms/slack_web_hook.clj | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/clj_slackbot/comms/slack_web_hook.clj b/src/clj_slackbot/comms/slack_web_hook.clj index b41ceb4..cc7176f 100644 --- a/src/clj_slackbot/comms/slack_web_hook.clj +++ b/src/clj_slackbot/comms/slack_web_hook.clj @@ -10,13 +10,13 @@ (defn post-to-slack ([post-url s channel] - (let [p (if channel {:channel channel} {})] - (client/post post-url - {:content-type :json - :form-params (assoc p :text s) - :query-params {"parse" "none"}}))) + (let [p (if channel {:channel channel} {})] + (client/post post-url + {:content-type :json + :form-params (assoc p :text s) + :query-params {"parse" "none"}}))) ([post-url s] - (post-to-slack post-url s nil))) + (post-to-slack post-url s nil))) (defn handle-clj [params command-token cin] (if-not (= (:token params) command-token) @@ -27,7 +27,9 @@ (str "#" (:channel_name params)))] ;; send the form to our evaluator and get out of here (>!! cin {:input (:text params) - :meta {:channel channel}}) + :meta {:channel channel + :response-url (:response_url params) + :user (:user_name params)}}) {:status 200 :body "..." :headers {"Content-Type" "text/plain"}}))) From f82df5c0acba5d64992bba5f7d30ccc6d5d80998 Mon Sep 17 00:00:00 2001 From: "Jose V. Trigueros" Date: Mon, 31 Oct 2016 21:49:31 -0600 Subject: [PATCH 3/9] Remove the requirement for post-url and obtain it from initial request As per the Slack API documentation, you don't provide a URL to respond to, instead you are given a URL that you should POST results back to. --- src/clj_slackbot/comms/slack_web_hook.clj | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/clj_slackbot/comms/slack_web_hook.clj b/src/clj_slackbot/comms/slack_web_hook.clj index cc7176f..c9e279a 100644 --- a/src/clj_slackbot/comms/slack_web_hook.clj +++ b/src/clj_slackbot/comms/slack_web_hook.clj @@ -34,10 +34,10 @@ {:status 200 :body "..." :headers {"Content-Type" "text/plain"}}))) -(defn start [{:keys [port post-url command-token] :as config}] +(defn start [{:keys [port command-token]}] ;; check we have everything - (when (some nil? [port post-url command-token]) - (throw (Exception. "Cannot initialize. Missing port, post-url or command-token"))) + (when (some nil? [port command-token]) + (throw (Exception. "Cannot initialize. Missing port or command-token"))) (println ":: starting http server on port:" port) (let [cin (async/chan 10) @@ -53,7 +53,8 @@ (if-not res (println "The form output channel has been closed. Leaving listen loop.") (let [result (:evaluator/result res) - channel (get-in res [:meta :channel])] + channel (get-in res [:meta :channel]) + post-url (get-in res [:meta :response-url])] (post-to-slack post-url (util/format-result-for-slack result) From 62de31cba65e0d35392660b1f181a55b7452c455 Mon Sep 17 00:00:00 2001 From: "Jose V. Trigueros" Date: Mon, 31 Oct 2016 21:52:34 -0600 Subject: [PATCH 4/9] Use format-result-for-slack with correct map structure `format-result-for-slack` expects a certain kind of map. The code has access to this map already, but a different one is sent instead. --- src/clj_slackbot/comms/slack_web_hook.clj | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/clj_slackbot/comms/slack_web_hook.clj b/src/clj_slackbot/comms/slack_web_hook.clj index c9e279a..1e9a34f 100644 --- a/src/clj_slackbot/comms/slack_web_hook.clj +++ b/src/clj_slackbot/comms/slack_web_hook.clj @@ -52,12 +52,11 @@ (go-loop [res ( Date: Mon, 31 Oct 2016 21:53:59 -0600 Subject: [PATCH 5/9] Update sample edn file and README to reflect config requirements `:post-url` shouldn't be mentioned as it is no longer needed --- README.md | 1 - config.example.edn | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index fa2d18a..4a32ed6 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ This bot now supports different kinds of comm-links (communication channels to t Webhook configuration needs four things defined in `config.edn`: - `:comm :clj-slackbot.comms.slack-web-hook/start` -- `:post-url` - The post URL to post responses to a channel. - `:command-token` - The token you get when you create a slash command in slack. Usually something like `/clj`. - `:port` - The port to run the web-server on. diff --git a/config.example.edn b/config.example.edn index 80a9f1d..434a20a 100644 --- a/config.example.edn +++ b/config.example.edn @@ -2,5 +2,4 @@ :api-token "XXX" :prefix "," :port 3000 - :command-token "XXX" - :post-url "XXX"} + :command-token "XXX"} From d6a07411f7ed3a93c37b217cb66193d6aeb5fe1b Mon Sep 17 00:00:00 2001 From: "Jose V. Trigueros" Date: Tue, 12 Mar 2019 20:57:43 -0700 Subject: [PATCH 6/9] Destructure channel and post-url directly in go-loop bindings --- src/clj_slackbot/comms/slack_web_hook.clj | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/clj_slackbot/comms/slack_web_hook.clj b/src/clj_slackbot/comms/slack_web_hook.clj index 1e9a34f..880006a 100644 --- a/src/clj_slackbot/comms/slack_web_hook.clj +++ b/src/clj_slackbot/comms/slack_web_hook.clj @@ -49,11 +49,10 @@ (route/not-found "Not Found")) (wrap-defaults api-defaults))] ;; start the loops we need to read back eval responses - (go-loop [res ( Date: Tue, 12 Mar 2019 21:03:05 -0700 Subject: [PATCH 7/9] Create form-params in one expression instead of using let binding --- src/clj_slackbot/comms/slack_web_hook.clj | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clj_slackbot/comms/slack_web_hook.clj b/src/clj_slackbot/comms/slack_web_hook.clj index 880006a..66f8b13 100644 --- a/src/clj_slackbot/comms/slack_web_hook.clj +++ b/src/clj_slackbot/comms/slack_web_hook.clj @@ -10,11 +10,11 @@ (defn post-to-slack ([post-url s channel] - (let [p (if channel {:channel channel} {})] - (client/post post-url - {:content-type :json - :form-params (assoc p :text s) - :query-params {"parse" "none"}}))) + (client/post post-url + {:content-type :json + :form-params (cond-> {:text s} + channel (assoc :channel channel)) + :query-params {"parse" "none"}})) ([post-url s] (post-to-slack post-url s nil))) From ef6e71b965aa921a826c355bd7e9318f38439182 Mon Sep 17 00:00:00 2001 From: "Jose V. Trigueros" Date: Tue, 12 Mar 2019 21:06:04 -0700 Subject: [PATCH 8/9] (condp = ...) is the same as (case ...) --- src/clj_slackbot/comms/slack_web_hook.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/clj_slackbot/comms/slack_web_hook.clj b/src/clj_slackbot/comms/slack_web_hook.clj index 66f8b13..884f25f 100644 --- a/src/clj_slackbot/comms/slack_web_hook.clj +++ b/src/clj_slackbot/comms/slack_web_hook.clj @@ -21,7 +21,7 @@ (defn handle-clj [params command-token cin] (if-not (= (:token params) command-token) {:status 403 :body "Unauthorized"} - (let [channel (condp = (:channel_name params) + (let [channel (case (:channel_name params) "directmessage" (str "@" (:user_name params)) "privategroup" (:channel_id params) (str "#" (:channel_name params)))] From fb0e9cb986eef6df1fd6d6d1e5a9ca77f2b40ffb Mon Sep 17 00:00:00 2001 From: "Jose V. Trigueros" Date: Tue, 12 Mar 2019 21:09:54 -0700 Subject: [PATCH 9/9] Destructure params argument in handle-clj --- src/clj_slackbot/comms/slack_web_hook.clj | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/clj_slackbot/comms/slack_web_hook.clj b/src/clj_slackbot/comms/slack_web_hook.clj index 884f25f..de734ab 100644 --- a/src/clj_slackbot/comms/slack_web_hook.clj +++ b/src/clj_slackbot/comms/slack_web_hook.clj @@ -18,23 +18,25 @@ ([post-url s] (post-to-slack post-url s nil))) -(defn handle-clj [params command-token cin] - (if-not (= (:token params) command-token) +(defn handle-clj + [{:keys [token channel_id channel_name response_url text user_name]} command-token cin] + (if-not (= token command-token) {:status 403 :body "Unauthorized"} - (let [channel (case (:channel_name params) - "directmessage" (str "@" (:user_name params)) - "privategroup" (:channel_id params) - (str "#" (:channel_name params)))] + (let [channel (case channel_name + "directmessage" (str "@" user_name) + "privategroup" channel_id + (str "#" channel_name))] ;; send the form to our evaluator and get out of here - (>!! cin {:input (:text params) + (>!! cin {:input text :meta {:channel channel - :response-url (:response_url params) - :user (:user_name params)}}) + :response-url response_url + :user user_name}}) {:status 200 :body "..." :headers {"Content-Type" "text/plain"}}))) -(defn start [{:keys [port command-token]}] +(defn start + [{:keys [port command-token]}] ;; check we have everything (when (some nil? [port command-token]) (throw (Exception. "Cannot initialize. Missing port or command-token")))