-module(telega@api). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([set_webhook/4, send_text/4]). -export_type([telegram_api_request/0]). -type telegram_api_request() :: {telegram_api_post_request, binary(), binary(), gleam@option:option(list({binary(), binary()}))} | {telegram_api_get_request, binary(), gleam@option:option(list({binary(), binary()}))}. -spec new_post_request( binary(), binary(), binary(), binary(), gleam@option:option(list({binary(), binary()})) ) -> telegram_api_request(). new_post_request(Token, Telegram_url, Path, Body, Query) -> Url = <<<<<>/binary, "/"/utf8>>/binary, Path/binary>>, {telegram_api_post_request, Url, Body, Query}. -spec new_get_request( binary(), binary(), binary(), gleam@option:option(list({binary(), binary()})) ) -> telegram_api_request(). new_get_request(Token, Telegram_url, Path, Query) -> Url = <<<<<>/binary, "/"/utf8>>/binary, Path/binary>>, {telegram_api_get_request, Url, Query}. -spec set_query( gleam@http@request:request(binary()), gleam@option:option(list({binary(), binary()})) ) -> gleam@http@request:request(binary()). set_query(Api_request, Query) -> case Query of none -> Api_request; {some, Query@1} -> gleam@http@request:set_query(Api_request, Query@1) end. -spec api_to_request(telegram_api_request()) -> {ok, gleam@http@request:request(binary())} | {error, binary()}. api_to_request(Api_request) -> _pipe@7 = case Api_request of {telegram_api_get_request, Url, Query} -> _pipe = gleam@http@request:to(Url), _pipe@1 = gleam@result:map( _pipe, fun(_capture) -> gleam@http@request:set_method(_capture, get) end ), _pipe@2 = gleam@result:map( _pipe@1, fun(_capture@1) -> set_query(_capture@1, Query) end ), gleam@result:map( _pipe@2, fun(_capture@2) -> gleam@http@request:set_header( _capture@2, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ) end ); {telegram_api_post_request, Url@1, Body, Query@1} -> _pipe@3 = gleam@http@request:to(Url@1), _pipe@4 = gleam@result:map( _pipe@3, fun(_capture@3) -> gleam@http@request:set_body(_capture@3, Body) end ), _pipe@5 = gleam@result:map( _pipe@4, fun(_capture@4) -> gleam@http@request:set_method(_capture@4, post) end ), _pipe@6 = gleam@result:map( _pipe@5, fun(_capture@5) -> gleam@http@request:set_header( _capture@5, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ) end ), gleam@result:map( _pipe@6, fun(_capture@6) -> set_query(_capture@6, Query@1) end ) end, gleam@result:map_error( _pipe@7, fun(_) -> <<"Failed to convert API request to HTTP request"/utf8>> end ). -spec fetch({ok, gleam@http@request:request(binary())} | {error, binary()}) -> {ok, gleam@http@response:response(binary())} | {error, binary()}. fetch(Api_request) -> gleam@result:'try'( Api_request, fun(Api_request@1) -> _pipe = gleam@httpc:send(Api_request@1), gleam@result:map_error( _pipe, fun(Error) -> _pipe@1 = gleam@dynamic:string(Error), gleam@result:unwrap( _pipe@1, <<"Failed to send request"/utf8>> ) end ) end ). -spec set_webhook(binary(), binary(), binary(), gleam@option:option(binary())) -> {ok, gleam@http@response:response(binary())} | {error, binary()}. set_webhook(Token, Webhook_url, Telegram_url, Secret_token) -> Query = [{<<"url"/utf8>>, Webhook_url}], Query@1 = case Secret_token of none -> Query; {some, Secret_token@1} -> [{<<"secret_token"/utf8>>, Secret_token@1} | Query] end, _pipe = new_get_request( Token, Telegram_url, <<"setWebhook"/utf8>>, {some, Query@1} ), _pipe@1 = api_to_request(_pipe), fetch(_pipe@1). -spec send_text(integer(), binary(), binary(), binary()) -> {ok, gleam@http@response:response(binary())} | {error, binary()}. send_text(Chat_id, Text, Token, Telegram_url) -> _pipe@1 = new_post_request( Token, Telegram_url, <<"sendMessage"/utf8>>, begin _pipe = gleam@json:object( [{<<"chat_id"/utf8>>, gleam@json:int(Chat_id)}, {<<"text"/utf8>>, gleam@json:string(Text)}] ), gleam@json:to_string(_pipe) end, none ), _pipe@2 = api_to_request(_pipe@1), fetch(_pipe@2).