summaryrefslogtreecommitdiff
path: root/lib/lsg_telegram/room.ex
diff options
context:
space:
mode:
authorJordan Bracco <href@random.sh>2022-12-11 17:19:11 +0000
committerJordan Bracco <href@random.sh>2022-12-11 17:19:11 +0000
commit4d7812a8b9d824deda745e42cb7aac8e64577013 (patch)
tree96fb97a251b7cb824c1a7c353aeaad308b179879 /lib/lsg_telegram/room.ex
parentfeat(base-plugin): list enabled plugins with `!plugins` (diff)
feat(telegram): create account for telegram user
Ref T53
Diffstat (limited to 'lib/lsg_telegram/room.ex')
-rw-r--r--lib/lsg_telegram/room.ex34
1 files changed, 28 insertions, 6 deletions
diff --git a/lib/lsg_telegram/room.ex b/lib/lsg_telegram/room.ex
index db3308e..da774c8 100644
--- a/lib/lsg_telegram/room.ex
+++ b/lib/lsg_telegram/room.ex
@@ -71,15 +71,37 @@ defmodule LSG.TelegramRoom do
:ignoree
end
- def handle_update(%{"message" => %{"from" => %{"id" => user_id}, "text" => text}}, _token, state) do
- account = IRC.Account.find_meta_account("telegram-id", user_id)
+ defp find_or_create_meta_account(from = %{"id" => user_id}, state) do
+ if account = IRC.Account.find_meta_account("telegram-id", user_id) do
+ account
+ else
+ first_name = Map.get(from, "first_name")
+ last_name = Map.get(from, "last_name")
+ name = [first_name, last_name]
+ |> Enum.filter(& &1)
+ |> Enum.join(" ")
+
+ username = Map.get(from, "username", first_name)
+
+ account = username
+ |> IRC.Account.new_account()
+ |> IRC.Account.update_account_name(name)
+ |> IRC.Account.put_meta("telegram-id", user_id)
+
+ Logger.info("telegram_room: created account #{account.id} for telegram user #{user_id}")
+ account
+ end
+ end
+
+ def handle_update(%{"message" => %{"from" => from = %{"id" => user_id}, "text" => text}}, _token, state) do
+ account = find_or_create_meta_account(from, state)
connection = IRC.Connection.get_network(state.net)
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)
+ 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 = IRC.Connection.get_network(state.net)
IRC.send_message_as(account, state.net, state.chan, "@ #{lat}, #{lon}", true)
{:ok, state}
@@ -113,8 +135,8 @@ 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)
+ defp upload(_type, %{"message" => m = %{"chat" => %{"id" => chat_id}, "from" => from = %{"id" => user_id}}}, token, state) do
+ account = find_or_create_meta_account(from, state)
if account do
{content, type} = cond do
m["photo"] -> {m["photo"], "photo"}