Current section
Files
Jump to
Current section
Files
src/telega.erl
-module(telega).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([is_webhook_path/2, is_secret_token_valid/2, new/4, handle_all/2, wait_any/2, handle_command/3, wait_command/3, handle_commands/3, wait_commands/3, handle_text/2, wait_text/2, handle_hears/3, wait_hears/3, handle_callback_query/3, wait_callback_query/3, log_context/3, with_session_settings/3, init/1, init_nil_session/1, handle_update/2]).
-export_type([telega/1, telega_builder/1]).
-opaque telega(URL) :: {telega,
telega@internal@config:config(),
list(telega@bot:handler(URL)),
gleam@option:option(telega@bot:session_settings(URL)),
gleam@option:option(gleam@erlang@process:subject(telega@bot:registry_message()))}.
-opaque telega_builder(URM) :: {telega_builder, telega(URM)}.
-spec is_webhook_path(telega(any()), binary()) -> boolean().
is_webhook_path(Telega, Path) ->
erlang:element(3, erlang:element(2, Telega)) =:= Path.
-spec is_secret_token_valid(telega(any()), binary()) -> boolean().
is_secret_token_valid(Telega, Token) ->
erlang:element(4, erlang:element(2, Telega)) =:= Token.
-spec new(binary(), binary(), binary(), gleam@option:option(binary())) -> telega_builder(any()).
new(Token, Server_url, Webhook_path, Secret_token) ->
{telega_builder,
{telega,
telega@internal@config:new(
Token,
Server_url,
Webhook_path,
Secret_token
),
[],
none,
none}}.
-spec handle_all(
telega_builder(URU),
fun((telega@bot:context(URU)) -> {ok, URU} | {error, binary()})
) -> telega_builder(URU).
handle_all(Builder, Handler) ->
{telega_builder,
erlang:setelement(
3,
erlang:element(2, Builder),
[{handle_all, Handler} |
erlang:element(3, erlang:element(2, Builder))]
)}.
-spec wait_any(
telega@bot:context(USA),
fun((telega@bot:context(USA)) -> {ok, USA} | {error, binary()})
) -> {ok, USA} | {error, binary()}.
wait_any(Ctx, Continue) ->
telega@bot:wait_handler(Ctx, {handle_all, Continue}).
-spec handle_command(
telega_builder(USH),
binary(),
fun((telega@bot:context(USH), telega@update:command()) -> {ok, USH} |
{error, binary()})
) -> telega_builder(USH).
handle_command(Builder, Command, Handler) ->
{telega_builder,
erlang:setelement(
3,
erlang:element(2, Builder),
[{handle_command, Command, Handler} |
erlang:element(3, erlang:element(2, Builder))]
)}.
-spec wait_command(
telega@bot:context(USN),
binary(),
fun((telega@bot:context(USN), telega@update:command()) -> {ok, USN} |
{error, binary()})
) -> {ok, USN} | {error, binary()}.
wait_command(Ctx, Command, Continue) ->
telega@bot:wait_handler(Ctx, {handle_command, Command, Continue}).
-spec handle_commands(
telega_builder(USU),
list(binary()),
fun((telega@bot:context(USU), telega@update:command()) -> {ok, USU} |
{error, binary()})
) -> telega_builder(USU).
handle_commands(Builder, Commands, Handler) ->
{telega_builder,
erlang:setelement(
3,
erlang:element(2, Builder),
[{handle_commands, Commands, Handler} |
erlang:element(3, erlang:element(2, Builder))]
)}.
-spec wait_commands(
telega@bot:context(UTB),
list(binary()),
fun((telega@bot:context(UTB), telega@update:command()) -> {ok, UTB} |
{error, binary()})
) -> {ok, UTB} | {error, binary()}.
wait_commands(Ctx, Commands, Continue) ->
telega@bot:wait_handler(Ctx, {handle_commands, Commands, Continue}).
-spec handle_text(
telega_builder(UTJ),
fun((telega@bot:context(UTJ), binary()) -> {ok, UTJ} | {error, binary()})
) -> telega_builder(UTJ).
handle_text(Builder, Handler) ->
{telega_builder,
erlang:setelement(
3,
erlang:element(2, Builder),
[{handle_text, Handler} |
erlang:element(3, erlang:element(2, Builder))]
)}.
-spec wait_text(
telega@bot:context(UTP),
fun((telega@bot:context(UTP), binary()) -> {ok, UTP} | {error, binary()})
) -> {ok, UTP} | {error, binary()}.
wait_text(Ctx, Continue) ->
telega@bot:wait_handler(Ctx, {handle_text, Continue}).
-spec handle_hears(
telega_builder(UTW),
telega@bot:hears(),
fun((telega@bot:context(UTW), binary()) -> {ok, UTW} | {error, binary()})
) -> telega_builder(UTW).
handle_hears(Builder, Hears, Handler) ->
{telega_builder,
erlang:setelement(
3,
erlang:element(2, Builder),
[{handle_hears, Hears, Handler} |
erlang:element(3, erlang:element(2, Builder))]
)}.
-spec wait_hears(
telega@bot:context(UUC),
telega@bot:hears(),
fun((telega@bot:context(UUC), binary()) -> {ok, UUC} | {error, binary()})
) -> {ok, UUC} | {error, binary()}.
wait_hears(Ctx, Hears, Continue) ->
telega@bot:wait_handler(Ctx, {handle_hears, Hears, Continue}).
-spec handle_callback_query(
telega_builder(UUI),
telega@bot:callback_query_filter(),
fun((telega@bot:context(UUI), binary(), binary()) -> {ok, UUI} |
{error, binary()})
) -> telega_builder(UUI).
handle_callback_query(Builder, Filter, Handler) ->
{telega_builder,
erlang:setelement(
3,
erlang:element(2, Builder),
[{handle_callback_query, Filter, Handler} |
erlang:element(3, erlang:element(2, Builder))]
)}.
-spec wait_callback_query(
telega@bot:context(UUO),
telega@bot:callback_query_filter(),
fun((telega@bot:context(UUO), binary(), binary()) -> {ok, UUO} |
{error, binary()})
) -> {ok, UUO} | {error, binary()}.
wait_callback_query(Ctx, Filter, Continue) ->
telega@bot:wait_handler(Ctx, {handle_callback_query, Filter, Continue}).
-spec log_context(
telega@bot:context(UUV),
binary(),
fun(() -> {ok, UUV} | {error, binary()})
) -> {ok, UUV} | {error, binary()}.
log_context(Ctx, Prefix, Handler) ->
Prefix@1 = <<<<"["/utf8, Prefix/binary>>/binary, "] "/utf8>>,
telega@log:info(
<<<<Prefix@1/binary, "Received update: "/utf8>>/binary,
(gleam@string:inspect(erlang:element(3, Ctx)))/binary>>
),
_pipe = Handler(),
gleam@result:map_error(
_pipe,
fun(E) ->
telega@log:error(
<<<<Prefix@1/binary, "Handler failed: "/utf8>>/binary,
(gleam@string:inspect(E))/binary>>
),
E
end
).
-spec with_session_settings(
telega_builder(UVB),
fun((binary(), UVB) -> {ok, UVB} | {error, binary()}),
fun((binary()) -> {ok, UVB} | {error, binary()})
) -> telega_builder(UVB).
with_session_settings(Builder, Persist_session, Get_session) ->
{telega_builder,
erlang:setelement(
4,
erlang:element(2, Builder),
{some, {session_settings, Persist_session, Get_session}}
)}.
-spec nil_session_settings(telega_builder(nil)) -> telega_builder(nil).
nil_session_settings(Builder) ->
{telega_builder,
erlang:setelement(
4,
erlang:element(2, Builder),
{some,
{session_settings,
fun(_, _) -> {ok, nil} end,
fun(_) -> {ok, nil} end}}
)}.
-spec init(telega_builder(UVO)) -> {ok, telega(UVO)} | {error, binary()}.
init(Builder) ->
{telega_builder, Telega} = Builder,
gleam@result:'try'(
telega@bot:set_webhook(erlang:element(2, Telega)),
fun(Is_ok) ->
gleam@bool:guard(
not Is_ok,
{error, <<"Failed to set webhook"/utf8>>},
fun() ->
Session_settings = gleam@option:to_result(
erlang:element(4, Telega),
<<"Session settings not initialized"/utf8>>
),
gleam@result:'try'(
Session_settings,
fun(Session_settings@1) ->
Telega_subject = gleam@erlang@process:new_subject(),
Registry_actor = gleam@otp@supervisor:supervisor(
fun(_) ->
telega@bot:start_registry(
erlang:element(2, Telega),
erlang:element(3, Telega),
Session_settings@1,
Telega_subject
)
end
),
gleam@result:'try'(
begin
_pipe = gleam@otp@supervisor:start(
fun(_capture) ->
gleam@otp@supervisor:add(
_capture,
Registry_actor
)
end
),
gleam@result:map_error(
_pipe,
fun(E) ->
<<"Failed to start telega:\n"/utf8,
(gleam@string:inspect(E))/binary>>
end
)
end,
fun(_) ->
gleam@result:'try'(
begin
_pipe@1 = gleam@erlang@process:'receive'(
Telega_subject,
1000
),
gleam@result:map_error(
_pipe@1,
fun(E@1) ->
<<"Failed to start registry:\n"/utf8,
(gleam@string:inspect(
E@1
))/binary>>
end
)
end,
fun(Registry_subject) ->
{ok,
erlang:setelement(
5,
Telega,
{some, Registry_subject}
)}
end
)
end
)
end
)
end
)
end
).
-spec init_nil_session(telega_builder(nil)) -> {ok, telega(nil)} |
{error, binary()}.
init_nil_session(Builder) ->
_pipe = Builder,
_pipe@1 = nil_session_settings(_pipe),
init(_pipe@1).
-spec handle_update(telega(any()), telega@update:update()) -> {ok, nil} |
{error, binary()}.
handle_update(Telega, Update) ->
Registry_subject = gleam@option:to_result(
erlang:element(5, Telega),
<<"Registry not initialized"/utf8>>
),
gleam@result:'try'(
Registry_subject,
fun(Registry_subject@1) ->
{ok,
gleam@otp@actor:send(
Registry_subject@1,
{handle_bot_registry_message, Update}
)}
end
).