Simple Elixir client for the cat facts API https://github.com/alexwohlbruck/cat-facts.
Written to quickly/imperfectly illustrate:
- how to call an API in Elixir, with retries
- parse the JSON into a structure
- build a Discord bot using Nostrum https://github.com/Kraigie/nostrum
The bot will respond to any message with text !fact
, and will post a random cat fact:
The API can be called like that:
iex(5)> CatFacts.ApiClient.get_random_fact()
23:15:27.080 [info] Received response with status code=200
{:ok,
%{
"__v" => 0,
"_id" => "63d7cad26e605bbb069f1445",
"createdAt" => "2023-01-30T13:49:06.481Z",
"deleted" => false,
"status" => %{"sentCount" => 0, "verified" => nil},
"text" => "Cats purr because their vocal cords in their throats vibrate. So the muscles of the larynx open and close the passage of air about 25 times per second.",
"type" => "cat",
"updatedAt" => "2023-01-30T13:49:06.481Z",
"user" => "63cd5150eec42e60ec32de30"
}}
If available in Hex, the package can be installed
by adding cat_facts
to your list of dependencies in mix.exs
:
def deps do
[
{:cat_facts, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/cat_facts.