summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lsg/application.ex11
-rw-r--r--lib/lsg/telegram.ex87
2 files changed, 51 insertions, 47 deletions
diff --git a/lib/lsg/application.ex b/lib/lsg/application.ex
index 3e5fb41..cdd7915 100644
--- a/lib/lsg/application.ex
+++ b/lib/lsg/application.ex
@@ -17,7 +17,6 @@ defmodule LSG.Application do
worker(LSG.Token, []),
worker(LSG.AuthToken, []),
{GenMagic.Pool, [name: LSG.GenMagic, pool_size: 2]},
- LSG.Telegram,
#worker(LSG.Icecast, []),
] ++ LSG.IRC.application_childs
@@ -25,6 +24,7 @@ defmodule LSG.Application do
# for other strategies and supported options
opts = [strategy: :one_for_one, name: LSG.Supervisor]
sup = Supervisor.start_link(children, opts)
+ start_telegram()
spawn_link(fn() -> LSG.IRC.after_start() end)
sup
end
@@ -36,4 +36,13 @@ defmodule LSG.Application do
:ok
end
+ defp start_telegram() do
+ token = Keyword.get(Application.get_env(:lsg, :telegram, []), :key)
+ options = [
+ username: Keyword.get(Application.get_env(:lsg, :telegram, []), :nick, "beauttebot"),
+ purge: false
+ ]
+ telegram = Telegram.Bot.ChatBot.Supervisor.start_link({LSG.Telegram, token, options})
+ end
+
end
diff --git a/lib/lsg/telegram.ex b/lib/lsg/telegram.ex
index 747f272..d393a2c 100644
--- a/lib/lsg/telegram.ex
+++ b/lib/lsg/telegram.ex
@@ -1,27 +1,34 @@
defmodule LSG.Telegram do
require Logger
-
- use Telegram.Bot,
- token: Keyword.get(Application.get_env(:lsg, :telegram, []), :key),
- username: Keyword.get(Application.get_env(:lsg, :telegram, []), :nick, "beauttebot"),
- purge: false
+ @behaviour Telegram.ChatBot
def my_path() do
"https://t.me/beauttebot"
end
- def init() do
- Logger.info("Telegram starting")
- # Create users in track: IRC.UserTrack.connected(...)
- :ok
+ def send_message(id, text, md2 \\ false) do
+ md = if md2, do: "MarkdownV2", else: "Markdown"
+ token = Keyword.get(Application.get_env(:lsg, :telegram, []), :key)
+ Telegram.Bot.ChatBot.Chat.Session.Supervisor.start_child(LSG.Telegram, id)
+ Telegram.Api.request(token, "sendMessage", chat_id: id, text: text, parse_mode: "Markdown")
+ end
+
+ @impl Telegram.ChatBot
+ def init(chat_id) do
+ Logger.info("Telegram session starting: #{chat_id}")
+ account = IRC.Account.find_meta_account("telegram-id", chat_id)
+ account_id = if account, do: account.id
+ {:ok, %{account: account_id}}
end
- def handle_update(_, %{"message" => m = %{"chat" => %{"type" => "private"}, "text" => text = "/start"<>_}}) do
- text = "Hello to beautte! Query the bot on IRC and say \"enable-telegram\" to continue."
+ @impl Telegram.ChatBot
+ def handle_update(%{"message" => m = %{"chat" => %{"type" => "private"}, "text" => text = "/start"<>_}}, _token, state) do
+ text = "*Welcome to beautte!*\n\nQuery the bot on IRC and say \"enable-telegram\" to continue."
send_message(m["chat"]["id"], text)
+ {:ok, %{account: nil}}
end
- def handle_update(_, %{"message" => m = %{"chat" => %{"type" => "private"}, "text" => text = "/enable"<>_}}) do
+ def handle_update(%{"message" => m = %{"chat" => %{"type" => "private"}, "text" => text = "/enable"<>_}}, _token, state) do
key = case String.split(text, " ") do
["/enable", key | _] -> key
_ -> "nil"
@@ -38,16 +45,19 @@ defmodule LSG.Telegram do
net = IRC.Account.get_meta(account, "telegram-validation-target")
IRC.Account.put_meta(account, "telegram-id", m["chat"]["id"])
IRC.Account.put_meta(account, "telegram-username", m["chat"]["username"])
+ IRC.Account.put_meta(account, "telegram-username", m["chat"]["username"])
IRC.Account.delete_meta(account, "telegram-validation-code")
IRC.Account.delete_meta(account, "telegram-validation-target")
IRC.Connection.broadcast_message(net, account, "Telegram #{m["chat"]["username"]} account added!")
- "Yay! Linked to account #{account.name}"
+ "Yay! Linked to account **#{account.name}**."
else
"Token invalid"
end
send_message(m["chat"]["id"], text)
+ {:ok, %{account: account.id}}
end
+
#[debug] Unhandled update: %{"message" =>
# %{"chat" => %{"first_name" => "J", "id" => 2075406, "type" => "private", "username" => "ahref"},
# "date" => 1591096015,
@@ -60,24 +70,10 @@ defmodule LSG.Telegram do
# "file_size" => 21420, "file_unique_id" => "AQADRv09JF0AA2RTAQAB", "height" => 148, "width" => 501}]},
# "update_id" => 218161546}
- def handle_update(token, data = %{"message" => %{"photo" => _}}) do
- start_upload(token, "photo", data)
- end
-
- def handle_update(token, data = %{"message" => %{"voice" => _}}) do
- start_upload(token, "voice", data)
- end
-
- def handle_update(token, data = %{"message" => %{"video" => _}}) do
- start_upload(token, "video", data)
- end
-
- def handle_update(token, data = %{"message" => %{"document" => _}}) do
- start_upload(token, "document", data)
- end
-
- def handle_update(token, data = %{"message" => %{"animation" => _}}) do
- start_upload(token, "animation", data)
+ for type <- ~w(photo voice video document animation) do
+ def handle_update(data = %{"message" => %{unquote(type) => _}}, token, state) do
+ start_upload(unquote(type), data, token, state)
+ end
end
#[debug] Unhandled update: %{"callback_query" =>
@@ -97,7 +93,7 @@ defmodule LSG.Telegram do
#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
+ def handle_update(%{"callback_query" => cb = %{"data" => "start-upload:"<>target, "id" => id, "message" => m = %{"message_id" => m_id, "chat" => %{"id" => chat_id}, "reply_to_message" => op}}}, t, state) do
account = IRC.Account.find_meta_account("telegram-id", chat_id)
if account do
target = case String.split(target, "/") do
@@ -134,7 +130,7 @@ defmodule LSG.Telegram do
bucket = Application.get_env(:lsg, :s3, []) |> Keyword.get(:bucket),
ext = Path.extname(file["file_path"]),
s3path = "#{account.id}/#{file_unique_id}#{ext}",
- Telegram.Api.request(t, "editMessageText", chat_id: chat_id, message_id: m_id, text: "Uploading...", reply_markup: %{}),
+ Telegram.Api.request(t, "editMessageText", chat_id: chat_id, message_id: m_id, text: "*Uploading...*", reply_markup: %{}, parse_mode: "MarkdownV2"),
s3req = ExAws.S3.put_object(bucket, s3path, body, acl: :public_read, content_type: magic.mime_type),
{:ok, _} <- ExAws.request(s3req)
do
@@ -148,30 +144,28 @@ defmodule LSG.Telegram do
end
if caption = op["caption"], do: as_irc_message(chat_id, caption, account)
text = "Sent on " <> Enum.join(sent, ", ") <> " !"
- Telegram.Api.request(t, "editMessageText", chat_id: chat_id, message_id: m_id, text: "Sent!", reply_markup: %{})
+ Telegram.Api.request(t, "editMessageText", chat_id: chat_id, message_id: m_id, text: "_Sent!_", reply_markup: %{}, parse_mode: "MarkdownV2")
else
error ->
- Telegram.Api.request(t, "editMessageText", chat_id: chat_id, message_id: m_id, text: "Something failed.", reply_markup: %{})
+ Telegram.Api.request(t, "editMessageText", chat_id: chat_id, message_id: m_id, text: "Something failed.", reply_markup: %{}, parse_mode: "MarkdownV2")
Logger.error("Failed upload from Telegram: #{inspect error}")
end
end)
end
+ {:ok, state}
end
- def handle_update(_, %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}, "text" => text}}) do
+ def handle_update(%{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}, "text" => text}}, _, state) do
account = IRC.Account.find_meta_account("telegram-id", id)
if account do
as_irc_message(id, text, account)
end
+ {:ok, state}
end
- def send_message(id, text) do
- token = Keyword.get(Application.get_env(:lsg, :telegram, []), :key)
- Telegram.Api.request(token, "sendMessage", chat_id: id, text: text)
- end
-
- def handle_update(token__, m) do
+ def handle_update(m, _, state) do
Logger.debug("Unhandled update: #{inspect m}")
+ {:ok, state}
end
defp as_irc_message(id, text, account) do
@@ -200,22 +194,23 @@ defmodule LSG.Telegram do
message
end
- defp start_upload(token, _, %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}}}) do
+ defp start_upload(_type, %{"message" => m = %{"chat" => %{"id" => id, "type" => "private"}}}, token, state) 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)
+ |> Enum.map(fn(i) -> %{"text" => i, "callback_data" => "start-upload:#{i}"} end)
kb = if Enum.count(targets) > 1 do
- [%{"text" => "everywhere", "callback_data" => "everywhere"}] ++ targets
+ [%{"text" => "everywhere", "callback_data" => "start-upload: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"])
+ 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"], parse_mode: "MarkdownV2")
end
+ {:ok, state}
end
end