yuyabee / bot_framework

Microsoft Bot Framework Client for Elixir

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BotFramework

Microsoft Bot Framework (V3) Client in Elixir.

Installation

  1. Add bot_framework to your list of dependencies in mix.exs:
def deps do
  [{:bot_framework, "~> 0.2.0"}]
end
  1. Ensure bot_framework is started before your application:
def application do
  [applications: [:bot_framework]]
end
  1. Configure API key:
config :bot_framework,
  client_id: "CLIENT_ID",
  client_secret: "CLIENT_SECRET"
  1. Define route:
defmodule BotTest.Router do
  use BotFramework.Phoenix.Router

  # ... other stuff ...

  scope "/api", BotTest do
    bf_handler "/handle_message", MessageController
  end
end
  1. 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

About

Microsoft Bot Framework Client for Elixir


Languages

Language:Elixir 100.0%