Current section

Files

Jump to
telega src telega.erl
Raw

src/telega.erl

-module(telega).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([set_webhook/1, is_webhook_path/2, is_secret_token_valid/2, reply/2, add_handler/2, handle_update/2, new/4]).
-export_type([config/0, bot/0, handler/0, context/0, command/0, command_context/0]).
-opaque config() :: {config,
binary(),
binary(),
binary(),
gleam@option:option(binary()),
binary()}.
-type bot() :: {bot, config(), list(handler())}.
-type handler() :: {handle_all, fun((context()) -> {ok, nil} | {error, nil})} |
{handle_command,
fun((command_context()) -> {ok, nil} | {error, nil}),
binary()} |
{handle_commands,
fun((command_context()) -> {ok, nil} | {error, nil}),
list(binary())} |
{handle_text, fun((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,
telega@message:message(),
bot(),
command()}.
-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>>,
gleam@result:'try'(
telega@api:set_webhook(
erlang:element(2, erlang:element(2, Bot)),
Webhook_url,
erlang:element(6, erlang:element(2, Bot)),
erlang:element(5, erlang:element(2, Bot))
),
fun(Response) -> case erlang:element(2, Response) of
200 ->
{ok, true};
_ ->
{error, <<"Failed to set webhook"/utf8>>}
end end
).
-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 reply(context(), binary()) -> {ok, telega@message:message()} |
{error, nil}.
reply(Ctx, Text) ->
Chat_id = erlang:element(
2,
erlang:element(8, erlang:element(3, erlang:element(2, Ctx)))
),
_pipe = telega@api:send_message(
Chat_id,
Text,
erlang:element(2, erlang:element(2, erlang:element(3, Ctx))),
erlang:element(6, erlang:element(2, erlang:element(3, Ctx)))
),
_pipe@1 = gleam@result:map(_pipe, fun(_) -> erlang:element(2, Ctx) end),
gleam@result:nil_error(_pipe@1).
-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 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({context, Message, Bot});
{{handle_command, Handle@2, Command}, command_message} ->
Message_command = extract_command(Message),
case erlang:element(3, Message_command) =:= Command of
true ->
Handle@2(
{command_context, Message, Bot, Message_command}
);
false ->
{ok, nil}
end;
{{handle_commands, Handle@3, Commands}, 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,
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)).
-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,
<<"https://api.telegram.org/bot"/utf8>>},
[]}.