I decided to build a chat bot with Elm to help me gain more experience with the language (I think it’s always good to practice with close-to-real projects). I chose a chat bot because I’ve built a few before in different languages, so I though it would be a fun experiment.
The API I got working for defining the “blueprint” of the bot looks like this:
root = Bot.tree "root" |> Bot.send "Hello, %name%, I hope you're doing fine." |> Bot.wait (1 * Time.second) |> Bot.send "Please choose an option" |> Bot.options [ ( "Option 1", option1Tree ) , ( "Option 2", option2Tree ) ]
This code works fine, and I’m fairly happy with how it looks. There are other features already, such as
goto to navigate back and forth in the bot tree.
The part I’m having trouble with is HTTP requests. At some point, usually all bots needs to use an API to search or notify the server about something the user typed. The request should be stored in the bot tree so it can be called when the user gets to that point in the conversation.
From what I understood, a request should have:
- URL (
- Some message (
Result Http.Error a -> Msg)
- Formatter (
Result Http.Error a -> BotMessage)
But I can’t seem to find a way to store a list of requests with different decoder types and different messages. The only type they share is the formatter output, that should always be a
The API I was hoping for defining requests would look something like this:
tree = Bot.tree |> Bot.ask "Tell me a github username" "username" |> Bot.request "https://api.github.com/users/%username%/repos" githubDecoder githubFormatter
But I couldn’t figure out the types to make it work.
Am I approaching this problem in the wrong way? I know this is an broad question, but any help is appreciated!