-module(telega@bot). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -define(FILEPATH, "src/telega/bot.gleam"). -export([cancel_conversation/2, next_session/2, get_session/2, handle_update/2, wait_handler/4, start/6]). -export_type([bot/2, bot_message/0, chat_instance_message/2, continuation/2, chat_instance/2, context/2, session_settings/2, callback_query_filter/0, hears/0, handler/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. -opaque bot(AKMD, AKME) :: {bot, gleam@erlang@process:subject(bot_message()), telega@internal@config:config(), telega@model:user(), fun((context(AKMD, AKME), AKME) -> {ok, nil} | {error, AKME}), session_settings(AKMD, AKME), list(handler(AKMD, AKME)), gleam@erlang@process:subject(telega@internal@registry:registry_message(chat_instance_message(AKMD, AKME)))}. -opaque bot_message() :: {cancel_conversation_bot_message, binary()} | {handle_update_bot_message, telega@update:update(), gleam@erlang@process:subject(boolean())}. -opaque chat_instance_message(AKMF, AKMG) :: {handle_new_chat_instance_message, telega@update:update(), list(handler(AKMF, AKMG)), gleam@erlang@process:subject(boolean())} | {wait_handler_chat_instance_message, handler(AKMF, AKMG), gleam@option:option(handler(AKMF, AKMG)), gleam@option:option(integer())}. -type continuation(AKMH, AKMI) :: {continuation, handler(AKMH, AKMI), gleam@option:option(handler(AKMH, AKMI)), gleam@option:option(gleam@time@timestamp:timestamp())}. -type chat_instance(AKMJ, AKMK) :: {chat_instance, binary(), AKMJ, telega@internal@config:config(), fun((context(AKMJ, AKMK), AKMK) -> {ok, nil} | {error, AKMK}), session_settings(AKMJ, AKMK), gleam@erlang@process:subject(chat_instance_message(AKMJ, AKMK)), gleam@option:option(continuation(AKMJ, AKMK))}. -type context(AKML, AKMM) :: {context, binary(), telega@update:update(), telega@internal@config:config(), AKML, gleam@erlang@process:subject(chat_instance_message(AKML, AKMM))}. -type session_settings(AKMN, AKMO) :: {session_settings, fun((binary(), AKMN) -> {ok, AKMN} | {error, AKMO}), fun((binary()) -> {ok, gleam@option:option(AKMN)} | {error, AKMO}), fun(() -> AKMN)}. -type callback_query_filter() :: {callback_query_filter, gleam@regexp:regexp()}. -type hears() :: {hear_text, binary()} | {hear_texts, list(binary())} | {hear_regex, gleam@regexp:regexp()} | {hear_regexes, list(gleam@regexp:regexp())}. -type handler(AKMP, AKMQ) :: {handle_all, fun((context(AKMP, AKMQ), telega@update:update()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_command, binary(), fun((context(AKMP, AKMQ), telega@update:command()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_commands, list(binary()), fun((context(AKMP, AKMQ), telega@update:command()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_text, fun((context(AKMP, AKMQ), binary()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_hears, hears(), fun((context(AKMP, AKMQ), binary()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_message, fun((context(AKMP, AKMQ), telega@model:message()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_voice, fun((context(AKMP, AKMQ), telega@model:voice()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_audio, fun((context(AKMP, AKMQ), telega@model:audio()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_video, fun((context(AKMP, AKMQ), telega@model:video()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_photos, fun((context(AKMP, AKMQ), list(telega@model:photo_size())) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_web_app_data, fun((context(AKMP, AKMQ), telega@model:web_app_data()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_callback_query, callback_query_filter(), fun((context(AKMP, AKMQ), binary(), binary()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})} | {handle_chat_member, fun((context(AKMP, AKMQ), telega@model:chat_member_updated()) -> {ok, context(AKMP, AKMQ)} | {error, AKMQ})}. -file("src/telega/bot.gleam", 84). ?DOC(" Stops waiting for any handler for specific key (chat_id)\n"). -spec cancel_conversation(bot(any(), any()), binary()) -> nil. cancel_conversation(Bot, Key) -> gleam@otp@actor:send( erlang:element(2, Bot), {cancel_conversation_bot_message, Key} ). -file("src/telega/bot.gleam", 425). -spec new_context(chat_instance(AKPO, AKPP), telega@update:update()) -> context(AKPO, AKPP). new_context(Chat, Update) -> {context, erlang:element(2, Chat), Update, erlang:element(4, Chat), erlang:element(3, Chat), erlang:element(7, Chat)}. -file("src/telega/bot.gleam", 453). -spec next_session(context(AKPV, AKPW), AKPV) -> {ok, context(AKPV, AKPW)} | {error, AKPW}. next_session(Ctx, Session) -> {ok, begin _record = Ctx, {context, erlang:element(2, _record), erlang:element(3, _record), erlang:element(4, _record), Session, erlang:element(6, _record)} end}. -file("src/telega/bot.gleam", 460). -spec build_session_key(telega@update:update()) -> binary(). build_session_key(Update) -> <<<<(erlang:integer_to_binary(erlang:element(3, Update)))/binary, ":"/utf8>>/binary, (erlang:integer_to_binary(erlang:element(2, Update)))/binary>>. -file("src/telega/bot.gleam", 464). -spec get_session(session_settings(AKQE, AKQF), telega@update:update()) -> {ok, gleam@option:option(AKQE)} | {error, AKQF}. get_session(Session_settings, Update) -> _pipe = Update, _pipe@1 = build_session_key(_pipe), (erlang:element(3, Session_settings))(_pipe@1). -file("src/telega/bot.gleam", 475). ?DOC(false). -spec handle_update( gleam@erlang@process:subject(bot_message()), telega@update:update() ) -> boolean(). handle_update(Bot_subject, Update) -> gleam@erlang@process:call_forever( Bot_subject, fun(_capture) -> {handle_update_bot_message, Update, _capture} end ). -file("src/telega/bot.gleam", 492). -spec check_hears(binary(), hears()) -> boolean(). check_hears(Text, Hear) -> case Hear of {hear_text, Str} -> Text =:= Str; {hear_texts, Strings} -> gleam@list:contains(Strings, Text); {hear_regex, Re} -> gleam@regexp:check(Re, Text); {hear_regexes, Regexes} -> gleam@list:any( Regexes, fun(_capture) -> gleam@regexp:check(_capture, Text) end ) end. -file("src/telega/bot.gleam", 575). -spec extract_update_handlers(list(handler(AKYM, AKYN)), telega@update:update()) -> list(handler(AKYM, AKYN)). extract_update_handlers(Handlers, Update) -> gleam@list:filter(Handlers, fun(Handler) -> case {Handler, Update} of {{handle_all, _}, _} -> true; {{handle_text, _}, {text_update, _, _, _, _, _}} -> true; {{handle_hears, Hears, _}, {text_update, _, _, Text, _, _}} -> check_hears(Text, Hears); {{handle_command, Command, _}, {command_update, _, _, Update_command, _, _}} -> erlang:element(3, Update_command) =:= Command; {{handle_callback_query, Filter, _}, {callback_query_update, _, _, Query, _}} -> case erlang:element(7, Query) of {some, Data} -> gleam@regexp:check(erlang:element(2, Filter), Data); none -> false end; {_, _} -> false end end). -file("src/telega/bot.gleam", 605). ?DOC( " Pass any handler to start waiting\n" "\n" " `or` - calls if there are any other updates\n" " `timeout` - the conversation will be canceled after this timeout\n" ). -spec wait_handler( context(AKQT, AKQU), handler(AKQT, AKQU), gleam@option:option(handler(AKQT, AKQU)), gleam@option:option(integer()) ) -> {ok, context(AKQT, AKQU)} | {error, AKQU}. wait_handler(Ctx, Handler, Handle_else, Timeout) -> gleam@otp@actor:send( erlang:element(6, Ctx), {wait_handler_chat_instance_message, Handler, Handle_else, Timeout} ), {ok, Ctx}. -file("src/telega/bot.gleam", 618). -spec do_handle( context(ALAS, ALAT), telega@update:update(), handler(ALAS, ALAT) ) -> gleam@option:option({ok, context(ALAS, ALAT)} | {error, ALAT}). do_handle(Context, Update, Handler) -> case {Handler, Update} of {{handle_all, Handler@1}, _} -> _pipe = Context, _pipe@1 = Handler@1(_pipe, Update), {some, _pipe@1}; {{handle_text, Handler@2}, {text_update, _, _, Text, _, _}} -> _pipe@2 = Context, _pipe@3 = Handler@2(_pipe@2, Text), {some, _pipe@3}; {{handle_hears, _, Handler@3}, {text_update, _, _, Text@1, _, _}} -> _pipe@4 = Context, _pipe@5 = Handler@3(_pipe@4, Text@1), {some, _pipe@5}; {{handle_command, _, Handler@4}, {command_update, _, _, Update_command, _, _}} -> _pipe@6 = Context, _pipe@7 = Handler@4(_pipe@6, Update_command), {some, _pipe@7}; {{handle_commands, _, Handler@5}, {command_update, _, _, Update_command@1, _, _}} -> _pipe@8 = Context, _pipe@9 = Handler@5(_pipe@8, Update_command@1), {some, _pipe@9}; {{handle_callback_query, _, Handler@6}, {callback_query_update, _, _, Query, _}} -> gleam@option:map( erlang:element(7, Query), fun(Data) -> Handler@6(Context, Data, erlang:element(2, Query)) end ); {{handle_message, Handler@7}, {message_update, _, _, Message, _}} -> _pipe@10 = Context, _pipe@11 = Handler@7(_pipe@10, Message), {some, _pipe@11}; {{handle_chat_member, Handler@8}, {chat_member_update, _, _, Chat_member_updated, _}} -> _pipe@12 = Context, _pipe@13 = Handler@8(_pipe@12, Chat_member_updated), {some, _pipe@13}; {{handle_voice, Handler@9}, {voice_update, _, _, Voice, _, _}} -> _pipe@14 = Context, _pipe@15 = Handler@9(_pipe@14, Voice), {some, _pipe@15}; {{handle_audio, Handler@10}, {audio_update, _, _, Audio, _, _}} -> _pipe@16 = Context, _pipe@17 = Handler@10(_pipe@16, Audio), {some, _pipe@17}; {{handle_video, Handler@11}, {video_update, _, _, Video, _, _}} -> _pipe@18 = Context, _pipe@19 = Handler@11(_pipe@18, Video), {some, _pipe@19}; {{handle_web_app_data, Handler@12}, {web_app_update, _, _, Web_app_data, _, _}} -> _pipe@20 = Context, _pipe@21 = Handler@12(_pipe@20, Web_app_data), {some, _pipe@21}; {_, _} -> none end. -file("src/telega/bot.gleam", 308). -spec do_handle_continuation( context(AKPE, AKPF), continuation(AKPE, AKPF), telega@update:update(), gleam@erlang@process:subject(boolean()), chat_instance(AKPE, AKPF) ) -> gleam@otp@actor:next(chat_instance(AKPE, AKPF), any()). do_handle_continuation(Context, Continuation, Update, Reply_with, Chat) -> case do_handle(Context, Update, erlang:element(2, Continuation)) of {some, {ok, {context, _, _, _, New_session, _}}} -> case (erlang:element(2, erlang:element(6, Chat)))( erlang:element(2, Chat), New_session ) of {ok, Persisted_session} -> gleam@otp@actor:send(Reply_with, true), gleam@otp@actor:continue( begin _record = Chat, {chat_instance, erlang:element(2, _record), Persisted_session, erlang:element(4, _record), erlang:element(5, _record), erlang:element(6, _record), erlang:element(7, _record), none} end ); {error, E} -> case (erlang:element(5, Chat))(Context, E) of {ok, _} -> gleam@otp@actor:send(Reply_with, false), gleam@otp@actor:continue(Chat); {error, E@1} -> telega@internal@log:error_d( <<"Error in session persistence after continuation: "/utf8>>, E@1 ), gleam@otp@actor:stop() end end; {some, {error, E@2}} -> case (erlang:element(5, Chat))(Context, E@2) of {ok, _} -> gleam@otp@actor:send(Reply_with, false), gleam@otp@actor:continue(Chat); {error, E@3} -> telega@internal@log:error_d( <<"Error in catch handler: "/utf8>>, E@3 ), gleam@otp@actor:stop() end; none -> case erlang:element(3, Continuation) of {some, Handler} -> case do_handle(Context, Update, Handler) of {some, {ok, {context, _, _, _, New_session@1, _}}} -> case (erlang:element(2, erlang:element(6, Chat)))( erlang:element(2, Chat), New_session@1 ) of {ok, Persisted_session@1} -> gleam@otp@actor:send(Reply_with, true), gleam@otp@actor:continue( begin _record@1 = Chat, {chat_instance, erlang:element(2, _record@1), Persisted_session@1, erlang:element(4, _record@1), erlang:element(5, _record@1), erlang:element(6, _record@1), erlang:element(7, _record@1), erlang:element(8, _record@1)} end ); {error, E@4} -> case (erlang:element(5, Chat))(Context, E@4) of {ok, _} -> gleam@otp@actor:send( Reply_with, false ), gleam@otp@actor:continue(Chat); {error, E@5} -> telega@internal@log:error_d( <<"Error in session persistence after handle_else: "/utf8>>, E@5 ), gleam@otp@actor:stop() end end; {some, {error, E@6}} -> case (erlang:element(5, Chat))(Context, E@6) of {ok, _} -> gleam@otp@actor:send(Reply_with, false), gleam@otp@actor:continue(Chat); {error, E@7} -> telega@internal@log:error_d( <<"Error in catch else handler: "/utf8>>, E@7 ), gleam@otp@actor:stop() end; none -> gleam@otp@actor:send(Reply_with, false), gleam@otp@actor:continue(Chat) end; none -> gleam@otp@actor:send(Reply_with, false), gleam@otp@actor:continue(Chat) end end. -file("src/telega/bot.gleam", 650). -spec loop_handlers( chat_instance(AKRL, AKRM), context(AKRL, AKRM), any(), telega@update:update(), list(handler(AKRL, AKRM)) ) -> {ok, AKRL} | {error, AKRM}. loop_handlers(Chat, Context, Config, Update, Handlers) -> case Handlers of [Handler | Rest] -> case do_handle(Context, Update, Handler) of {some, {ok, {context, _, _, _, New_session, _}}} -> loop_handlers( Chat, begin _record = Context, {context, erlang:element(2, _record), erlang:element(3, _record), erlang:element(4, _record), New_session, erlang:element(6, _record)} end, Config, Update, Rest ); {some, {error, E}} -> {error, E}; none -> loop_handlers(Chat, Context, Config, Update, Rest) end; [] -> (erlang:element(2, erlang:element(6, Chat)))( erlang:element(2, Chat), erlang:element(5, Context) ) end. -file("src/telega/bot.gleam", 243). -spec do_handle_new_chat_instance_message( context(AKOU, AKOV), chat_instance(AKOU, AKOV), telega@update:update(), list(handler(AKOU, AKOV)), gleam@erlang@process:subject(boolean()) ) -> gleam@otp@actor:next(chat_instance(AKOU, AKOV), any()). do_handle_new_chat_instance_message(Context, Chat, Update, Handlers, Reply_with) -> case erlang:element(8, Chat) of {some, Continuation} -> case erlang:element(4, Continuation) of {some, Ttl} -> case gleam@time@timestamp:compare( Ttl, gleam@time@timestamp:system_time() ) of lt -> do_handle_new_chat_instance_message( Context, begin _record = Chat, {chat_instance, erlang:element(2, _record), erlang:element(3, _record), erlang:element(4, _record), erlang:element(5, _record), erlang:element(6, _record), erlang:element(7, _record), none} end, Update, Handlers, Reply_with ); _ -> do_handle_continuation( Context, Continuation, Update, Reply_with, Chat ) end; none -> do_handle_continuation( Context, Continuation, Update, Reply_with, Chat ) end; none -> case loop_handlers( Chat, Context, erlang:element(4, Chat), Update, Handlers ) of {ok, New_session} -> gleam@otp@actor:send(Reply_with, true), gleam@otp@actor:continue( begin _record@1 = Chat, {chat_instance, erlang:element(2, _record@1), New_session, erlang:element(4, _record@1), erlang:element(5, _record@1), erlang:element(6, _record@1), erlang:element(7, _record@1), erlang:element(8, _record@1)} end ); {error, E} -> case (erlang:element(5, Chat))(Context, E) of {ok, _} -> gleam@otp@actor:send(Reply_with, false), gleam@otp@actor:continue(Chat); {error, E@1} -> telega@internal@log:error_d( <<"Error in catch handler: "/utf8>>, E@1 ), gleam@otp@actor:stop() end end end. -file("src/telega/bot.gleam", 219). -spec loop_chat_instance( chat_instance(AKOO, AKOP), chat_instance_message(AKOO, AKOP) ) -> gleam@otp@actor:next(chat_instance(AKOO, AKOP), any()). loop_chat_instance(Chat, Message) -> case Message of {handle_new_chat_instance_message, Update, Handlers, Reply_with} -> do_handle_new_chat_instance_message( new_context(Chat, Update), Chat, Update, Handlers, Reply_with ); {wait_handler_chat_instance_message, Handler, Handle_else, Timeout} -> _pipe@2 = begin _record = Chat, {chat_instance, erlang:element(2, _record), erlang:element(3, _record), erlang:element(4, _record), erlang:element(5, _record), erlang:element(6, _record), erlang:element(7, _record), begin _pipe@1 = {continuation, Handler, Handle_else, begin gleam@option:map( Timeout, fun(Timeout@1) -> _pipe = gleam@time@timestamp:system_time( ), gleam@time@timestamp:add( _pipe, gleam@time@duration:seconds( Timeout@1 ) ) end ) end}, {some, _pipe@1} end} end, gleam@otp@actor:continue(_pipe@2) end. -file("src/telega/bot.gleam", 182). -spec start_chat_instance( binary(), telega@internal@config:config(), session_settings(AKOH, AKOI), fun((context(AKOH, AKOI), AKOI) -> {ok, nil} | {error, AKOI}) ) -> {ok, gleam@erlang@process:subject(chat_instance_message(AKOH, AKOI))} | {error, telega@error:telega_error()}. start_chat_instance(Key, Config, Session_settings, Catch_handler) -> Session@1 = case (erlang:element(3, Session_settings))(Key) of {ok, {some, Session}} -> Session; {ok, none} -> (erlang:element(4, Session_settings))(); {error, Error} -> erlang:error(#{gleam_error => panic, message => (<<"Failed to get session: "/utf8, (gleam@string:inspect(Error))/binary>>), file => <>, module => <<"telega/bot"/utf8>>, function => <<"start_chat_instance"/utf8>>, line => 192}) end, gleam@result:'try'( begin _pipe@2 = gleam@otp@actor:new_with_initialiser( 100, fun(Subject) -> Chat_instance = {chat_instance, Key, Session@1, Config, Catch_handler, Session_settings, Subject, none}, _pipe = gleam@otp@actor:initialised(Chat_instance), _pipe@1 = gleam@otp@actor:returning(_pipe, Subject), {ok, _pipe@1} end ), _pipe@3 = gleam@otp@actor:on_message( _pipe@2, fun loop_chat_instance/2 ), _pipe@4 = gleam@otp@actor:start(_pipe@3), gleam@result:map_error( _pipe@4, fun(Field@0) -> {chat_instance_start_error, Field@0} end ) end, fun(Started) -> {ok, erlang:element(3, Started)} end ). -file("src/telega/bot.gleam", 106). -spec handle_update_bot_message( bot(any(), any()), telega@update:update(), gleam@erlang@process:subject(boolean()) ) -> {ok, nil} | {error, telega@error:telega_error()}. handle_update_bot_message(Bot, Update, Reply_with) -> Key = build_session_key(Update), case telega@internal@registry:get(erlang:element(8, Bot), Key) of {some, Chat_subject} -> Handlers = extract_update_handlers(erlang:element(7, Bot), Update), _pipe = gleam@otp@actor:send( Chat_subject, {handle_new_chat_instance_message, Update, Handlers, Reply_with} ), {ok, _pipe}; none -> gleam@result:'try'( start_chat_instance( Key, erlang:element(3, Bot), erlang:element(6, Bot), erlang:element(5, Bot) ), fun(Subject) -> Chat_subject@1 = telega@internal@registry:register( erlang:element(8, Bot), Key, Subject ), Handlers@1 = extract_update_handlers( erlang:element(7, Bot), Update ), _pipe@1 = gleam@otp@actor:send( Chat_subject@1, {handle_new_chat_instance_message, Update, Handlers@1, Reply_with} ), {ok, _pipe@1} end ) end. -file("src/telega/bot.gleam", 88). -spec bot_loop(bot(ALPX, ALPY), bot_message()) -> gleam@otp@actor:next(bot(ALPX, ALPY), any()). bot_loop(Bot, Message) -> case Message of {handle_update_bot_message, Update, Reply_with} -> case handle_update_bot_message(Bot, Update, Reply_with) of {ok, _} -> gleam@otp@actor:continue(Bot); {error, Error} -> telega@internal@log:error_d( <<"Error in handler: "/utf8>>, Error ), gleam@otp@actor:stop() end; {cancel_conversation_bot_message, Key} -> telega@internal@registry:unregister(erlang:element(8, Bot), Key), gleam@otp@actor:continue(Bot) end. -file("src/telega/bot.gleam", 54). -spec start( gleam@erlang@process:subject(telega@internal@registry:registry_message(chat_instance_message(AKND, AKNE))), telega@internal@config:config(), telega@model:user(), list(handler(AKND, AKNE)), session_settings(AKND, AKNE), fun((context(AKND, AKNE), AKNE) -> {ok, nil} | {error, AKNE}) ) -> {ok, gleam@erlang@process:subject(bot_message())} | {error, telega@error:telega_error()}. start( Registry_subject, Config, Bot_info, Handlers, Session_settings, Catch_handler ) -> Self = gleam@erlang@process:new_subject(), Bot = {bot, Self, Config, Bot_info, Catch_handler, Session_settings, Handlers, Registry_subject}, _pipe = gleam@otp@actor:new(Bot), _pipe@1 = gleam@otp@actor:on_message(_pipe, fun bot_loop/2), _pipe@2 = gleam@otp@actor:start(_pipe@1), _pipe@3 = gleam@result:map( _pipe@2, fun(Started) -> erlang:element(3, Started) end ), gleam@result:map_error( _pipe@3, fun(Err) -> {bot_start_error, gleam@string:inspect(Err)} end ).