Skip to content

Commit

Permalink
Add optional <name> paramter to chuck
Browse files Browse the repository at this point in the history
  • Loading branch information
devth committed Oct 24, 2018
1 parent c1a179c commit 1e5f55a
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/yetibot/commands/chuck_norris.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
(ns yetibot.commands.chuck-norris
(:require
[yetibot.core.util.http :refer [html-decode]]
[clj-http.client :as client]
[clojure.string :as string]
[yetibot.core.hooks :refer [cmd-hook cmd-unhook]]
[yetibot.core.util.http :refer [get-json]]))
[yetibot.core.util.http :refer [html-decode]]))

(def endpoint "http://api.icndb.com/jokes/random?limitTo=[nerdy]")
(def uri "http://api.icndb.com/jokes/random")

(def qs-params {:limitTo "[nerdy]"})

(defn get-json
([] (get-json {}))
([extra-qs-params]
(client/get uri {:as :json
:query-params (merge qs-params extra-qs-params)})))

(defn chuck-joke
{:doc "chuck # tell a random Chuck Norris joke"
:yb/cat #{:fun}}
[_] (-> (get-json endpoint) :value :joke html-decode))
[_]
(-> (get-json) :body :value :joke html-decode))

(defn chuck-named-joke
"chuck <name> # tell a random joke using <name> instead of Chuck Norris"
[{match :match}]
(let [[first-name last-name] (string/split match #"\s+" 2)
joke (-> (get-json {:firstName first-name :lastName last-name})
:body :value :joke html-decode)]
(if (empty? last-name)
;; when last name is empty there will be weird spaces in the response;
;; remove them
(string/replace joke (str first-name " ") first-name)
joke)))

(cmd-hook ["chuck" #"^chuck(norris)*$"]
_ chuck-joke)
#".+" chuck-named-joke
_ chuck-joke)

0 comments on commit 1e5f55a

Please sign in to comment.