-module(telega@api). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([send_message/3, set_my_commands/3, delete_my_commands/2, get_my_commands/2, send_dice/3, set_webhook/3]). -export_type([telegram_api_request/0, api_response/1]). -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()}))}. -type api_response(PEL) :: {api_response, boolean(), PEL}. -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@6 = 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 ), gleam@result:map( _pipe@1, fun(_capture@1) -> set_query(_capture@1, Query) end ); {telegram_api_post_request, Url@1, Body, Query@1} -> _pipe@2 = gleam@http@request:to(Url@1), _pipe@3 = gleam@result:map( _pipe@2, fun(_capture@2) -> gleam@http@request:set_body(_capture@2, Body) end ), _pipe@4 = gleam@result:map( _pipe@3, fun(_capture@3) -> gleam@http@request:set_method(_capture@3, post) end ), _pipe@5 = gleam@result:map( _pipe@4, fun(_capture@4) -> gleam@http@request:set_header( _capture@4, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ) end ), gleam@result:map( _pipe@5, fun(_capture@5) -> set_query(_capture@5, Query@1) end ) end, gleam@result:map_error( _pipe@6, 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 response_decoder( fun((gleam@dynamic:dynamic_()) -> {ok, PGF} | {error, list(gleam@dynamic:decode_error())}) ) -> fun((gleam@dynamic:dynamic_()) -> {ok, api_response(PGF)} | {error, list(gleam@dynamic:decode_error())}). response_decoder(Result_decoder) -> gleam@dynamic:decode2( fun(Field@0, Field@1) -> {api_response, Field@0, Field@1} end, gleam@dynamic:field(<<"ok"/utf8>>, fun gleam@dynamic:bool/1), gleam@dynamic:field(<<"result"/utf8>>, Result_decoder) ). -spec map_resonse( {ok, gleam@http@response:response(binary())} | {error, binary()}, fun((gleam@dynamic:dynamic_()) -> {ok, PFZ} | {error, list(gleam@dynamic:decode_error())}) ) -> {ok, PFZ} | {error, binary()}. map_resonse(Response, Result_decoder) -> _pipe = Response, _pipe@3 = gleam@result:map( _pipe, fun(Response@1) -> {response, _, _, Body} = Response@1, Decode = response_decoder(Result_decoder), _pipe@1 = gleam@json:decode(Body, Decode), _pipe@2 = gleam@result:map_error( _pipe@1, fun(_) -> <<"Failed to decode response: "/utf8, Body/binary>> end ), gleam@result:map( _pipe@2, fun(Response@2) -> erlang:element(3, Response@2) end ) end ), gleam@result:flatten(_pipe@3). -spec new_post_request( binary(), binary(), binary(), gleam@option:option(list({binary(), binary()})) ) -> telegram_api_request(). new_post_request(Token, Path, Body, Query) -> Url = <<<<<<"https://api.telegram.org/bot"/utf8, Token/binary>>/binary, "/"/utf8>>/binary, Path/binary>>, {telegram_api_post_request, Url, Body, Query}. -spec send_message(binary(), integer(), binary()) -> {ok, telega@models@message:message()} | {error, binary()}. send_message(Token, Chat_id, Text) -> _pipe@1 = new_post_request( Token, <<"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), _pipe@3 = fetch(_pipe@2), map_resonse(_pipe@3, fun telega@models@message:decode/1). -spec set_my_commands( binary(), list(telega@models@bot_command:bot_command()), gleam@option:option(telega@models@bot_command:bot_command_parameters()) ) -> {ok, boolean()} | {error, binary()}. set_my_commands(Token, Commands, Parameters) -> Parameters@1 = begin _pipe = gleam@option:unwrap( Parameters, telega@models@bot_command:new_botcommand_parameters() ), telega@models@bot_command:encode_botcommand_parameters(_pipe) end, Body_json = gleam@json:object( [{<<"commands"/utf8>>, gleam@json:array( Commands, fun(Command) -> gleam@json:object( [{<<"command"/utf8>>, gleam@json:string( erlang:element(2, Command) )}, {<<"description"/utf8>>, gleam@json:string( erlang:element(3, Command) )} | Parameters@1] ) end )}] ), _pipe@1 = new_post_request( Token, <<"setMyCommands"/utf8>>, gleam@json:to_string(Body_json), none ), _pipe@2 = api_to_request(_pipe@1), _pipe@3 = fetch(_pipe@2), map_resonse(_pipe@3, fun gleam@dynamic:bool/1). -spec delete_my_commands( binary(), gleam@option:option(telega@models@bot_command:bot_command_parameters()) ) -> {ok, boolean()} | {error, binary()}. delete_my_commands(Token, Parameters) -> Parameters@1 = begin _pipe = gleam@option:unwrap( Parameters, telega@models@bot_command:new_botcommand_parameters() ), telega@models@bot_command:encode_botcommand_parameters(_pipe) end, Body_json = gleam@json:object(Parameters@1), _pipe@1 = new_post_request( Token, <<"deleteMyCommands"/utf8>>, gleam@json:to_string(Body_json), none ), _pipe@2 = api_to_request(_pipe@1), _pipe@3 = fetch(_pipe@2), map_resonse(_pipe@3, fun gleam@dynamic:bool/1). -spec get_my_commands( binary(), gleam@option:option(telega@models@bot_command:bot_command_parameters()) ) -> {ok, list(telega@models@bot_command:bot_command())} | {error, binary()}. get_my_commands(Token, Parameters) -> Parameters@1 = begin _pipe = gleam@option:unwrap( Parameters, telega@models@bot_command:new_botcommand_parameters() ), telega@models@bot_command:encode_botcommand_parameters(_pipe) end, Body_json = gleam@json:object(Parameters@1), _pipe@1 = new_post_request( Token, <<"getMyCommands"/utf8>>, gleam@json:to_string(Body_json), none ), _pipe@2 = api_to_request(_pipe@1), _pipe@3 = fetch(_pipe@2), map_resonse(_pipe@3, fun telega@models@bot_command:decode/1). -spec send_dice( binary(), integer(), gleam@option:option(telega@models@dice:send_dice_parameters()) ) -> {ok, telega@models@message:message()} | {error, binary()}. send_dice(Token, Chat_id, Parameters) -> Parameters@1 = gleam@option:unwrap( Parameters, telega@models@dice:new_send_dice_parameters(Chat_id) ), Body_json = telega@models@dice:encode_send_dice_parameters(Parameters@1), _pipe = new_post_request( Token, <<"sendDice"/utf8>>, gleam@json:to_string(Body_json), none ), _pipe@1 = api_to_request(_pipe), _pipe@2 = fetch(_pipe@1), map_resonse(_pipe@2, fun telega@models@message:decode/1). -spec new_get_request( binary(), binary(), gleam@option:option(list({binary(), binary()})) ) -> telegram_api_request(). new_get_request(Token, Path, Query) -> Url = <<<<<<"https://api.telegram.org/bot"/utf8, Token/binary>>/binary, "/"/utf8>>/binary, Path/binary>>, {telegram_api_get_request, Url, Query}. -spec set_webhook(binary(), binary(), gleam@option:option(binary())) -> {ok, boolean()} | {error, binary()}. set_webhook(Token, Webhook_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, <<"setWebhook"/utf8>>, {some, Query@1}), _pipe@1 = api_to_request(_pipe), _pipe@2 = fetch(_pipe@1), map_resonse(_pipe@2, fun gleam@dynamic:bool/1).