-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(TTB) :: {telega, telega@config:config(), list(telega@bot:handler(TTB)), gleam@option:option(telega@bot:session_settings(TTB)), gleam@option:option(gleam@erlang@process:subject(telega@bot:registry_message()))}. -opaque telega_builder(TTC) :: {telega_builder, telega(TTC)}. -spec is_webhook_path(telega(any()), binary()) -> boolean(). is_webhook_path(Telega, Path) -> telega@config:get_webhook_path(erlang:element(2, Telega)) =:= Path. -spec is_secret_token_valid(telega(any()), binary()) -> boolean(). is_secret_token_valid(Telega, Token) -> telega@config:get_secret_token(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@config:new(Token, Server_url, Webhook_path, Secret_token), [], none, none}}. -spec handle_all( telega_builder(TTK), fun((telega@bot:context(TTK)) -> {ok, TTK} | {error, binary()}) ) -> telega_builder(TTK). 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(TTQ), fun((telega@bot:context(TTQ)) -> {ok, TTQ} | {error, binary()}) ) -> {ok, TTQ} | {error, binary()}. wait_any(Ctx, Continue) -> telega@bot:wait_handler(Ctx, {handle_all, Continue}). -spec handle_command( telega_builder(TTX), binary(), fun((telega@bot:context(TTX), telega@update:command()) -> {ok, TTX} | {error, binary()}) ) -> telega_builder(TTX). 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(TUD), binary(), fun((telega@bot:context(TUD), telega@update:command()) -> {ok, TUD} | {error, binary()}) ) -> {ok, TUD} | {error, binary()}. wait_command(Ctx, Command, Continue) -> telega@bot:wait_handler(Ctx, {handle_command, Command, Continue}). -spec handle_commands( telega_builder(TUK), list(binary()), fun((telega@bot:context(TUK), telega@update:command()) -> {ok, TUK} | {error, binary()}) ) -> telega_builder(TUK). 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(TUR), list(binary()), fun((telega@bot:context(TUR), telega@update:command()) -> {ok, TUR} | {error, binary()}) ) -> {ok, TUR} | {error, binary()}. wait_commands(Ctx, Commands, Continue) -> telega@bot:wait_handler(Ctx, {handle_commands, Commands, Continue}). -spec handle_text( telega_builder(TUZ), fun((telega@bot:context(TUZ), binary()) -> {ok, TUZ} | {error, binary()}) ) -> telega_builder(TUZ). 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(TVF), fun((telega@bot:context(TVF), binary()) -> {ok, TVF} | {error, binary()}) ) -> {ok, TVF} | {error, binary()}. wait_text(Ctx, Continue) -> telega@bot:wait_handler(Ctx, {handle_text, Continue}). -spec handle_hears( telega_builder(TVM), telega@bot:hears(), fun((telega@bot:context(TVM), binary()) -> {ok, TVM} | {error, binary()}) ) -> telega_builder(TVM). 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(TVS), telega@bot:hears(), fun((telega@bot:context(TVS), binary()) -> {ok, TVS} | {error, binary()}) ) -> {ok, TVS} | {error, binary()}. wait_hears(Ctx, Hears, Continue) -> telega@bot:wait_handler(Ctx, {handle_hears, Hears, Continue}). -spec handle_callback_query( telega_builder(TVY), telega@bot:callback_query_filter(), fun((telega@bot:context(TVY), binary(), binary()) -> {ok, TVY} | {error, binary()}) ) -> telega_builder(TVY). 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(TWE), telega@bot:callback_query_filter(), fun((telega@bot:context(TWE), binary(), binary()) -> {ok, TWE} | {error, binary()}) ) -> {ok, TWE} | {error, binary()}. wait_callback_query(Ctx, Filter, Continue) -> telega@bot:wait_handler(Ctx, {handle_callback_query, Filter, Continue}). -spec log_context( telega@bot:context(TWL), binary(), fun(() -> {ok, TWL} | {error, binary()}) ) -> {ok, TWL} | {error, binary()}. log_context(Ctx, Prefix, Handler) -> Prefix@1 = <<<<"["/utf8, Prefix/binary>>/binary, "] "/utf8>>, telega@log:info( <<<>/binary, (gleam@string:inspect(erlang:element(3, Ctx)))/binary>> ), _pipe = Handler(), gleam@result:map_error( _pipe, fun(E) -> telega@log:error( <<<>/binary, (gleam@string:inspect(E))/binary>> ), E end ). -spec with_session_settings( telega_builder(TWR), fun((binary(), TWR) -> {ok, TWR} | {error, binary()}), fun((binary()) -> {ok, TWR} | {error, binary()}) ) -> telega_builder(TWR). 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(TXE)) -> {ok, telega(TXE)} | {error, binary()}. init(Builder) -> {telega_builder, Telega} = Builder, gleam@result:'try'( telega@api: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 ).