Current section
Files
Jump to
Current section
Files
src/telega.erl
-module(telega).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([is_webhook_path/2, is_secret_token_valid/2, new/4, handle_all/2, wait_any/2, handle_command/3, wait_command/3, handle_commands/3, wait_commands/3, handle_text/2, wait_text/2, handle_hears/3, wait_hears/3, handle_callback_query/3, wait_callback_query/3, log_context/3, with_session_settings/3, init/1, init_nil_session/1, handle_update/2]).
-export_type([telega/1, telega_builder/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.
-opaque telega(ALRR) :: {telega,
telega@internal@config:config(),
telega@model:user(),
list(telega@bot:handler(ALRR)),
telega@bot:session_settings(ALRR),
gleam@erlang@process:subject(telega@bot:registry_message())}.
-opaque telega_builder(ALRS) :: {telega_builder,
telega@internal@config:config(),
list(telega@bot:handler(ALRS)),
gleam@option:option(telega@bot:session_settings(ALRS)),
gleam@option:option(gleam@erlang@process:subject(telega@bot:registry_message()))}.
-file("src/telega.gleam", 42).
?DOC(
" Check if a path is the webhook path for the bot.\n"
"\n"
" Useful if you plan to implement own adapter.\n"
).
-spec is_webhook_path(telega(any()), binary()) -> boolean().
is_webhook_path(Telega, Path) ->
erlang:element(3, erlang:element(2, Telega)) =:= Path.
-file("src/telega.gleam", 49).
?DOC(
" Check if a secret token is valid.\n"
"\n"
" Useful if you plan to implement own adapter.\n"
).
-spec is_secret_token_valid(telega(any()), binary()) -> boolean().
is_secret_token_valid(Telega, Token) ->
erlang:element(4, erlang:element(2, Telega)) =:= Token.
-file("src/telega.gleam", 54).
?DOC(" Create a new Telega instance.\n").
-spec new(binary(), binary(), binary(), gleam@option:option(binary())) -> telega_builder(any()).
new(Token, Server_url, Webhook_path, Secret_token) ->
{telega_builder,
telega@internal@config:new(
Token,
Server_url,
Webhook_path,
Secret_token
),
[],
none,
none}.
-file("src/telega.gleam", 69).
?DOC(" Handles all messages.\n").
-spec handle_all(
telega_builder(ALSA),
fun((telega@bot:context(ALSA)) -> {ok, ALSA} | {error, binary()})
) -> telega_builder(ALSA).
handle_all(Builder, Handler) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
[{handle_all, Handler} | erlang:element(3, Builder)],
erlang:element(4, _record),
erlang:element(5, _record)}.
-file("src/telega.gleam", 77).
?DOC(" Stops bot message handling and waits for any message.\n").
-spec wait_any(
telega@bot:context(ALSG),
fun((telega@bot:context(ALSG)) -> {ok, ALSG} | {error, binary()})
) -> {ok, ALSG} | {error, binary()}.
wait_any(Ctx, Continue) ->
telega@bot:wait_handler(Ctx, {handle_all, Continue}).
-file("src/telega.gleam", 85).
?DOC(" Handles a specific command.\n").
-spec handle_command(
telega_builder(ALSN),
binary(),
fun((telega@bot:context(ALSN), telega@update:command()) -> {ok, ALSN} |
{error, binary()})
) -> telega_builder(ALSN).
handle_command(Builder, Command, Handler) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
[{handle_command, Command, Handler} | erlang:element(3, Builder)],
erlang:element(4, _record),
erlang:element(5, _record)}.
-file("src/telega.gleam", 96).
-spec wait_command(
telega@bot:context(ALST),
binary(),
fun((telega@bot:context(ALST), telega@update:command()) -> {ok, ALST} |
{error, binary()})
) -> {ok, ALST} | {error, binary()}.
wait_command(Ctx, Command, Continue) ->
telega@bot:wait_handler(Ctx, {handle_command, Command, Continue}).
-file("src/telega.gleam", 105).
?DOC(" Handles multiple commands.\n").
-spec handle_commands(
telega_builder(ALTA),
list(binary()),
fun((telega@bot:context(ALTA), telega@update:command()) -> {ok, ALTA} |
{error, binary()})
) -> telega_builder(ALTA).
handle_commands(Builder, Commands, Handler) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
[{handle_commands, Commands, Handler} | erlang:element(3, Builder)],
erlang:element(4, _record),
erlang:element(5, _record)}.
-file("src/telega.gleam", 116).
-spec wait_commands(
telega@bot:context(ALTH),
list(binary()),
fun((telega@bot:context(ALTH), telega@update:command()) -> {ok, ALTH} |
{error, binary()})
) -> {ok, ALTH} | {error, binary()}.
wait_commands(Ctx, Commands, Continue) ->
telega@bot:wait_handler(Ctx, {handle_commands, Commands, Continue}).
-file("src/telega.gleam", 125).
?DOC(" Handles text messages.\n").
-spec handle_text(
telega_builder(ALTP),
fun((telega@bot:context(ALTP), binary()) -> {ok, ALTP} | {error, binary()})
) -> telega_builder(ALTP).
handle_text(Builder, Handler) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
[{handle_text, Handler} | erlang:element(3, Builder)],
erlang:element(4, _record),
erlang:element(5, _record)}.
-file("src/telega.gleam", 132).
-spec wait_text(
telega@bot:context(ALTV),
fun((telega@bot:context(ALTV), binary()) -> {ok, ALTV} | {error, binary()})
) -> {ok, ALTV} | {error, binary()}.
wait_text(Ctx, Continue) ->
telega@bot:wait_handler(Ctx, {handle_text, Continue}).
-file("src/telega.gleam", 140).
?DOC(" Handles messages that match the given `Hears`.\n").
-spec handle_hears(
telega_builder(ALUC),
telega@bot:hears(),
fun((telega@bot:context(ALUC), binary()) -> {ok, ALUC} | {error, binary()})
) -> telega_builder(ALUC).
handle_hears(Builder, Hears, Handler) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
[{handle_hears, Hears, Handler} | erlang:element(3, Builder)],
erlang:element(4, _record),
erlang:element(5, _record)}.
-file("src/telega.gleam", 151).
-spec wait_hears(
telega@bot:context(ALUI),
telega@bot:hears(),
fun((telega@bot:context(ALUI), binary()) -> {ok, ALUI} | {error, binary()})
) -> {ok, ALUI} | {error, binary()}.
wait_hears(Ctx, Hears, Continue) ->
telega@bot:wait_handler(Ctx, {handle_hears, Hears, Continue}).
-file("src/telega.gleam", 160).
?DOC(" Handles messages from inline keyboard callback.\n").
-spec handle_callback_query(
telega_builder(ALUO),
telega@bot:callback_query_filter(),
fun((telega@bot:context(ALUO), binary(), binary()) -> {ok, ALUO} |
{error, binary()})
) -> telega_builder(ALUO).
handle_callback_query(Builder, Filter, Handler) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
[{handle_callback_query, Filter, Handler} | erlang:element(3, Builder)],
erlang:element(4, _record),
erlang:element(5, _record)}.
-file("src/telega.gleam", 173).
?DOC(" Wait for a callback query and continue with the given function.\n").
-spec wait_callback_query(
telega@bot:context(ALUU),
telega@bot:callback_query_filter(),
fun((telega@bot:context(ALUU), binary(), binary()) -> {ok, ALUU} |
{error, binary()})
) -> {ok, ALUU} | {error, binary()}.
wait_callback_query(Ctx, Filter, Continue) ->
telega@bot:wait_handler(Ctx, {handle_callback_query, Filter, Continue}).
-file("src/telega.gleam", 183).
?DOC(" Log the message and error message if the handler fails.\n").
-spec log_context(
telega@bot:context(ALVB),
binary(),
fun(() -> {ok, ALVB} | {error, binary()})
) -> {ok, ALVB} | {error, binary()}.
log_context(Ctx, Prefix, Handler) ->
Prefix@1 = <<<<"["/utf8, Prefix/binary>>/binary, "] "/utf8>>,
telega@log:info(
<<<<Prefix@1/binary, "Received update: "/utf8>>/binary,
(gleam@string:inspect(erlang:element(3, Ctx)))/binary>>
),
_pipe = Handler(),
gleam@result:map_error(
_pipe,
fun(E) ->
telega@log:error(
<<<<Prefix@1/binary, "Handler failed: "/utf8>>/binary,
(gleam@string:inspect(E))/binary>>
),
E
end
).
-file("src/telega.gleam", 200).
?DOC(" Construct a session settings.\n").
-spec with_session_settings(
telega_builder(ALVH),
fun((binary(), ALVH) -> {ok, ALVH} | {error, binary()}),
fun((binary()) -> {ok, ALVH} | {error, binary()})
) -> telega_builder(ALVH).
with_session_settings(Builder, Persist_session, Get_session) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
erlang:element(3, _record),
{some, {session_settings, Persist_session, Get_session}},
erlang:element(5, _record)}.
-file("src/telega.gleam", 215).
-spec nil_session_settings(telega_builder(nil)) -> telega_builder(nil).
nil_session_settings(Builder) ->
_record = Builder,
{telega_builder,
erlang:element(2, _record),
erlang:element(3, _record),
{some,
{session_settings,
fun(_, _) -> {ok, nil} end,
fun(_) -> {ok, nil} end}},
erlang:element(5, _record)}.
-file("src/telega.gleam", 239).
?DOC(
" Initialize a Telega instance.\n"
" This function should be called after all handlers are added.\n"
" It will set the webhook and start the `Registry`.\n"
).
-spec init(telega_builder(ALVU)) -> {ok, telega(ALVU)} | {error, binary()}.
init(Builder) ->
gleam@result:'try'(
telega@bot:set_webhook(erlang:element(2, Builder)),
fun(Is_ok) ->
gleam@bool:guard(
not Is_ok,
{error, <<"Failed to set webhook"/utf8>>},
fun() ->
gleam@result:'try'(
telega@api:get_me(
erlang:element(5, erlang:element(2, Builder))
),
fun(Bot_info) ->
Session_settings = gleam@option:to_result(
erlang:element(4, Builder),
<<"Session settings not initialized"/utf8>>
),
gleam@result:'try'(
Session_settings,
fun(Session_settings@1) ->
Telega_subject = gleam@erlang@process:new_subject(
),
Registry_actor = gleam@otp@supervisor:supervisor(
fun(_) ->
telega@bot:start_registry(
erlang:element(2, Builder),
erlang:element(3, Builder),
Session_settings@1,
Telega_subject,
Bot_info
)
end
),
gleam@result:'try'(
begin
_pipe = gleam@otp@supervisor:start(
fun(_capture) ->
gleam@otp@supervisor:add(
_capture,
Registry_actor
)
end
),
gleam@result:map_error(
_pipe,
fun(E) ->
<<"Failed to start telega:\n"/utf8,
(gleam@string:inspect(E))/binary>>
end
)
end,
fun(_) ->
gleam@result:'try'(
begin
_pipe@1 = gleam_erlang_ffi:'receive'(
Telega_subject,
1000
),
gleam@result:map_error(
_pipe@1,
fun(E@1) ->
<<"Failed to start registry:\n"/utf8,
(gleam@string:inspect(
E@1
))/binary>>
end
)
end,
fun(Registry_subject) ->
{ok,
{telega,
erlang:element(
2,
Builder
),
Bot_info,
erlang:element(
3,
Builder
),
Session_settings@1,
Registry_subject}}
end
)
end
)
end
)
end
)
end
)
end
).
-file("src/telega.gleam", 228).
?DOC(
" Initialize a Telega instance with a `Nil` session.\n"
" Useful when you don't need to persist the session.\n"
).
-spec init_nil_session(telega_builder(nil)) -> {ok, telega(nil)} |
{error, binary()}.
init_nil_session(Builder) ->
_pipe = Builder,
_pipe@1 = nil_session_settings(_pipe),
init(_pipe@1).
-file("src/telega.gleam", 288).
?DOC(" Handle an update from the Telegram API.\n").
-spec handle_update(telega(any()), telega@update:update()) -> {ok, nil} |
{error, binary()}.
handle_update(Telega, Update) ->
{ok,
gleam@otp@actor:send(
erlang:element(6, Telega),
{handle_bot_registry_message, Update}
)}.