Created
August 6, 2014 15:27
-
-
Save nathanic/81278b67dce8d95a61d7 to your computer and use it in GitHub Desktop.
Number Trivia plugin for Bitsbot
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
(ns lazybot.plugins.number-trivia | |
(:use [lazybot registry] | |
[lazybot.utilities]) | |
(:require [clj-http.client :as client])) | |
; http://numbersapi.com/ | |
; from API docs: | |
;;; Just hit http://numbersapi.com/number/type to get a plain text response, where | |
;;; type is one of trivia, math, date, or year. Defaults to trivia if omitted. | |
;;; number is | |
;;; - an integer, or | |
;;; - the keyword random, for which we will try to return a random available fact, or | |
;;; - a day of year in the form month/day (eg. 2/29, 1/09, 04/1), if type is date | |
;;; - ranges of numbers | |
(def BASE-URL "http://numbersapi.com/") | |
(defn api-request [& path-pieces] | |
(-> (client/get (apply str BASE-URL path-pieces)) | |
:body)) | |
(defn get-fact [num type] | |
(println "got request for fact: " num type) | |
(try | |
(api-request num "/" type) | |
(catch Throwable t | |
(str "Dang, caught a Throwable! " t)))) | |
(defplugin | |
(:cmd | |
"Show an arguably fun fact about a number. Facthood is also arguable. Defaults to a random number. Unlock nerd mode with --nerd. `@number [--nerd] <num>`" | |
#{"number" "number-trivia"} | |
(fn [{:keys [nick args] :as com-m}] | |
(if (= "--nerd" (first args)) | |
(send-message com-m (get-fact (or (first (rest args)) "random") "math")) | |
(send-message com-m (get-fact (or (first args) "random") "trivia"))) )) | |
(:cmd | |
"Show a some kind of trivial 'fun fact' about a year. If unspecified, I'll pick a random year. @year <year>" | |
#{"year" "year-trivia"} | |
(fn [{:keys [nick args] :as com-m}] | |
(send-message com-m (get-fact (or (first args) "random") "year")))) | |
(:cmd | |
"Show a somewhat fun fact about a date. If unspecified, I'll pick a random date. @date <month>/<day>" | |
#{"date" "date-trivia"} | |
(fn [{:keys [nick args] :as com-m}] | |
(send-message com-m (get-fact (or (first args) "random") "date")))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment