alice_new v0.4.3 mix alice.new.handler View Source
Generates a new Alice handler.
This is the easiest way to set up a new Alice handler.
Install alice.new
mix archive.install hex alice_new
Build a Handler
First, navigate the command-line to the directory where you want to create
your new Alice handler. Then run the following commands: (change my_handler
to the name of your handler)
mix alice.new.handler my_handler
cd alice_my_handler
mix deps.get
Writing Route Handlers
In lib/alice/handlers/my_handler.ex:
defmodule Alice.Handlers.MyHandler do
use Alice.Router
command ~r/repeat after me: (?<term>.+)/i, :repeat
route ~r/repeat after me: (?<term>.+)/i, :repeat
@doc "`repeat after me: thing` - replies you said, 'thing'"
def repeat(conn) do
term = Alice.Conn.last_capture(conn)
response_text = "you said, '#{term}'"
reply(conn, response_text)
end
end
Testing Handlers
Alice provides several helpers to make it easy to test your handlers. First
you'll need to invoke to add use Alice.HandlerCase, handlers: [YourHandler] passing it the handler you're trying to test. Then you can use
message_received() within your test, which will simulate a message coming
in from the chat backend and route it through to the handlers appropriately.
If you're wanting to invoke a command, you'll need to make sure your message
includes <@alice> within the string. From there you can use either
first_reply() to get the first reply sent out or all_replies() which will
return a List of replies that have been received during your test. You can
use either to use normal assertions on to ensure your handler behaves in the
manner you expect.
In test/alice/handlers/my_handler_test.exs:
defmodule Alice.Handlers.MyHandlerTest do
use Alice.HandlerCase, handlers: Alice.Handlers.MyHandler
test "the repeat command repeats a term" do
send_message("<@alice> repeat after me: this is a boring handler")
assert first_reply() == "you said, 'this is a boring handler'"
end
test "the repeat route repeats a term" do
send_message("repeat after me: this is a boring handler")
assert first_reply() == "you said, 'this is a boring handler'"
end
end
Registering Handlers
In the mix.exs file of your bot, add your handler to the list of handlers
to register on start
def application do
[ applications: [:alice],
mod: {Alice, [Alice.Handlers.MyHandler] } ]
end
Link to this section Summary
Functions
Callback implementation for Mix.Task.run/1.
Link to this section Functions
Callback implementation for Mix.Task.run/1.