Current section
Files
Jump to
Current section
Files
src/telega@flow@registry.erl
-module(telega@flow@registry).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/telega/flow/registry.gleam").
-export([new_registry/0, register_with_data/4, register/3, register_callable/2, register_cancel_command_with/3, register_cancel_command/2, cancel_user_flows/3, cancel_flow_instance/2, call_flow/4, apply_to_router/2, to_handler/1]).
-export_type([flow_registry/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(" FlowRegistry and router integration.\n").
-opaque flow_registry(ARSB, ARSC) :: {flow_registry,
list({telega@flow@types:flow_trigger(),
telega@flow@types:flow(gleam@dynamic:dynamic_(), ARSB, ARSC),
gleam@dict:dict(binary(), binary())}),
gleam@dict:dict(binary(), telega@flow@types:flow(gleam@dynamic:dynamic_(), ARSB, ARSC)),
list({binary(),
fun((telega@bot:context(ARSB, ARSC), list(binary())) -> {ok,
telega@bot:context(ARSB, ARSC)} |
{error, ARSC})})}.
-file("src/telega/flow/registry.gleam", 40).
?DOC(" Create a new empty flow registry\n").
-spec new_registry() -> flow_registry(any(), any()).
new_registry() ->
{flow_registry, [], maps:new(), []}.
-file("src/telega/flow/registry.gleam", 703).
-spec unsafe_coerce(any()) -> any().
unsafe_coerce(Value) ->
telega@internal@coerce:unsafe_coerce(Value).
-file("src/telega/flow/registry.gleam", 54).
?DOC(" Add a flow to the registry with a trigger and initial data\n").
-spec register_with_data(
flow_registry(ARSR, ARSS),
telega@flow@types:flow_trigger(),
telega@flow@types:flow(any(), ARSR, ARSS),
gleam@dict:dict(binary(), binary())
) -> flow_registry(ARSR, ARSS).
register_with_data(Registry, Trigger, Flow, Initial_data) ->
Coerced_flow = unsafe_coerce(Flow),
{flow_registry,
lists:append(
erlang:element(2, Registry),
[{Trigger, Coerced_flow, Initial_data}]
),
gleam@dict:insert(
erlang:element(3, Registry),
erlang:element(2, Flow),
Coerced_flow
),
erlang:element(4, Registry)}.
-file("src/telega/flow/registry.gleam", 45).
?DOC(" Add a flow to the registry with a trigger\n").
-spec register(
flow_registry(ARSH, ARSI),
telega@flow@types:flow_trigger(),
telega@flow@types:flow(any(), ARSH, ARSI)
) -> flow_registry(ARSH, ARSI).
register(Registry, Trigger, Flow) ->
register_with_data(Registry, Trigger, Flow, maps:new()).
-file("src/telega/flow/registry.gleam", 69).
?DOC(" Register a flow without a trigger (for calling from handlers)\n").
-spec register_callable(
flow_registry(ARTD, ARTE),
telega@flow@types:flow(any(), ARTD, ARTE)
) -> flow_registry(ARTD, ARTE).
register_callable(Registry, Flow) ->
Coerced_flow = unsafe_coerce(Flow),
{flow_registry,
erlang:element(2, Registry),
gleam@dict:insert(
erlang:element(3, Registry),
erlang:element(2, Flow),
Coerced_flow
),
erlang:element(4, Registry)}.
-file("src/telega/flow/registry.gleam", 92).
?DOC(" Register a cancel command with a custom callback\n").
-spec register_cancel_command_with(
flow_registry(ARTT, ARTU),
binary(),
fun((telega@bot:context(ARTT, ARTU), list(binary())) -> {ok,
telega@bot:context(ARTT, ARTU)} |
{error, ARTU})
) -> flow_registry(ARTT, ARTU).
register_cancel_command_with(Registry, Command, On_cancel) ->
{flow_registry,
erlang:element(2, Registry),
erlang:element(3, Registry),
lists:append(erlang:element(4, Registry), [{Command, On_cancel}])}.
-file("src/telega/flow/registry.gleam", 82).
?DOC(" Register a cancel command that cancels all active flows for the user\n").
-spec register_cancel_command(flow_registry(ARTN, ARTO), binary()) -> flow_registry(ARTN, ARTO).
register_cancel_command(Registry, Command) ->
register_cancel_command_with(
Registry,
Command,
fun(Ctx, _) -> {ok, Ctx} end
).
-file("src/telega/flow/registry.gleam", 107).
?DOC(" Cancel all flows for a user in a chat\n").
-spec cancel_user_flows(flow_registry(any(), ARUH), integer(), integer()) -> {ok,
list(binary())} |
{error, ARUH}.
cancel_user_flows(Registry, User_id, Chat_id) ->
Flows = maps:values(erlang:element(3, Registry)),
gleam@list:try_fold(
Flows,
[],
fun(Acc, Flow) ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(Flow_id) of
{ok, {some, _}} ->
case (erlang:element(4, erlang:element(7, Flow)))(Flow_id) of
{ok, _} ->
{ok, [Flow_id | Acc]};
{error, Err} ->
{error, Err}
end;
{ok, none} ->
{ok, Acc};
{error, Err@1} ->
{error, Err@1}
end
end
).
-file("src/telega/flow/registry.gleam", 129).
?DOC(" Cancel a specific flow instance by ID\n").
-spec cancel_flow_instance(flow_registry(any(), ARUO), binary()) -> {ok,
boolean()} |
{error, ARUO}.
cancel_flow_instance(Registry, Flow_id) ->
Flows = maps:values(erlang:element(3, Registry)),
Found = gleam@list:fold(Flows, false, fun(Acc, Flow) -> case Acc of
true ->
true;
false ->
case (erlang:element(3, erlang:element(7, Flow)))(Flow_id) of
{ok, {some, _}} ->
_ = (erlang:element(4, erlang:element(7, Flow)))(
Flow_id
),
true;
_ ->
false
end
end end),
{ok, Found}.
-file("src/telega/flow/registry.gleam", 235).
-spec start(
telega@flow@types:flow(any(), ARWA, ARWB),
gleam@dict:dict(binary(), binary()),
telega@bot:context(ARWA, ARWB)
) -> {ok, telega@bot:context(ARWA, ARWB)} | {error, ARWB}.
start(Flow, Initial_data, Ctx) ->
{From_id, Chat_id} = telega@flow@engine:extract_ids_from_context(Ctx),
telega@flow@engine:start_or_resume(
Flow,
Ctx,
From_id,
Chat_id,
Initial_data
).
-file("src/telega/flow/registry.gleam", 152).
?DOC(" Call a registered flow from any handler\n").
-spec call_flow(
telega@bot:context(ARUT, ARUU),
flow_registry(ARUT, ARUU),
binary(),
gleam@dict:dict(binary(), binary())
) -> {ok, telega@bot:context(ARUT, ARUU)} | {error, ARUU}.
call_flow(Ctx, Registry, Flow_name, Initial_data) ->
case gleam_stdlib:map_get(erlang:element(3, Registry), Flow_name) of
{ok, Found_flow} ->
start(Found_flow, Initial_data, Ctx);
{error, _} ->
{ok, Ctx}
end.
-file("src/telega/flow/registry.gleam", 296).
?DOC(
" Check if instance is expired, run timeout/exit hooks if so, delete and return True.\n"
" Returns False if not expired.\n"
).
-spec run_timeout_and_cleanup(
telega@flow@types:flow(gleam@dynamic:dynamic_(), ARXM, ARXN),
telega@bot:context(ARXM, ARXN),
telega@flow@types:flow_instance()
) -> {boolean(), {ok, telega@bot:context(ARXM, ARXN)} | {error, ARXN}}.
run_timeout_and_cleanup(Flow, Ctx, Inst) ->
case telega@flow@instance:is_expired(Inst, erlang:element(17, Flow)) of
true ->
Ctx_result = case erlang:element(18, Flow) of
{some, Timeout_fn} ->
case Timeout_fn(Ctx, Inst) of
{ok, New_ctx} ->
case erlang:element(16, Flow) of
{some, Exit_fn} ->
Exit_fn(New_ctx, Inst);
none ->
{ok, New_ctx}
end;
{error, Err} ->
{error, Err}
end;
none ->
case erlang:element(16, Flow) of
{some, Exit_fn@1} ->
Exit_fn@1(Ctx, Inst);
none ->
{ok, Ctx}
end
end,
_ = (erlang:element(4, erlang:element(7, Flow)))(
erlang:element(2, Inst)
),
{true, Ctx_result};
false ->
{false, {ok, Ctx}}
end.
-file("src/telega/flow/registry.gleam", 651).
-spec auto_resume_command_handler(flow_registry(ASAQ, ASAR)) -> fun((telega@bot:context(ASAQ, ASAR), telega@update:update()) -> {ok,
telega@bot:context(ASAQ, ASAR)} |
{error, ASAR}).
auto_resume_command_handler(Registry) ->
fun(Ctx, Upd) -> case Upd of
{command_update, _, _, Command, _, _} ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(
Ctx
),
Flows = maps:values(erlang:element(3, Registry)),
Payload = gleam@option:unwrap(
erlang:element(4, Command),
<<""/utf8>>
),
Result = gleam@list:fold(
Flows,
none,
fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(
Flow_id
) of
{ok, {some, Inst}} when erlang:element(
8,
Inst
) =/= none ->
case run_timeout_and_cleanup(
Flow,
Ctx,
Inst
) of
{true, _} ->
none;
{false, _} ->
Data = maps:from_list(
[{<<"__wait_result"/utf8>>,
<<<<<<"command:"/utf8,
(erlang:element(
3,
Command
))/binary>>/binary,
":"/utf8>>/binary,
Payload/binary>>},
{<<"command"/utf8>>,
erlang:element(
3,
Command
)},
{<<"command_payload"/utf8>>,
Payload}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Data}
),
{some, _pipe}
end;
_ ->
none
end
end end
),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end;
_ ->
{ok, Ctx}
end end.
-file("src/telega/flow/registry.gleam", 588).
-spec auto_resume_location_handler(flow_registry(ASAG, ASAH)) -> fun((telega@bot:context(ASAG, ASAH), telega@update:update()) -> {ok,
telega@bot:context(ASAG, ASAH)} |
{error, ASAH}).
auto_resume_location_handler(Registry) ->
fun(Ctx, Upd) -> case Upd of
{message_update, _, _, Message, _} ->
case erlang:element(59, Message) of
{some, Location} ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(
Ctx
),
Flows = maps:values(erlang:element(3, Registry)),
Lat_str = gleam_stdlib:float_to_string(
erlang:element(2, Location)
),
Lng_str = gleam_stdlib:float_to_string(
erlang:element(3, Location)
),
Result = gleam@list:fold(
Flows,
none,
fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(
3,
erlang:element(7, Flow)
))(Flow_id) of
{ok, {some, Inst}} when erlang:element(
8,
Inst
) =/= none ->
case run_timeout_and_cleanup(
Flow,
Ctx,
Inst
) of
{true, _} ->
none;
{false, _} ->
Data = maps:from_list(
[{<<"__wait_result"/utf8>>,
<<<<<<"location:"/utf8,
Lat_str/binary>>/binary,
","/utf8>>/binary,
Lng_str/binary>>},
{<<"__location_lat"/utf8>>,
Lat_str},
{<<"__location_lng"/utf8>>,
Lng_str}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Data}
),
{some, _pipe}
end;
_ ->
none
end
end end
),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end;
none ->
{ok, Ctx}
end;
_ ->
{ok, Ctx}
end end.
-file("src/telega/flow/registry.gleam", 546).
-spec auto_resume_audio_handler(flow_registry(ARZW, ARZX)) -> fun((telega@bot:context(ARZW, ARZX), telega@model@types:audio()) -> {ok,
telega@bot:context(ARZW, ARZX)} |
{error, ARZX}).
auto_resume_audio_handler(Registry) ->
fun(Ctx, Audio) ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(Ctx),
Flows = maps:values(erlang:element(3, Registry)),
Result = gleam@list:fold(Flows, none, fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(
Flow_id
) of
{ok, {some, Inst}} when erlang:element(8, Inst) =/= none ->
case run_timeout_and_cleanup(Flow, Ctx, Inst) of
{true, _} ->
none;
{false, _} ->
Data = maps:from_list(
[{<<"__wait_result"/utf8>>,
<<"audio:"/utf8,
(erlang:element(
2,
Audio
))/binary>>},
{<<"__audio_file_id"/utf8>>,
erlang:element(2, Audio)}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Data}
),
{some, _pipe}
end;
_ ->
none
end
end end),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end
end.
-file("src/telega/flow/registry.gleam", 504).
-spec auto_resume_voice_handler(flow_registry(ARZM, ARZN)) -> fun((telega@bot:context(ARZM, ARZN), telega@model@types:voice()) -> {ok,
telega@bot:context(ARZM, ARZN)} |
{error, ARZN}).
auto_resume_voice_handler(Registry) ->
fun(Ctx, Voice) ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(Ctx),
Flows = maps:values(erlang:element(3, Registry)),
Result = gleam@list:fold(Flows, none, fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(
Flow_id
) of
{ok, {some, Inst}} when erlang:element(8, Inst) =/= none ->
case run_timeout_and_cleanup(Flow, Ctx, Inst) of
{true, _} ->
none;
{false, _} ->
Data = maps:from_list(
[{<<"__wait_result"/utf8>>,
<<"voice:"/utf8,
(erlang:element(
2,
Voice
))/binary>>},
{<<"__voice_file_id"/utf8>>,
erlang:element(2, Voice)}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Data}
),
{some, _pipe}
end;
_ ->
none
end
end end),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end
end.
-file("src/telega/flow/registry.gleam", 462).
-spec auto_resume_video_handler(flow_registry(ARZC, ARZD)) -> fun((telega@bot:context(ARZC, ARZD), telega@model@types:video()) -> {ok,
telega@bot:context(ARZC, ARZD)} |
{error, ARZD}).
auto_resume_video_handler(Registry) ->
fun(Ctx, Video) ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(Ctx),
Flows = maps:values(erlang:element(3, Registry)),
Result = gleam@list:fold(Flows, none, fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(
Flow_id
) of
{ok, {some, Inst}} when erlang:element(8, Inst) =/= none ->
case run_timeout_and_cleanup(Flow, Ctx, Inst) of
{true, _} ->
none;
{false, _} ->
Data = maps:from_list(
[{<<"__wait_result"/utf8>>,
<<"video:"/utf8,
(erlang:element(
2,
Video
))/binary>>},
{<<"__video_file_id"/utf8>>,
erlang:element(2, Video)}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Data}
),
{some, _pipe}
end;
_ ->
none
end
end end),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end
end.
-file("src/telega/flow/registry.gleam", 418).
-spec auto_resume_photo_handler(flow_registry(ARYR, ARYS)) -> fun((telega@bot:context(ARYR, ARYS), list(telega@model@types:photo_size())) -> {ok,
telega@bot:context(ARYR, ARYS)} |
{error, ARYS}).
auto_resume_photo_handler(Registry) ->
fun(Ctx, Photos) ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(Ctx),
Flows = maps:values(erlang:element(3, Registry)),
File_ids = gleam@list:map(Photos, fun(P) -> erlang:element(2, P) end),
File_ids_str = gleam@string:join(File_ids, <<","/utf8>>),
Result = gleam@list:fold(Flows, none, fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(
Flow_id
) of
{ok, {some, Inst}} when erlang:element(8, Inst) =/= none ->
case run_timeout_and_cleanup(Flow, Ctx, Inst) of
{true, _} ->
none;
{false, _} ->
Data = maps:from_list(
[{<<"__wait_result"/utf8>>,
<<"photo:"/utf8,
File_ids_str/binary>>},
{<<"__photos"/utf8>>,
File_ids_str}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Data}
),
{some, _pipe}
end;
_ ->
none
end
end end),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end
end.
-file("src/telega/flow/registry.gleam", 369).
-spec auto_resume_callback_handler(flow_registry(ARYH, ARYI)) -> fun((telega@bot:context(ARYH, ARYI), binary(), binary()) -> {ok,
telega@bot:context(ARYH, ARYI)} |
{error, ARYI}).
auto_resume_callback_handler(Registry) ->
fun(Ctx, _, Data) ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(Ctx),
Flows = maps:values(erlang:element(3, Registry)),
Result = gleam@list:fold(Flows, none, fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(
Flow_id
) of
{ok, {some, Inst}} when erlang:element(8, Inst) =/= none ->
case run_timeout_and_cleanup(Flow, Ctx, Inst) of
{true, _} ->
none;
{false, _} ->
Wait_result_value = telega@flow@instance:encode_callback_wait_result(
Data
),
Callback_data = maps:from_list(
[{<<"callback_data"/utf8>>, Data},
{<<"__wait_result"/utf8>>,
Wait_result_value}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Callback_data}
),
{some, _pipe}
end;
_ ->
none
end
end end),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end
end.
-file("src/telega/flow/registry.gleam", 326).
-spec auto_resume_handler(flow_registry(ARXX, ARXY)) -> fun((telega@bot:context(ARXX, ARXY), binary()) -> {ok,
telega@bot:context(ARXX, ARXY)} |
{error, ARXY}).
auto_resume_handler(Registry) ->
fun(Ctx, Text) ->
{User_id, Chat_id} = telega@flow@engine:extract_ids_from_context(Ctx),
Flows = maps:values(erlang:element(3, Registry)),
Result = gleam@list:fold(Flows, none, fun(Acc, Flow) -> case Acc of
{some, _} ->
Acc;
none ->
Flow_id = telega@flow@storage:generate_id(
User_id,
Chat_id,
erlang:element(2, Flow)
),
case (erlang:element(3, erlang:element(7, Flow)))(
Flow_id
) of
{ok, {some, Inst}} when erlang:element(8, Inst) =/= none ->
case run_timeout_and_cleanup(Flow, Ctx, Inst) of
{true, _} ->
none;
{false, _} ->
Data = maps:from_list(
[{<<"user_input"/utf8>>, Text},
{<<"__wait_result"/utf8>>,
<<"text:"/utf8,
Text/binary>>}]
),
_pipe = telega@flow@engine:resume_with_instance(
Flow,
Ctx,
Inst,
{some, Data}
),
{some, _pipe}
end;
_ ->
none
end
end end),
case Result of
{some, Res} ->
Res;
none ->
{ok, Ctx}
end
end.
-file("src/telega/flow/registry.gleam", 244).
-spec to_handler_with_data(
telega@flow@types:flow(any(), ARWO, ARWP),
gleam@dict:dict(binary(), binary())
) -> fun((telega@bot:context(ARWO, ARWP), telega@update:command()) -> {ok,
telega@bot:context(ARWO, ARWP)} |
{error, ARWP}).
to_handler_with_data(Flow, Initial_data) ->
fun(Ctx, _) -> start(Flow, Initial_data, Ctx) end.
-file("src/telega/flow/registry.gleam", 252).
-spec add_flow_route(
telega@router:router(ARXB, ARXC),
telega@flow@types:flow_trigger(),
telega@flow@types:flow(gleam@dynamic:dynamic_(), ARXB, ARXC),
gleam@dict:dict(binary(), binary())
) -> telega@router:router(ARXB, ARXC).
add_flow_route(Router, Trigger, Flow, Initial_data) ->
case Trigger of
{on_command, Command} ->
telega@router:on_command(
Router,
Command,
to_handler_with_data(Flow, Initial_data)
);
{on_text, Pattern} ->
telega@router:on_text(
Router,
Pattern,
fun(Ctx, _) -> start(Flow, Initial_data, Ctx) end
);
{on_callback, Pattern@1} ->
telega@router:on_callback(
Router,
Pattern@1,
fun(Ctx@1, _, _) -> start(Flow, Initial_data, Ctx@1) end
);
{on_filtered, Filter} ->
telega@router:on_filtered(
Router,
Filter,
fun(Ctx@2, _) -> start(Flow, Initial_data, Ctx@2) end
);
on_photo ->
telega@router:on_photo(
Router,
fun(Ctx@3, _) -> start(Flow, Initial_data, Ctx@3) end
);
on_video ->
telega@router:on_video(
Router,
fun(Ctx@4, _) -> start(Flow, Initial_data, Ctx@4) end
);
on_audio ->
telega@router:on_audio(
Router,
fun(Ctx@5, _) -> start(Flow, Initial_data, Ctx@5) end
);
on_voice ->
telega@router:on_voice(
Router,
fun(Ctx@6, _) -> start(Flow, Initial_data, Ctx@6) end
);
on_any_text ->
telega@router:on_any_text(
Router,
fun(Ctx@7, _) -> start(Flow, Initial_data, Ctx@7) end
)
end.
-file("src/telega/flow/registry.gleam", 165).
?DOC(" Apply all registered flows to a router\n").
-spec apply_to_router(
telega@router:router(ARVF, ARVG),
flow_registry(ARVF, ARVG)
) -> telega@router:router(ARVF, ARVG).
apply_to_router(Router, Registry) ->
Router_with_flows = gleam@list:fold(
erlang:element(2, Registry),
Router,
fun(Router@1, Flow_entry) ->
{Trigger, Flow, Initial_data} = Flow_entry,
add_flow_route(Router@1, Trigger, Flow, Initial_data)
end
),
Router_with_cancel = gleam@list:fold(
erlang:element(4, Registry),
Router_with_flows,
fun(R, Cancel_entry) ->
{Command, On_cancel} = Cancel_entry,
telega@router:on_command(
R,
Command,
fun(Ctx, _) ->
case cancel_user_flows(
Registry,
erlang:element(2, erlang:element(3, Ctx)),
erlang:element(3, erlang:element(3, Ctx))
) of
{ok, Cancelled} ->
On_cancel(Ctx, Cancelled);
{error, _} ->
{ok, Ctx}
end
end
)
end
),
case maps:size(erlang:element(3, Registry)) of
0 ->
Router_with_cancel;
_ ->
_pipe = Router_with_cancel,
_pipe@1 = telega@router:on_any_text(
_pipe,
auto_resume_handler(Registry)
),
_pipe@2 = telega@router:on_callback(
_pipe@1,
{prefix, <<""/utf8>>},
auto_resume_callback_handler(Registry)
),
_pipe@3 = telega@router:on_photo(
_pipe@2,
auto_resume_photo_handler(Registry)
),
_pipe@4 = telega@router:on_video(
_pipe@3,
auto_resume_video_handler(Registry)
),
_pipe@5 = telega@router:on_voice(
_pipe@4,
auto_resume_voice_handler(Registry)
),
_pipe@6 = telega@router:on_audio(
_pipe@5,
auto_resume_audio_handler(Registry)
),
_pipe@7 = telega@router:on_filtered(
_pipe@6,
telega@router:filter(
<<"has_location"/utf8>>,
fun(Upd) -> case Upd of
{message_update, _, _, Message, _} ->
gleam@option:is_some(
erlang:element(59, Message)
);
_ ->
false
end end
),
auto_resume_location_handler(Registry)
),
telega@router:on_filtered(
_pipe@7,
telega@router:filter(
<<"is_command"/utf8>>,
fun(Upd@1) -> case Upd@1 of
{command_update, _, _, _, _, _} ->
true;
_ ->
false
end end
),
auto_resume_command_handler(Registry)
)
end.
-file("src/telega/flow/registry.gleam", 228).
?DOC(" Create a router handler that starts a flow\n").
-spec to_handler(telega@flow@types:flow(any(), ARVO, ARVP)) -> fun((telega@bot:context(ARVO, ARVP), telega@update:command()) -> {ok,
telega@bot:context(ARVO, ARVP)} |
{error, ARVP}).
to_handler(Flow) ->
fun(Ctx, _) -> start(Flow, maps:new(), Ctx) end.