Skip to content

Instantly share code, notes, and snippets.

@zabirauf
Created June 15, 2016 05:12
Show Gist options
  • Save zabirauf/e5a7ae3378c8cfa2a71383ea56a200d9 to your computer and use it in GitHub Desktop.
Save zabirauf/e5a7ae3378c8cfa2a71383ea56a200d9 to your computer and use it in GitHub Desktop.
Weather conversation Wit.ai actions for the story created to get weather information. Complete project at https://github.com/zabirauf/witai_elixir_weather_bot
defmodule EchoBot.WeatherConversationAction do
use Wit.Actions
alias Wit.Models.Response.Converse, as: WitConverse
alias ExMicrosoftBot.Client
def say(_session_id, %{} = context, %WitConverse{msg: msg_to_send} = message) do
%{"session" => %{"from" => to, "to" => from, "msgId" => msgId}} = context
message_to_send = %{from: from, to: to, replyToMessageId: msgId, text: msg_to_send}
Client.send_message(get_bot_auth_data(), message_to_send)
end
def merge(_session_id, %{} = context, %WitConverse{} = message) do
%WitConverse{entities: %{"location" => [%{"value" => location}|_]}} = message
Map.put(context, "loc", location)
end
def error(_session, %{} = _context, error) do
Logger.error "Error recieved #{inspect(error)}"
end
@doc """
Fetches the weather
"""
defaction fetch_weather(_session_id, %{} = context) do
location = Map.get(context, "loc")
case EchoBot.WeatherClient.get_weather(location) do
# Get the weather for the lcoation in the context
{:ok, %{"main" => %{"temp" => temp}}} ->
# Returning back the updated context
Map.put(context, "temperature", kelvin_to_fahrenheit(temp) |> round)
_ ->
context
end
end
@doc """
Called when the story has ended
"""
defaction story_ended(_session_id, %{} = context) do
Logger.info "Story ended"
context
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment