diff options
Diffstat (limited to '')
-rw-r--r-- | lib/telegram/room.ex | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/lib/telegram/room.ex b/lib/telegram/room.ex index ede939e..7a8f427 100644 --- a/lib/telegram/room.ex +++ b/lib/telegram/room.ex @@ -32,7 +32,12 @@ defmodule Nola.TelegramRoom do end def after_start() do - for id <- room(:ids), do: Telegram.Bot.ChatBot.Chat.Session.Supervisor.start_child(Nola.Telegram, Integer.parse(id) |> elem(0)) + {:ok, rooms} = rooms(:ids) + for id <- rooms do + spawn(fn() -> + Telegram.Bot.ChatBot.Chat.Session.Supervisor.start_child(Nola.Telegram, Integer.parse(id) |> elem(0)) + end) + end end @impl Telegram.ChatBot @@ -40,16 +45,16 @@ defmodule Nola.TelegramRoom do token = Keyword.get(Application.get_env(:nola, :telegram, []), :key) {:ok, chat} = Api.request(token, "getChat", chat_id: id) Logger.metadata(transport: :telegram, id: id, telegram_room_id: id) - tg_room = case room(id) do + tg_room = case room(to_string(id)) do {:ok, tg_room = %{"network" => _net, "channel" => _chan}} -> tg_room - {:error, :not_found} -> + _ -> [net, chan] = String.split(chat["title"], "/", parts: 2) {net, chan} = case Nola.Irc.Connection.get_network(net, chan) do %Nola.Irc.Connection{} -> {net, chan} _ -> {nil, nil} end - {:ok, _id, _rev} = Couch.post(@couch, %{"_id" => id, "network" => net, "channel" => nil}) - {:ok, tg_room} = room(id) + _ = Couch.post(@couch, %{"_id" => id, "network" => net, "channel" => chan}) + {:ok, tg_room} = room(to_string(id)) tg_room end %{"network" => net, "channel" => chan} = tg_room @@ -68,7 +73,7 @@ defmodule Nola.TelegramRoom do def init(id) do Logger.error("telegram_room: bad id (not room id)", transport: :telegram, id: id, telegram_room_id: id) - :ignoree + :ignore end defp find_or_create_meta_account(from = %{"id" => user_id}, state) do @@ -86,7 +91,8 @@ defmodule Nola.TelegramRoom do account = username |> Nola.Account.new_account() |> Nola.Account.update_account_name(name) - |> Nola.Account.put_meta("telegram-id", user_id) + + Nola.Account.put_meta(account, "telegram-id", user_id) Logger.info("telegram_room: created account #{account.id} for telegram user #{user_id}") account @@ -95,14 +101,14 @@ defmodule Nola.TelegramRoom do def handle_update(%{"message" => %{"from" => from = %{"id" => user_id}, "text" => text}}, _token, state) do account = find_or_create_meta_account(from, state) - connection = Nola.Irc.Connection.get_network(state.net) + #connection = Nola.Irc.Connection.get_network(state.net) Nola.Irc.send_message_as(account, state.net, state.chan, text, true) {:ok, state} end def handle_update(data = %{"message" => %{"from" => from = %{"id" => user_id}, "location" => %{"latitude" => lat, "longitude" => lon}}}, _token, state) do account = find_or_create_meta_account(from, state) - connection = Nola.Irc.Connection.get_network(state.net) + #connection = Nola.Irc.Connection.get_network(state.net) Nola.Irc.send_message_as(account, state.net, state.chan, "@ #{lat}, #{lon}", true) {:ok, state} end @@ -172,7 +178,7 @@ defmodule Nola.TelegramRoom do do path = NolaWeb.Router.Helpers.url(NolaWeb.Endpoint) <> "/files/#{s3path}" txt = "#{type}: #{text}#{path}" - connection = Nola.Irc.Connection.get_network(state.net) + #connection = Nola.Irc.Connection.get_network(state.net) Nola.Irc.send_message_as(account, state.net, state.chan, txt, true) else error -> |