Change from Telegram to Matrix for messaging
This commit is contained in:
parent
567248ea1f
commit
e7706744a9
6 changed files with 72 additions and 39 deletions
ui/lib
39
ui/lib/ui/message_bot.ex
Normal file
39
ui/lib/ui/message_bot.ex
Normal file
|
@ -0,0 +1,39 @@
|
|||
defmodule Ui.MessageBot do
|
||||
def message(msg) do
|
||||
{:ok, _result} = Tesla.put(client(), "/_matrix/client/r0/rooms/" <> room() <> "/send/m.room.message/" <> transaction_id(), construct_message(msg))
|
||||
end
|
||||
|
||||
def client do
|
||||
middleware = [
|
||||
{Tesla.Middleware.BaseUrl, base_url()},
|
||||
Tesla.Middleware.JSON,
|
||||
{Tesla.Middleware.Headers, [{"Authorization", "Bearer " <> token()}]}
|
||||
]
|
||||
|
||||
Tesla.client(middleware)
|
||||
end
|
||||
|
||||
def token do
|
||||
Application.fetch_env!(:ui, :matrix_token)
|
||||
end
|
||||
|
||||
def base_url do
|
||||
Application.fetch_env!(:ui, :matrix_url)
|
||||
end
|
||||
|
||||
def room do
|
||||
Application.fetch_env!(:ui, :matrix_room)
|
||||
end
|
||||
|
||||
def transaction_id do
|
||||
id = DateTime.utc_now()
|
||||
|> DateTime.to_unix()
|
||||
|> Integer.to_string()
|
||||
|
||||
"esrom-" <> id
|
||||
end
|
||||
|
||||
def construct_message(msg) do
|
||||
%{msgtype: "m.text", body: msg}
|
||||
end
|
||||
end
|
|
@ -1,13 +0,0 @@
|
|||
defmodule Ui.TelegramBot do
|
||||
def message(msg) do
|
||||
Telegram.Api.request(token(), "sendMessage", chat_id: chat_id(), text: msg)
|
||||
end
|
||||
|
||||
defp chat_id do
|
||||
Application.fetch_env!(:ui, :chat_id)
|
||||
end
|
||||
|
||||
defp token do
|
||||
Application.fetch_env!(:ui, :token)
|
||||
end
|
||||
end
|
|
@ -17,11 +17,12 @@ defmodule UiWeb.MorseLive do
|
|||
Logger.info("#{ip} pressed the button!")
|
||||
|
||||
if not Morse.Server.in_progress?() and ip_send_message?(ip) do
|
||||
Logger.info("Sending Telegram message.")
|
||||
spawn(fn -> Ui.TelegramBot.message("#{ip} pressed the button!") end)
|
||||
Logger.info("Sending message.")
|
||||
spawn(fn -> Ui.MessageBot.message("#{ip} pressed the button!") end)
|
||||
end
|
||||
|
||||
Morse.Server.toggle_morse()
|
||||
spawn(fn -> Ui.MessageBot.message("#{ip} pressed the button!") end)
|
||||
#Morse.Server.toggle_morse()
|
||||
|
||||
{:noreply, socket}
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue