Microsoft Bot Framework (V3) Client in Elixir.
- Add
bot_framework
to your list of dependencies inmix.exs
:
def deps do
[{:bot_framework, "~> 0.2.0"}]
end
- Ensure
bot_framework
is started before your application:
def application do
[applications: [:bot_framework]]
end
- Configure API key:
config :bot_framework,
client_id: "CLIENT_ID",
client_secret: "CLIENT_SECRET"
Usage with Phoenix framework
- Define route:
defmodule BotTest.Router do
use BotFramework.Phoenix.Router
# ... other stuff ...
scope "/api", BotTest do
bf_handler "/handle_message", MessageController
end
end
- Write handler(See BotFramework.Callback for more information)
defmodule BotTest.MessageController do
use BotTest.Web, :controller
use BotFramework.Phoenix.Controller
alias BotFramework.Models.{Activity}
# handle text message
def handle_activity(%Activity{type: "message", text: text}) when is_bitstring(text) do
case text |> String.downcase do
"hi" -> [text: "Hi this is a bot"]
"how are you?" -> [text: "Good. Thanks. And you?"]
"hero" ->
[text: "Hero sample", attachments: [%{
contentType: "application/vnd.microsoft.card.hero",
content: %{
title: "I'm a hero card",
subtitle: "Search Google",
images: [
%{ url: "https://www.gstatic.com/webp/gallery3/1_webp_a.png" },
],
buttons: [%{
type: "openUrl",
title: "Google",
value: "https://www.google.com"
}]
}}]]
"bad" -> [text: "Okay"]
"fine" -> [text: "Okay"]
_ -> [text: "Pardon me?"]
end
end
# handle attachments
def handle_activity(%Activity{type: "message", text: nil, attachments: attachments}) do
# do something with the attachments
[text: "Received attachment"]
end
end