Current section

Files

Jump to
telega src telega.erl
Raw

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(UTZ) :: {telega,
telega@internal@config:config(),
telega@model:user(),
list(telega@bot:handler(UTZ)),
telega@bot:session_settings(UTZ),
gleam@erlang@process:subject(telega@bot:registry_message())}.
-opaque telega_builder(UUA) :: {telega_builder,
telega@internal@config:config(),
list(telega@bot:handler(UUA)),
gleam@option:option(telega@bot:session_settings(UUA)),
gleam@option:option(gleam@erlang@process:subject(telega@bot:registry_message()))}.
-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@internal@config:new(
Token,
Server_url,
Webhook_path,
Secret_token
),
[],
none,
none}.
-spec handle_all(
telega_builder(UUI),
fun((telega@bot:context(UUI)) -> {ok, UUI} | {error, binary()})
) -> telega_builder(UUI).
handle_all(Builder, Handler) ->
erlang:setelement(
3,
Builder,
[{handle_all, Handler} | erlang:element(3, Builder)]
).
-spec wait_any(
telega@bot:context(UUO),
fun((telega@bot:context(UUO)) -> {ok, UUO} | {error, binary()})
) -> {ok, UUO} | {error, binary()}.
wait_any(Ctx, Continue) ->
telega@bot:wait_handler(Ctx, {handle_all, Continue}).
-spec handle_command(
telega_builder(UUV),
binary(),
fun((telega@bot:context(UUV), telega@update:command()) -> {ok, UUV} |
{error, binary()})
) -> telega_builder(UUV).
handle_command(Builder, Command, Handler) ->
erlang:setelement(
3,
Builder,
[{handle_command, Command, Handler} | erlang:element(3, Builder)]
).
-spec wait_command(
telega@bot:context(UVB),
binary(),
fun((telega@bot:context(UVB), telega@update:command()) -> {ok, UVB} |
{error, binary()})
) -> {ok, UVB} | {error, binary()}.
wait_command(Ctx, Command, Continue) ->
telega@bot:wait_handler(Ctx, {handle_command, Command, Continue}).
-spec handle_commands(
telega_builder(UVI),
list(binary()),
fun((telega@bot:context(UVI), telega@update:command()) -> {ok, UVI} |
{error, binary()})
) -> telega_builder(UVI).
handle_commands(Builder, Commands, Handler) ->
erlang:setelement(
3,
Builder,
[{handle_commands, Commands, Handler} | erlang:element(3, Builder)]
).
-spec wait_commands(
telega@bot:context(UVP),
list(binary()),
fun((telega@bot:context(UVP), telega@update:command()) -> {ok, UVP} |
{error, binary()})
) -> {ok, UVP} | {error, binary()}.
wait_commands(Ctx, Commands, Continue) ->
telega@bot:wait_handler(Ctx, {handle_commands, Commands, Continue}).
-spec handle_text(
telega_builder(UVX),
fun((telega@bot:context(UVX), binary()) -> {ok, UVX} | {error, binary()})
) -> telega_builder(UVX).
handle_text(Builder, Handler) ->
erlang:setelement(
3,
Builder,
[{handle_text, Handler} | erlang:element(3, Builder)]
).
-spec wait_text(
telega@bot:context(UWD),
fun((telega@bot:context(UWD), binary()) -> {ok, UWD} | {error, binary()})
) -> {ok, UWD} | {error, binary()}.
wait_text(Ctx, Continue) ->
telega@bot:wait_handler(Ctx, {handle_text, Continue}).
-spec handle_hears(
telega_builder(UWK),
telega@bot:hears(),
fun((telega@bot:context(UWK), binary()) -> {ok, UWK} | {error, binary()})
) -> telega_builder(UWK).
handle_hears(Builder, Hears, Handler) ->
erlang:setelement(
3,
Builder,
[{handle_hears, Hears, Handler} | erlang:element(3, Builder)]
).
-spec wait_hears(
telega@bot:context(UWQ),
telega@bot:hears(),
fun((telega@bot:context(UWQ), binary()) -> {ok, UWQ} | {error, binary()})
) -> {ok, UWQ} | {error, binary()}.
wait_hears(Ctx, Hears, Continue) ->
telega@bot:wait_handler(Ctx, {handle_hears, Hears, Continue}).
-spec handle_callback_query(
telega_builder(UWW),
telega@bot:callback_query_filter(),
fun((telega@bot:context(UWW), binary(), binary()) -> {ok, UWW} |
{error, binary()})
) -> telega_builder(UWW).
handle_callback_query(Builder, Filter, Handler) ->
erlang:setelement(
3,
Builder,
[{handle_callback_query, Filter, Handler} | erlang:element(3, Builder)]
).
-spec wait_callback_query(
telega@bot:context(UXC),
telega@bot:callback_query_filter(),
fun((telega@bot:context(UXC), binary(), binary()) -> {ok, UXC} |
{error, binary()})
) -> {ok, UXC} | {error, binary()}.
wait_callback_query(Ctx, Filter, Continue) ->
telega@bot:wait_handler(Ctx, {handle_callback_query, Filter, Continue}).
-spec log_context(
telega@bot:context(UXJ),
binary(),
fun(() -> {ok, UXJ} | {error, binary()})
) -> {ok, UXJ} | {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(UXP),
fun((binary(), UXP) -> {ok, UXP} | {error, binary()}),
fun((binary()) -> {ok, UXP} | {error, binary()})
) -> telega_builder(UXP).
with_session_settings(Builder, Persist_session, Get_session) ->
erlang:setelement(
4,
Builder,
{some, {session_settings, Persist_session, Get_session}}
).
-spec nil_session_settings(telega_builder(nil)) -> telega_builder(nil).
nil_session_settings(Builder) ->
erlang:setelement(
4,
Builder,
{some,
{session_settings,
fun(_, _) -> {ok, nil} end,
fun(_) -> {ok, nil} end}}
).
-spec init(telega_builder(UYC)) -> {ok, telega(UYC)} | {error, binary()}.
init(Builder) ->
gleam@result:'try'(
telega@bot:set_webhook(erlang:element(2, Builder)),
fun(Is_ok) ->
gleam@bool:guard(
not Is_ok,
{error, <<"Failed to set webhook"/utf8>>},
fun() ->
gleam@result:'try'(
telega@api:get_me(
erlang:element(5, erlang:element(2, Builder))
),
fun(Bot_info) ->
Session_settings = gleam@option:to_result(
erlang:element(4, Builder),
<<"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, Builder),
erlang:element(3, Builder),
Session_settings@1,
Telega_subject,
Bot_info
)
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,
{telega,
erlang:element(
2,
Builder
),
Bot_info,
erlang:element(
3,
Builder
),
Session_settings@1,
Registry_subject}}
end
)
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) ->
{ok,
gleam@otp@actor:send(
erlang:element(6, Telega),
{handle_bot_registry_message, Update}
)}.