-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional <name> paramter to chuck
- Loading branch information
Showing
1 changed file
with
28 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |