summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lsg/application.ex11
-rw-r--r--lib/lsg/telegram.ex87
-rw-r--r--mix.lock3
3 files changed, 53 insertions, 48 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
diff --git a/mix.lock b/mix.lock
index 8adc6e5..9d572c1 100644
--- a/mix.lock
+++ b/mix.lock
@@ -54,9 +54,10 @@
"plug_crypto": {:hex, :plug_crypto, "1.2.2", "05654514ac717ff3a1843204b424477d9e60c143406aa94daf2274fdd280794d", [:mix], [], "hexpm", "87631c7ad914a5a445f0a3809f99b079113ae4ed4b867348dd9eec288cecb6db"},
"poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm", "ba8836feea4b394bb718a161fc59a288fe0109b5006d6bdf97b6badfcf6f0f25"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
+ "retry": {:hex, :retry, "0.14.1", "722d1b0cf87096b71213f5801d99fface7ca76adc83fc9dbf3e1daee952aef10", [:mix], [], "hexpm", "b3a609f286f6fe4f6b2c15f32cd4a8a60427d78d05d7b68c2dd9110981111ae0"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"ssl_verify_hostname": {:hex, :ssl_verify_hostname, "1.0.6", "45866d958d9ae51cfe8fef0050ab8054d25cba23ace43b88046092aa2c714645", [:make], [], "hexpm", "72b2fc8a8e23d77eed4441137fefa491bbf4a6dc52e9c0045f3f8e92e66243b5"},
- "telegram": {:git, "https://github.com/hrefhref/telegram.git", "04841028b953f37a036cb18b73bd8ad8e934fe26", [branch: "master"]},
+ "telegram": {:git, "https://github.com/hrefhref/telegram.git", "d7d4e1199ed0222cad73bf3eebd74a1925544c0a", [branch: "master"]},
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
"tesla": {:hex, :tesla, "1.4.3", "f5a494e08fb1abe4fd9c28abb17f3d9b62b8f6fc492860baa91efb1aab61c8a0", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.3", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "e0755bb664bf4d664af72931f320c97adbf89da4586670f4864bf259b5750386"},
"timex": {:hex, :timex, "3.7.6", "502d2347ec550e77fdf419bc12d15bdccd31266bb7d925b30bf478268098282f", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "a296327f79cb1ec795b896698c56e662ed7210cc9eb31f0ab365eb3a62e2c589"},