Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cljquotes command for spouting random quotes about Clojure #928

Merged
merged 2 commits into from Apr 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/yetibot/commands/cljquotes.clj
@@ -0,0 +1,44 @@
(ns yetibot.commands.cljquotes
(:require
[clojure.core.memoize :as memo]
[clojure.edn :as edn]
[clojure.string :as string]

[clj-http.client :as client]

[yetibot.core.hooks :refer [cmd-hook]]
))

(defn fetch-quotes
[]
(let [url "https://raw.githubusercontent.com/Azel4231/clojure-quotes/master/quotes.edn"]
(-> (client/get url)
:body
edn/read-string)))

;; only fetch once per day
(def quote-fetch-delay (* 24 60 60 1000))

(def quotes
(memo/ttl fetch-quotes :ttl/threshold quote-fetch-delay))

(defn format-quote
[quote]
(let [{:clojure-quotes.core/keys [text quotee reference]} quote
{:clojure-quotes.core/keys [url time]} reference]
(str ">>> " text "\n"
"-- " quotee
(when url (str " (" url (when time (str " @" time)) ")")))))

(defn quote-cmd
"cljquote # random clojure quote"
[context]
(let [quote (rand-nth (quotes))]
{:result/value (format-quote quote)
:result/data quote}))

(cmd-hook #"cljquote"
_ quote-cmd)

#_(format-quote (rand-nth (quotes)))
#_(string/join "\n\n" (map format-quote (quotes)))