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([new/4, is_webhook_path/2, is_secret_token_valid/2, set_webhook/1, reply/2, set_my_commands/3, get_my_commands/2, delete_my_commands/2, send_dice/2, add_handler/2, handle_update/2]).
-export_type([config/0, bot/0, handler/0, context/0, command/0, command_context/0, text_context/0]).
-opaque config() :: {config,
binary(),
binary(),
binary(),
gleam@option:option(binary())}.
-type bot() :: {bot, config(), list(handler())}.
-type handler() :: {handle_all, fun((context()) -> {ok, nil} | {error, nil})} |
{handle_command,
binary(),
fun((command_context()) -> {ok, nil} | {error, nil})} |
{handle_commands,
list(binary()),
fun((command_context()) -> {ok, nil} | {error, nil})} |
{handle_text, fun((text_context()) -> {ok, nil} | {error, nil})}.
-type context() :: {context, telega@message:message(), bot()}.
-type command() :: {command, binary(), binary(), gleam@option:option(binary())}.
-type command_context() :: {command_context, context(), command()}.
-type text_context() :: {text_context, context(), binary()}.
-spec new(binary(), binary(), binary(), gleam@option:option(binary())) -> bot().
new(Token, Server_url, Webhook_path, Secret_token) ->
{bot, {config, Token, Server_url, Webhook_path, Secret_token}, []}.
-spec is_webhook_path(bot(), binary()) -> boolean().
is_webhook_path(Bot, Path) ->
erlang:element(4, erlang:element(2, Bot)) =:= Path.
-spec is_secret_token_valid(bot(), binary()) -> boolean().
is_secret_token_valid(Bot, Token) ->
case erlang:element(5, erlang:element(2, Bot)) of
{some, Secret} ->
Secret =:= Token;
none ->
true
end.
-spec set_webhook(bot()) -> {ok, boolean()} | {error, binary()}.
set_webhook(Bot) ->
Webhook_url = <<<<(erlang:element(3, erlang:element(2, Bot)))/binary,
"/"/utf8>>/binary,
(erlang:element(4, erlang:element(2, Bot)))/binary>>,
telega@api:set_webhook(
erlang:element(2, erlang:element(2, Bot)),
Webhook_url,
erlang:element(5, erlang:element(2, Bot))
).
-spec reply(context(), binary()) -> {ok, telega@models@message:message()} |
{error, binary()}.
reply(Ctx, Text) ->
telega@api:send_message(
erlang:element(2, erlang:element(2, erlang:element(3, Ctx))),
erlang:element(
2,
erlang:element(8, erlang:element(3, erlang:element(2, Ctx)))
),
Text
).
-spec set_my_commands(
context(),
list(telega@models@bot_command:bot_command()),
gleam@option:option(telega@models@bot_command:bot_command_parameters())
) -> {ok, boolean()} | {error, binary()}.
set_my_commands(Ctx, Commands, Parameters) ->
telega@api:set_my_commands(
erlang:element(2, erlang:element(2, erlang:element(3, Ctx))),
Commands,
Parameters
).
-spec get_my_commands(
context(),
gleam@option:option(telega@models@bot_command:bot_command_parameters())
) -> {ok, list(telega@models@bot_command:bot_command())} | {error, binary()}.
get_my_commands(Ctx, Parameters) ->
telega@api:get_my_commands(
erlang:element(2, erlang:element(2, erlang:element(3, Ctx))),
Parameters
).
-spec delete_my_commands(
context(),
gleam@option:option(telega@models@bot_command:bot_command_parameters())
) -> {ok, boolean()} | {error, binary()}.
delete_my_commands(Ctx, Parameters) ->
telega@api:delete_my_commands(
erlang:element(2, erlang:element(2, erlang:element(3, Ctx))),
Parameters
).
-spec send_dice(
context(),
gleam@option:option(telega@models@dice:send_dice_parameters())
) -> {ok, telega@models@message:message()} | {error, binary()}.
send_dice(Ctx, Parameters) ->
telega@api:send_dice(
erlang:element(2, erlang:element(2, erlang:element(3, Ctx))),
erlang:element(
2,
erlang:element(8, erlang:element(3, erlang:element(2, Ctx)))
),
Parameters
).
-spec add_handler(bot(), handler()) -> bot().
add_handler(Bot, Handler) ->
erlang:setelement(3, Bot, [Handler | erlang:element(3, Bot)]).
-spec extract_command(telega@message:message()) -> command().
extract_command(Message) ->
case erlang:element(17, erlang:element(3, Message)) of
none ->
{command, <<""/utf8>>, <<""/utf8>>, none};
{some, Text} ->
case gleam@string:split(Text, <<" "/utf8>>) of
[Command | Payload] ->
{command, Text, Command, case Payload of
[] ->
none;
[Payload@1 | _] ->
{some, Payload@1}
end};
_ ->
{command, Text, <<""/utf8>>, none}
end
end.
-spec extract_text(telega@message:message()) -> binary().
extract_text(Message) ->
gleam@option:unwrap(
erlang:element(17, erlang:element(3, Message)),
<<""/utf8>>
).
-spec do_handle_update(bot(), telega@message:message(), list(handler())) -> nil.
do_handle_update(Bot, Message, Handlers) ->
case Handlers of
[Handler | Rest] ->
Handle_result = case {Handler, erlang:element(2, Message)} of
{{handle_all, Handle}, _} ->
Handle({context, Message, Bot});
{{handle_text, Handle@1}, text_message} ->
Handle@1(
{text_context,
{context, Message, Bot},
extract_text(Message)}
);
{{handle_command, Command, Handle@2}, command_message} ->
Message_command = extract_command(Message),
case erlang:element(3, Message_command) =:= Command of
true ->
Handle@2(
{command_context,
{context, Message, Bot},
Message_command}
);
false ->
{ok, nil}
end;
{{handle_commands, Commands, Handle@3}, command_message} ->
Message_command@1 = extract_command(Message),
case gleam@list:contains(
Commands,
erlang:element(3, Message_command@1)
) of
true ->
Handle@3(
{command_context,
{context, Message, Bot},
Message_command@1}
);
false ->
{ok, nil}
end;
{_, _} ->
{ok, nil}
end,
case Handle_result of
{ok, _} ->
do_handle_update(Bot, Message, Rest);
{error, _} ->
telega@log:error(<<"Failed to handle message"/utf8>>),
nil
end;
_ ->
nil
end.
-spec handle_update(bot(), telega@message:message()) -> nil.
handle_update(Bot, Message) ->
do_handle_update(Bot, Message, erlang:element(3, Bot)).