-module(telega@flow@handler). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/telega/flow/handler.gleam"). -export([text_step/3, message_step/2, create_resume_handler/1, create_resume_handler_with_keyboard/2, create_text_handler/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC(" Built-in step handlers and resume handler factories.\n"). -file("src/telega/flow/handler.gleam", 18). ?DOC(" Create a text input step\n"). -spec text_step(binary(), binary(), ARGO) -> fun((telega@bot:context(ARGP, ARGQ), telega@flow@types:flow_instance()) -> {ok, {telega@bot:context(ARGP, ARGQ), telega@flow@types:flow_action(ARGO), telega@flow@types:flow_instance()}} | {error, ARGQ}). text_step(Prompt, Data_key, Next_step) -> fun(Ctx, Instance_val) -> case telega@flow@instance:get_wait_result(Instance_val) of {text_input, Value} -> Instance_val@1 = telega@flow@instance:store_data( Instance_val, Data_key, Value ), {ok, {Ctx, {next, Next_step}, Instance_val@1}}; pending -> case telega@reply:with_text(Ctx, Prompt) of {ok, _} -> {ok, {Ctx, wait, Instance_val}}; {error, _} -> {ok, {Ctx, cancel, Instance_val}} end; _ -> case telega@reply:with_text(Ctx, Prompt) of {ok, _} -> {ok, {Ctx, wait, Instance_val}}; {error, _} -> {ok, {Ctx, cancel, Instance_val}} end end end. -file("src/telega/flow/handler.gleam", 46). ?DOC(" Create a message display step\n"). -spec message_step( fun((telega@flow@types:flow_instance()) -> binary()), gleam@option:option(ARGU) ) -> fun((telega@bot:context(ARGW, ARGX), telega@flow@types:flow_instance()) -> {ok, {telega@bot:context(ARGW, ARGX), telega@flow@types:flow_action(ARGU), telega@flow@types:flow_instance()}} | {error, ARGX}). message_step(Message_fn, Next_step) -> fun(Ctx, Instance_val) -> Message = Message_fn(Instance_val), case telega@reply:with_text(Ctx, Message) of {ok, _} -> case Next_step of {some, Step} -> {ok, {Ctx, {next, Step}, Instance_val}}; none -> {ok, {Ctx, {complete, erlang:element( 3, erlang:element(6, Instance_val) )}, Instance_val}} end; {error, _} -> {ok, {Ctx, cancel, Instance_val}} end end. -file("src/telega/flow/handler.gleam", 107). -spec resume_handler(telega@flow@types:flow(any(), ARIN, ARIO)) -> fun((telega@bot:context(ARIN, ARIO), telega@update:update()) -> {ok, telega@bot:context(ARIN, ARIO)} | {error, ARIO}). resume_handler(Flow) -> fun(Ctx, Upd) -> case Upd of {callback_query_update, _, _, Query, _} -> Data = gleam@option:unwrap( erlang:element(7, Query), <<""/utf8>> ), Token@1 = case gleam@string:split(Data, <<":"/utf8>>) of [_, Token | _] -> Token; _ -> Data end, Resume_data = maps:from_list( [{<<"callback_data"/utf8>>, Data}, {<<"__wait_result"/utf8>>, telega@flow@instance:encode_callback_wait_result( Data )}] ), telega@flow@engine:resume_with_token( Flow, Ctx, Token@1, {some, Resume_data} ); _ -> {ok, Ctx} end end. -file("src/telega/flow/handler.gleam", 65). ?DOC(" Create a router handler for resuming flows from callback queries\n"). -spec create_resume_handler(telega@flow@types:flow(any(), ARHC, ARHD)) -> fun((telega@bot:context(ARHC, ARHD), telega@update:update()) -> {ok, telega@bot:context(ARHC, ARHD)} | {error, ARHD}). create_resume_handler(Flow) -> resume_handler(Flow). -file("src/telega/flow/handler.gleam", 131). -spec resume_handler_with_keyboard( telega@flow@types:flow(any(), ARIZ, ARJA), telega@keyboard:keyboard_callback_data(binary()) ) -> fun((telega@bot:context(ARIZ, ARJA), telega@update:update()) -> {ok, telega@bot:context(ARIZ, ARJA)} | {error, ARJA}). resume_handler_with_keyboard(Flow, Callback_data) -> fun(Ctx, Upd) -> case Upd of {callback_query_update, _, _, Query, _} -> Data = gleam@option:unwrap( erlang:element(7, Query), <<""/utf8>> ), Wait_result_value = telega@flow@instance:encode_callback_wait_result( Data ), Resume_data = maps:from_list( [{<<"callback_data"/utf8>>, Data}, {<<"__wait_result"/utf8>>, Wait_result_value}] ), case telega@keyboard:unpack_callback(Data, Callback_data) of {ok, Callback} -> telega@flow@engine:resume_with_token( Flow, Ctx, erlang:element(2, Callback), {some, Resume_data} ); {error, _} -> Token@1 = case gleam@string:split(Data, <<":"/utf8>>) of [_, Token | _] -> Token; _ -> Data end, telega@flow@engine:resume_with_token( Flow, Ctx, Token@1, {some, Resume_data} ) end; _ -> {ok, Ctx} end end. -file("src/telega/flow/handler.gleam", 73). ?DOC(" Create a router handler for resuming flows from callback queries with keyboard parsing\n"). -spec create_resume_handler_with_keyboard( telega@flow@types:flow(any(), ARHO, ARHP), telega@keyboard:keyboard_callback_data(binary()) ) -> fun((telega@bot:context(ARHO, ARHP), telega@update:update()) -> {ok, telega@bot:context(ARHO, ARHP)} | {error, ARHP}). create_resume_handler_with_keyboard(Flow, Callback_data) -> resume_handler_with_keyboard(Flow, Callback_data). -file("src/telega/flow/handler.gleam", 82). ?DOC(" Create a text handler for resuming flows\n"). -spec create_text_handler(telega@flow@types:flow(any(), ARIB, ARIC)) -> fun((telega@bot:context(ARIB, ARIC), telega@update:update()) -> {ok, telega@bot:context(ARIB, ARIC)} | {error, ARIC}). create_text_handler(Flow) -> fun(Ctx, Upd) -> case Upd of {text_update, From_id, Chat_id, Text, _, _} -> case (erlang:element(5, erlang:element(7, Flow)))( From_id, Chat_id ) of {ok, [Inst | _]} when erlang:element(8, Inst) =/= none -> Data = maps:from_list([{<<"user_input"/utf8>>, Text}]), telega@flow@engine:resume_with_token( Flow, Ctx, gleam@option:unwrap( erlang:element(8, Inst), <<""/utf8>> ), {some, Data} ); _ -> {ok, Ctx} end; _ -> {ok, Ctx} end end.