Packages
messenger_bot_store
0.0.7
0.1.0
0.1.0-beta9
retired
0.1.0-beta8
retired
0.1.0-beta7
retired
0.1.0-beta6
retired
0.1.0-beta5
retired
0.1.0-beta4
retired
0.1.0-beta3
retired
0.1.0-beta2
retired
0.1.0-beta1
retired
0.0.11
retired
0.0.10
retired
0.0.9
retired
0.0.8
retired
0.0.7
retired
0.0.6
retired
0.0.5
retired
0.0.4
retired
0.0.3
retired
0.0.2
retired
0.0.1
retired
MessengerBot event storage addon for PostgreSQL
Retired package: Deprecated
Current section
Files
Jump to
Current section
Files
lib/messenger_bot_store/handlers/echo.ex
defmodule MessengerBotStore.Handler.Echo do
@moduledoc """
Messenger Echo Handler
"""
use GenServer
require Logger
alias EventBus.Model.Event
def start_link,
do: GenServer.start_link(__MODULE__, nil, name: __MODULE__)
def init(_) do
{:ok, nil}
end
@doc """
Process a request
"""
def process({:mb_message_echo_received, _} = event_shadow) do
GenServer.cast(__MODULE__, event_shadow)
end
def process({topic, id}) do
EventBus.mark_as_skipped({__MODULE__, topic, id})
end
# Callbacks
def handle_cast({topic, id}, state) do
event = EventBus.fetch_event({topic, id})
process_entry(event.data)
EventBus.mark_as_completed({__MODULE__, topic, id})
{:noreply, state}
end
defp process_entry(%{app_id: app_id, messaging: messaging, page_id: page_id,
time: time}) do
time =
time
|> Ecto.DateTime.from_unix!(:millisecond)
|> Ecto.DateTime.to_erl()
|> NaiveDateTime.from_erl!()
process_messaging(app_id, page_id, time, messaging)
end
defp process_messaging(app_id, page_id, time, %{"recipient" => %{"id" => id},
"message" => %{"is_echo" => true, "mid" => mid}} = messaging) do
conversation = %{mid: mid, app_id: app_id, page_id: page_id, user_id: id,
payload_action: "echo", payload_content: messaging,
category: "message", received_at: time}
case MessengerBotStore.create_conversation(conversation) do
{:ok, conversation_with_id} ->
create_event(conversation_with_id)
res ->
Logger.info(fn -> inspect(res) end)
end
end
defp process_messaging(_, _, _, _),
do: nil
defp create_event(conversation) do
event = %Event{id: conversation.id, topic: :mbs_conversation_created,
data: conversation, occurred_at: :os.system_time(:milli_seconds)}
EventBus.notify(event)
end
end