Packages
This library allows for easy creation of the web API that the Microsoft bot framework can connect to.
Current section
Files
Jump to
Current section
Files
lib/microsoftbot_router.ex
defmodule MicrosoftBot.Router do
@moduledoc """
This module defines the routes needed to handle messages from the Microsoft bot service
"""
defmacro __using__(_) do
quote do
import unquote(__MODULE__)
end
end
@doc """
Responsible for defining routes necessary to communicate with the Microsoft bot service
The route is
- post /api/message for getting messages
It takes the controller that uses `MicrosoftBot.Phoenix.Controller`
## Examples
Defining the controller
```
defmodule MessageController do
use MicrosoftBot.Phoenix.Controller
def message_received(%MicrosoftBot.Models.Message{} = message) do
# ...
# send message back or resp(conn, 200, "")
end
end
```
In the `routes.ex`
```
defmodule YourApp.Router do
use YourApp.Web, :router
# Add the following two lines
use MicrosoftBot.Router
microsoftbot_routes "/api/message", MessageController
end
```
## Parameters:
- `path` the path you want use for receiving messages
- `controller` the controller you want to use to handle the received messages
"""
@spec microsoftbot_routes(String.t, module) :: any
defmacro microsoftbot_routes(path, controller) do
quote do
pipeline :microsoftbot do
plug :accepts, ["json"]
plug :bot_authenticate
end
scope "/" do
pipe_through :microsoftbot
post unquote(path), unquote(controller), :handle_message
end
defp bot_authenticate(conn, _) do
case ExMicrosoftBot.Client.validate_bot_credentials(get_bot_auth_data, conn.req_headers) do
true -> conn
false -> resp(conn, 403, "") |> halt
end
end
defp get_bot_auth_data() do
%ExMicrosoftBot.Models.AuthData{
app_id: Application.get_env(:microsoftbot, :app_id),
app_secret: Application.get_env(:microsoftbot, :app_secret)
}
end
end
end
end