Current section
Files
Jump to
Current section
Files
src/telega@flow@handler.erl
-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_text_handler/1, create_resume_handler/1, create_resume_handler_with_keyboard/2]).
-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(), APAS) -> fun((telega@bot:context(APAT, APAU), telega@flow@types:flow_instance()) -> {ok,
{telega@bot:context(APAT, APAU),
telega@flow@types:flow_action(APAS),
telega@flow@types:flow_instance()}} |
{error, APAU}).
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(APAY)
) -> fun((telega@bot:context(APBA, APBB), telega@flow@types:flow_instance()) -> {ok,
{telega@bot:context(APBA, APBB),
telega@flow@types:flow_action(APAY),
telega@flow@types:flow_instance()}} |
{error, APBB}).
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", 82).
?DOC(" Create a text handler for resuming flows\n").
-spec create_text_handler(telega@flow@types:flow(any(), APCF, APCG)) -> fun((telega@bot:context(APCF, APCG), telega@update:update()) -> {ok,
telega@bot:context(APCF, APCG)} |
{error, APCG}).
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.
-file("src/telega/flow/handler.gleam", 107).
-spec resume_handler(telega@flow@types:flow(any(), APCR, APCS)) -> fun((telega@bot:context(APCR, APCS), telega@update:update()) -> {ok,
telega@bot:context(APCR, APCS)} |
{error, APCS}).
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(), APBG, APBH)) -> fun((telega@bot:context(APBG, APBH), telega@update:update()) -> {ok,
telega@bot:context(APBG, APBH)} |
{error, APBH}).
create_resume_handler(Flow) ->
resume_handler(Flow).
-file("src/telega/flow/handler.gleam", 131).
-spec resume_handler_with_keyboard(
telega@flow@types:flow(any(), APDD, APDE),
telega@keyboard:keyboard_callback_data(binary())
) -> fun((telega@bot:context(APDD, APDE), telega@update:update()) -> {ok,
telega@bot:context(APDD, APDE)} |
{error, APDE}).
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(), APBS, APBT),
telega@keyboard:keyboard_callback_data(binary())
) -> fun((telega@bot:context(APBS, APBT), telega@update:update()) -> {ok,
telega@bot:context(APBS, APBT)} |
{error, APBT}).
create_resume_handler_with_keyboard(Flow, Callback_data) ->
resume_handler_with_keyboard(Flow, Callback_data).