Current section
Files
Jump to
Current section
Files
src/telega@flow@instance.erl
-module(telega@flow@instance).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/telega/flow/instance.gleam").
-export([new_instance/5, new_instance_with_data/6, instance_id/1, instance_flow_name/1, instance_user_id/1, instance_chat_id/1, instance_current_step/1, instance_wait_token/1, instance_created_at/1, instance_updated_at/1, store_step_data/3, get_step_data/2, clear_step_data/1, clear_step_data_key/2, store_data/3, get_data/2, get_wait_result/1, instance_to_row/1, instance_from_row/1, is_expired/2, next_with_data/5, get_current_step/2, encode_callback_wait_result/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(" Instance CRUD, accessors, factories, WaitResult, and serialization.\n").
-file("src/telega/flow/instance.gleam", 18).
?DOC(" Create a new FlowInstance with minimal required fields\n").
-spec new_instance(binary(), binary(), integer(), integer(), binary()) -> telega@flow@types:flow_instance().
new_instance(Id, Flow_name, User_id, Chat_id, Current_step) ->
{flow_instance,
Id,
Flow_name,
User_id,
Chat_id,
{flow_state, Current_step, maps:new(), [Current_step], [], none},
maps:new(),
none,
none,
telega@internal@utils:current_time_ms(),
telega@internal@utils:current_time_ms()}.
-file("src/telega/flow/instance.gleam", 46).
?DOC(" Create a new FlowInstance with initial data\n").
-spec new_instance_with_data(
binary(),
binary(),
integer(),
integer(),
binary(),
gleam@dict:dict(binary(), binary())
) -> telega@flow@types:flow_instance().
new_instance_with_data(Id, Flow_name, User_id, Chat_id, Current_step, Data) ->
{flow_instance,
Id,
Flow_name,
User_id,
Chat_id,
{flow_state, Current_step, Data, [Current_step], [], none},
maps:new(),
none,
none,
telega@internal@utils:current_time_ms(),
telega@internal@utils:current_time_ms()}.
-file("src/telega/flow/instance.gleam", 75).
?DOC(" Get the instance ID\n").
-spec instance_id(telega@flow@types:flow_instance()) -> binary().
instance_id(Instance) ->
erlang:element(2, Instance).
-file("src/telega/flow/instance.gleam", 80).
?DOC(" Get the flow name\n").
-spec instance_flow_name(telega@flow@types:flow_instance()) -> binary().
instance_flow_name(Instance) ->
erlang:element(3, Instance).
-file("src/telega/flow/instance.gleam", 85).
?DOC(" Get the user ID\n").
-spec instance_user_id(telega@flow@types:flow_instance()) -> integer().
instance_user_id(Instance) ->
erlang:element(4, Instance).
-file("src/telega/flow/instance.gleam", 90).
?DOC(" Get the chat ID\n").
-spec instance_chat_id(telega@flow@types:flow_instance()) -> integer().
instance_chat_id(Instance) ->
erlang:element(5, Instance).
-file("src/telega/flow/instance.gleam", 95).
?DOC(" Get the current step name\n").
-spec instance_current_step(telega@flow@types:flow_instance()) -> binary().
instance_current_step(Instance) ->
erlang:element(2, erlang:element(6, Instance)).
-file("src/telega/flow/instance.gleam", 100).
?DOC(" Get the wait token\n").
-spec instance_wait_token(telega@flow@types:flow_instance()) -> gleam@option:option(binary()).
instance_wait_token(Instance) ->
erlang:element(8, Instance).
-file("src/telega/flow/instance.gleam", 105).
?DOC(" Get the created_at timestamp\n").
-spec instance_created_at(telega@flow@types:flow_instance()) -> integer().
instance_created_at(Instance) ->
erlang:element(10, Instance).
-file("src/telega/flow/instance.gleam", 110).
?DOC(" Get the updated_at timestamp\n").
-spec instance_updated_at(telega@flow@types:flow_instance()) -> integer().
instance_updated_at(Instance) ->
erlang:element(11, Instance).
-file("src/telega/flow/instance.gleam", 115).
?DOC(" Store step data\n").
-spec store_step_data(telega@flow@types:flow_instance(), binary(), binary()) -> telega@flow@types:flow_instance().
store_step_data(Instance, Key, Value) ->
{flow_instance,
erlang:element(2, Instance),
erlang:element(3, Instance),
erlang:element(4, Instance),
erlang:element(5, Instance),
erlang:element(6, Instance),
gleam@dict:insert(erlang:element(7, Instance), Key, Value),
erlang:element(8, Instance),
erlang:element(9, Instance),
erlang:element(10, Instance),
erlang:element(11, Instance)}.
-file("src/telega/flow/instance.gleam", 127).
?DOC(" Get step data\n").
-spec get_step_data(telega@flow@types:flow_instance(), binary()) -> gleam@option:option(binary()).
get_step_data(Instance, Key) ->
_pipe = gleam_stdlib:map_get(erlang:element(7, Instance), Key),
gleam@option:from_result(_pipe).
-file("src/telega/flow/instance.gleam", 136).
?DOC(" Clear all step data\n").
-spec clear_step_data(telega@flow@types:flow_instance()) -> telega@flow@types:flow_instance().
clear_step_data(Instance) ->
{flow_instance,
erlang:element(2, Instance),
erlang:element(3, Instance),
erlang:element(4, Instance),
erlang:element(5, Instance),
erlang:element(6, Instance),
maps:new(),
erlang:element(8, Instance),
erlang:element(9, Instance),
erlang:element(10, Instance),
erlang:element(11, Instance)}.
-file("src/telega/flow/instance.gleam", 141).
?DOC(" Clear specific step data key\n").
-spec clear_step_data_key(telega@flow@types:flow_instance(), binary()) -> telega@flow@types:flow_instance().
clear_step_data_key(Instance, Key) ->
{flow_instance,
erlang:element(2, Instance),
erlang:element(3, Instance),
erlang:element(4, Instance),
erlang:element(5, Instance),
erlang:element(6, Instance),
gleam@dict:delete(erlang:element(7, Instance), Key),
erlang:element(8, Instance),
erlang:element(9, Instance),
erlang:element(10, Instance),
erlang:element(11, Instance)}.
-file("src/telega/flow/instance.gleam", 149).
?DOC(" Store data in the flow instance\n").
-spec store_data(telega@flow@types:flow_instance(), binary(), binary()) -> telega@flow@types:flow_instance().
store_data(Instance, Key, Value) ->
{flow_instance,
erlang:element(2, Instance),
erlang:element(3, Instance),
erlang:element(4, Instance),
erlang:element(5, Instance),
begin
_record = erlang:element(6, Instance),
{flow_state,
erlang:element(2, _record),
gleam@dict:insert(
erlang:element(3, erlang:element(6, Instance)),
Key,
Value
),
erlang:element(4, _record),
erlang:element(5, _record),
erlang:element(6, _record)}
end,
erlang:element(7, Instance),
erlang:element(8, Instance),
erlang:element(9, Instance),
erlang:element(10, Instance),
erlang:element(11, Instance)}.
-file("src/telega/flow/instance.gleam", 164).
?DOC(" Get data from the flow instance\n").
-spec get_data(telega@flow@types:flow_instance(), binary()) -> gleam@option:option(binary()).
get_data(Instance, Key) ->
_pipe = gleam_stdlib:map_get(
erlang:element(3, erlang:element(6, Instance)),
Key
),
_pipe@1 = gleam@result:map(_pipe, fun(Field@0) -> {some, Field@0} end),
gleam@result:unwrap(_pipe@1, none).
-file("src/telega/flow/instance.gleam", 289).
-spec parse_command_result(binary()) -> telega@flow@types:wait_result().
parse_command_result(Raw) ->
case gleam@string:split_once(Raw, <<":"/utf8>>) of
{ok, {Cmd, Payload}} ->
{command_input, Cmd, Payload};
{error, _} ->
{command_input, Raw, <<""/utf8>>}
end.
-file("src/telega/flow/instance.gleam", 278).
-spec parse_location_result(binary()) -> telega@flow@types:wait_result().
parse_location_result(Raw) ->
case gleam@string:split(Raw, <<","/utf8>>) of
[Lat_str, Lng_str] ->
case {gleam_stdlib:parse_float(Lat_str),
gleam_stdlib:parse_float(Lng_str)} of
{{ok, Lat}, {ok, Lng}} ->
{location_input, Lat, Lng};
{_, _} ->
{data_callback, <<"location:"/utf8, Raw/binary>>}
end;
_ ->
{data_callback, <<"location:"/utf8, Raw/binary>>}
end.
-file("src/telega/flow/instance.gleam", 262).
-spec parse_wait_result(binary()) -> telega@flow@types:wait_result().
parse_wait_result(Raw) ->
case Raw of
<<"bool:true"/utf8>> ->
{bool_callback, true};
<<"bool:false"/utf8>> ->
{bool_callback, false};
<<"photo:"/utf8, Rest/binary>> ->
{photo_input, gleam@string:split(Rest, <<","/utf8>>)};
<<"video:"/utf8, Rest@1/binary>> ->
{video_input, Rest@1};
<<"voice:"/utf8, Rest@2/binary>> ->
{voice_input, Rest@2};
<<"audio:"/utf8, Rest@3/binary>> ->
{audio_input, Rest@3};
<<"location:"/utf8, Rest@4/binary>> ->
parse_location_result(Rest@4);
<<"command:"/utf8, Rest@5/binary>> ->
parse_command_result(Rest@5);
<<"text:"/utf8, Rest@6/binary>> ->
{text_input, Rest@6};
<<"data:"/utf8, Rest@7/binary>> ->
{data_callback, Rest@7};
Other ->
{data_callback, Other}
end.
-file("src/telega/flow/instance.gleam", 171).
?DOC(" Get the result of waiting for user input or callback\n").
-spec get_wait_result(telega@flow@types:flow_instance()) -> telega@flow@types:wait_result().
get_wait_result(Instance) ->
case get_step_data(Instance, <<"__wait_result"/utf8>>) of
none ->
pending;
{some, Raw} ->
parse_wait_result(Raw)
end.
-file("src/telega/flow/instance.gleam", 179).
?DOC(" Convert a FlowInstance to a flat row for database storage\n").
-spec instance_to_row(telega@flow@types:flow_instance()) -> telega@flow@types:flow_instance_row().
instance_to_row(Instance) ->
{flow_instance_row,
erlang:element(2, Instance),
erlang:element(3, Instance),
erlang:element(4, Instance),
erlang:element(5, Instance),
erlang:element(2, erlang:element(6, Instance)),
erlang:element(3, erlang:element(6, Instance)),
erlang:element(7, Instance),
erlang:element(8, Instance),
erlang:element(9, Instance),
erlang:element(10, Instance),
erlang:element(11, Instance)}.
-file("src/telega/flow/instance.gleam", 196).
?DOC(" Construct a FlowInstance from a flat row\n").
-spec instance_from_row(telega@flow@types:flow_instance_row()) -> telega@flow@types:flow_instance().
instance_from_row(Row) ->
{flow_instance,
erlang:element(2, Row),
erlang:element(3, Row),
erlang:element(4, Row),
erlang:element(5, Row),
{flow_state,
erlang:element(6, Row),
erlang:element(7, Row),
[erlang:element(6, Row)],
[],
none},
erlang:element(8, Row),
erlang:element(9, Row),
erlang:element(10, Row),
erlang:element(11, Row),
erlang:element(12, Row)}.
-file("src/telega/flow/instance.gleam", 218).
?DOC(" Check if an instance is expired based on TTL or wait timeout\n").
-spec is_expired(
telega@flow@types:flow_instance(),
gleam@option:option(integer())
) -> boolean().
is_expired(Instance, Ttl_ms) ->
Now = telega@internal@utils:current_time_ms(),
Ttl_expired = case Ttl_ms of
{some, Ttl} ->
(Now - erlang:element(10, Instance)) > Ttl;
none ->
false
end,
Wait_expired = case erlang:element(9, Instance) of
{some, Timeout_at} ->
Now > Timeout_at;
none ->
false
end,
Ttl_expired orelse Wait_expired.
-file("src/telega/flow/instance.gleam", 232).
?DOC(" Update instance data and continue to next step\n").
-spec next_with_data(
telega@bot:context(AOHN, AOHO),
telega@flow@types:flow_instance(),
AOHR,
binary(),
binary()
) -> {ok,
{telega@bot:context(AOHN, AOHO),
telega@flow@types:flow_action(AOHR),
telega@flow@types:flow_instance()}} |
{error, AOHO}.
next_with_data(Ctx, Instance, Step, Key, Value) ->
Updated_instance = store_data(Instance, Key, Value),
{ok, {Ctx, {next, Step}, Updated_instance}}.
-file("src/telega/flow/instance.gleam", 244).
?DOC(" Get current step as typed value\n").
-spec get_current_step(
telega@flow@types:flow(AOHV, any(), any()),
telega@flow@types:flow_instance()
) -> {ok, AOHV} | {error, nil}.
get_current_step(Flow, Instance) ->
(erlang:element(6, Flow))(erlang:element(2, erlang:element(6, Instance))).
-file("src/telega/flow/instance.gleam", 253).
?DOC(false).
-spec encode_callback_wait_result(binary()) -> binary().
encode_callback_wait_result(Data) ->
case gleam@string:split(Data, <<":"/utf8>>) of
[_, <<"true"/utf8>>] ->
<<"bool:true"/utf8>>;
[_, <<"false"/utf8>>] ->
<<"bool:false"/utf8>>;
_ ->
<<"data:"/utf8, Data/binary>>
end.