From 01385cfe9572df546685ef326db8ddc00d6fe371 Mon Sep 17 00:00:00 2001 From: href Date: Thu, 2 Sep 2021 02:52:15 +0200 Subject: new plugin: user mention --- lib/lsg/telegram.ex | 68 ++++++++++++++--------------------------------------- 1 file changed, 18 insertions(+), 50 deletions(-) (limited to 'lib/lsg/telegram.ex') diff --git a/lib/lsg/telegram.ex b/lib/lsg/telegram.ex index 02d7115..747f272 100644 --- a/lib/lsg/telegram.ex +++ b/lib/lsg/telegram.ex @@ -11,6 +11,7 @@ defmodule LSG.Telegram do end def init() do + Logger.info("Telegram starting") # Create users in track: IRC.UserTrack.connected(...) :ok end @@ -79,24 +80,6 @@ defmodule LSG.Telegram do start_upload(token, "animation", data) end - def start_upload(token, _, %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}}}) do - account = IRC.Account.find_meta_account("telegram-id", id) - if account do - text = if(m["text"], do: m["text"], else: nil) - targets = IRC.Membership.of_account(account) - |> Enum.map(fn({net, chan}) -> "#{net}/#{chan}" end) - |> Enum.map(fn(i) -> %{"text" => i, "callback_data" => i} end) - kb = if Enum.count(targets) > 1 do - [%{"text" => "everywhere", "callback_data" => "everywhere"}] ++ targets - else - targets - end - |> Enum.chunk_every(2) - keyboard = %{"inline_keyboard" => kb} - Telegram.Api.request(token, "sendMessage", chat_id: id, text: "Where should I send this file?", reply_markup: keyboard, reply_to_message_id: m["message_id"]) - end - end - #[debug] Unhandled update: %{"callback_query" => # %{ # "chat_instance" => "-7948978714441865930", "data" => "evolu.net/#dmz", @@ -111,8 +94,8 @@ defmodule LSG.Telegram do # } # , "update_id" => 218161568} - def handle_update(t, %{"callback_query" => cb = %{"data" => "resend", "id" => id, "message" => m = %{"message_id" => m_id, "chat" => %{"id" => chat_id}, "reply_to_message" => op}}}) do - end + #def handle_update(t, %{"callback_query" => cb = %{"data" => "resend", "id" => id, "message" => m = %{"message_id" => m_id, "chat" => %{"id" => chat_id}, "reply_to_message" => op}}}) do + #end def handle_update(t, %{"callback_query" => cb = %{"data" => target, "id" => id, "message" => m = %{"message_id" => m_id, "chat" => %{"id" => chat_id}, "reply_to_message" => op}}}) do account = IRC.Account.find_meta_account("telegram-id", chat_id) @@ -213,41 +196,26 @@ defmodule LSG.Telegram do trigger: IRC.Connection.extract_trigger(trigger_text), at: nil } - IO.puts("converted telegram to message: #{inspect message}") IRC.Connection.publish(message, ["message:private", "message:telegram"]) message end - command "start" do - text = "Hello to beautte! Query the bot on IRC and say \"enable-telegram\" to continue." - request("sendMessage", chat_id: update["chat"]["id"], - text: text) - end - - command "start", args do - IO.inspect(update) - key = "none" - account = IRC.Account.find_meta_account("telegram-validation-code", String.downcase(key)) - text = if account do - net = IRC.Account.get_meta(account, "telegram-validation-target") - IRC.Account.put_meta(account, "telegram-id", update["chat"]["id"]) - IRC.Account.delete_meta(account, "telegram-validation-code") - IRC.Account.delete_meta(account, "telegram-validation-target") - IRC.Connection.broadcast_message(net, account, "Telegram account added!") - "Yay! Linked to account #{account.name}\n\nThe bot doesn't work by sending telegram commands, just send what you would usually send on IRC." - else - "Token invalid" + defp start_upload(token, _, %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}}}) do + account = IRC.Account.find_meta_account("telegram-id", id) + if account do + text = if(m["text"], do: m["text"], else: nil) + targets = IRC.Membership.of_account(account) + |> Enum.map(fn({net, chan}) -> "#{net}/#{chan}" end) + |> Enum.map(fn(i) -> %{"text" => i, "callback_data" => i} end) + kb = if Enum.count(targets) > 1 do + [%{"text" => "everywhere", "callback_data" => "everywhere"}] ++ targets + else + targets + end + |> Enum.chunk_every(2) + keyboard = %{"inline_keyboard" => kb} + Telegram.Api.request(token, "sendMessage", chat_id: id, text: "Where should I send this file?", reply_markup: keyboard, reply_to_message_id: m["message_id"]) end - request("sendMessage", chat_id: update["chat"]["id"], text: text) end - command unknown do - request("sendMessage", chat_id: update["chat"]["id"], - text: "Hey! You sent me a command #{unknown} #{inspect update}") - end - - message do - request("sendMessage", chat_id: update["chat"]["id"], - text: "Hey! You sent me a message: #{inspect update}") - end end -- cgit v1.2.3