diff options
author | href <href@random.sh> | 2021-09-02 13:00:55 +0200 |
---|---|---|
committer | href <href@random.sh> | 2021-09-02 13:00:55 +0200 |
commit | 28ab854e9dbcd2e40fc4b7986f4e5dd303bf27a1 (patch) | |
tree | 62bda7f25e7d9282283fc8eca290f60d7d0d8c86 /lib/lsg | |
parent | lastfm: improvements, fixes (diff) |
improve puppets, fix uploads
Diffstat (limited to 'lib/lsg')
-rw-r--r-- | lib/lsg/telegram.ex | 7 | ||||
-rw-r--r-- | lib/lsg/telegram_room.ex | 65 |
2 files changed, 66 insertions, 6 deletions
diff --git a/lib/lsg/telegram.ex b/lib/lsg/telegram.ex index e5790da..e8758e3 100644 --- a/lib/lsg/telegram.ex +++ b/lib/lsg/telegram.ex @@ -66,7 +66,6 @@ defmodule LSG.Telegram do {:ok, %{account: account.id}} end - #[debug] Unhandled update: %{"message" => # %{"chat" => %{"first_name" => "J", "id" => 2075406, "type" => "private", "username" => "ahref"}, # "date" => 1591096015, @@ -145,10 +144,8 @@ defmodule LSG.Telegram do do path = LSGWeb.Router.Helpers.url(LSGWeb.Endpoint) <> "/files/#{s3path}" sent = for {net, chan} <- target do - user = IRC.UserTrack.find_by_account(net, account) - nick = if(user, do: user.nick, else: account.name) - txt = "#{nick} sent#{type}#{text} #{path}" - IRC.Connection.broadcast_message(net, chan, txt) + txt = "sent#{type}#{text} #{path}" + IRC.send_message_as(account, net, chan, txt) "#{net}/#{chan}" end if caption = op["caption"], do: as_irc_message(chat_id, caption, account) diff --git a/lib/lsg/telegram_room.ex b/lib/lsg/telegram_room.ex index f973c58..9504cd4 100644 --- a/lib/lsg/telegram_room.ex +++ b/lib/lsg/telegram_room.ex @@ -23,10 +23,23 @@ defmodule LSG.TelegramRoom do def handle_update(%{"message" => %{"from" => %{"id" => user_id}, "text" => text}}, _token, state) do account = IRC.Account.find_meta_account("telegram-id", user_id) connection = IRC.Connection.get_network(state.net) - IRC.PuppetConnection.send_message(account, connection, state.chan, text) + IRC.send_message_as(account, state.net, state.chan, text, true) {:ok, state} end + def handle_update(data = %{"message" => %{"from" => %{"id" => user_id}, "location" => %{"latitude" => lat, "longitude" => lon}}}, _token, state) do + account = IRC.Account.find_meta_account("telegram-id", user_id) + connection = IRC.Connection.get_network(state.net) + IRC.send_message_as(account, state.net, state.chan, "@ #{lat}, #{lon}", true) + {:ok, state} + end + + for type <- ~w(photo voice video document animation) do + def handle_update(data = %{"message" => %{unquote(type) => _}}, token, state) do + upload(unquote(type), data, token, state) + end + end + def handle_update(update, token, state) do {:ok, state} end @@ -50,4 +63,54 @@ defmodule LSG.TelegramRoom do {:ok, state} end + defp upload(_type, %{"message" => m = %{"chat" => %{"id" => chat_id}, "from" => %{"id" => user_id}}}, token, state) do + account = IRC.Account.find_meta_account("telegram-id", user_id) + if account do + {content, type} = cond do + m["photo"] -> {m["photo"], "photo"} + m["voice"] -> {m["voice"], "voice message"} + m["video"] -> {m["video"], "video"} + m["document"] -> {m["document"], "file"} + m["animation"] -> {m["animation"], "gif"} + end + + file = if is_list(content) && Enum.count(content) > 1 do + Enum.sort_by(content, fn(p) -> p["file_size"] end, &>=/2) + |> List.first() + else + content + end + + file_id = file["file_id"] + file_unique_id = file["file_unique_id"] + text = if(m["caption"], do: m["caption"] <> " ", else: "") + + spawn(fn() -> + with \ + {:ok, file} <- Telegram.Api.request(token, "getFile", file_id: file_id), + path = "https://api.telegram.org/file/bot#{token}/#{file["file_path"]}", + {:ok, %HTTPoison.Response{status_code: 200, body: body}} <- HTTPoison.get(path), + <<smol_body::binary-size(20), _::binary>> = body, + {:ok, magic} <- GenMagic.Pool.perform(LSG.GenMagic, {:bytes, smol_body}), + bucket = Application.get_env(:lsg, :s3, []) |> Keyword.get(:bucket), + ext = Path.extname(file["file_path"]), + s3path = "#{account.id}/#{file_unique_id}#{ext}", + s3req = ExAws.S3.put_object(bucket, s3path, body, acl: :public_read, content_type: magic.mime_type), + {:ok, _} <- ExAws.request(s3req) + do + path = LSGWeb.Router.Helpers.url(LSGWeb.Endpoint) <> "/files/#{s3path}" + txt = "#{type}: #{text}#{path}" + connection = IRC.Connection.get_network(state.net) + IRC.send_message_as(account, state.net, state.chan, txt, true) + else + error -> + Telegram.Api.request(token, "sendMessage", chat_id: chat_id, text: "File upload failed, sorry.") + Logger.error("Failed upload from Telegram: #{inspect error}") + end + end) + + {:ok, state} + end + end + end |