Current section
Files
Jump to
Current section
Files
src/telega@telega_wisp.erl
-module(telega@telega_wisp).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([is_bot_request/2, bot_handler/2]).
-spec is_bot_request(
telega:bot(),
gleam@http@request:request(wisp:connection())
) -> boolean().
is_bot_request(Bot, Req) ->
case fun gleam@http@request:path_segments/1(Req) of
[Segment] ->
case telega:is_webhook_path(Bot, Segment) of
true ->
true;
false ->
false
end;
_ ->
false
end.
-spec is_secret_token_valid(
telega:bot(),
gleam@http@request:request(wisp:connection())
) -> boolean().
is_secret_token_valid(Bot, Req) ->
Secret_header_value = begin
_pipe = gleam@http@request:get_header(
Req,
<<"x-relegram-bot-api-secret-token"/utf8>>
),
gleam@result:unwrap(_pipe, <<""/utf8>>)
end,
telega:is_secret_token_valid(Bot, Secret_header_value).
-spec bot_handler(telega:bot(), gleam@http@request:request(wisp:connection())) -> gleam@http@response:response(wisp:body()).
bot_handler(Bot, Req) ->
wisp:require_json(Req, fun(Json) -> case telega:decode_message(Json) of
{ok, Message} ->
gleam@bool:lazy_guard(
is_secret_token_valid(Bot, Req),
fun() -> {response, 401, [], empty} end,
fun() ->
logging:log(
info,
<<"Received message: "/utf8,
(erlang:element(2, Message))/binary>>
),
telega:handle_update(Bot, Message),
wisp:ok()
end
);
{error, _} ->
logging:log(error, <<"Failed to decode message"/utf8>>),
wisp:ok()
end end).