-module(telega@api). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([set_webhook/2, send_text/3]). -spec fetch(gleam@http@request:request(binary())) -> {ok, gleam@http@response:response(binary())} | {error, binary()}. fetch(Request) -> _pipe = Request, _pipe@1 = gleam@httpc:send(_pipe), gleam@result:map_error( _pipe@1, fun(Error) -> _pipe@2 = gleam@dynamic:string(Error), gleam@result:unwrap(_pipe@2, <<"Failed to send request"/utf8>>) end ). -spec build_post_request(binary(), binary()) -> {ok, gleam@http@request:request(binary())} | {error, nil}. build_post_request(Url, Body) -> _pipe = gleam@http@request:to(Url), _pipe@1 = gleam@result:map( _pipe, fun(_capture) -> gleam@http@request:set_body(_capture, Body) end ), _pipe@2 = gleam@result:map( _pipe@1, fun(_capture@1) -> gleam@http@request:set_method(_capture@1, post) end ), gleam@result:map( _pipe@2, fun(_capture@2) -> gleam@http@request:set_header( _capture@2, <<"Content-Type"/utf8>>, <<"application/json"/utf8>> ) end ). -spec build_webhook_request(binary(), binary()) -> {ok, gleam@http@request:request(binary())} | {error, binary()}. build_webhook_request(Token, Webhook_url) -> Webhook_url@1 = <<<<<<"https://api.telegram.org/bot"/utf8, Token/binary>>/binary, "/setWebhook?url="/utf8>>/binary, Webhook_url/binary>>, logging:log(debug, <<"Webhook URL: "/utf8, Webhook_url@1/binary>>), _pipe = gleam@http@request:to(Webhook_url@1), gleam@result:map_error( _pipe, fun(_) -> <<"Failed to build webhook request"/utf8>> end ). -spec set_webhook(binary(), binary()) -> {ok, gleam@http@response:response(binary())} | {error, binary()}. set_webhook(Token, Webhook_url) -> _pipe = build_webhook_request(Token, Webhook_url), gleam@result:then(_pipe, fun fetch/1). -spec build_send_text_request(binary(), integer(), binary()) -> {ok, gleam@http@request:request(binary())} | {error, binary()}. build_send_text_request(Token, Chat_id, Message) -> Message@1 = begin _pipe = gleam@json:object( [{<<"chat_id"/utf8>>, gleam@json:int(Chat_id)}, {<<"text"/utf8>>, gleam@json:string(Message)}] ), gleam@json:to_string(_pipe) end, Send_message_url = <<<<"https://api.telegram.org/bot"/utf8, Token/binary>>/binary, "/sendMessage"/utf8>>, _pipe@1 = build_post_request(Send_message_url, Message@1), gleam@result:map_error( _pipe@1, fun(_) -> <<"Failed to build sendMessage request"/utf8>> end ). -spec send_text(binary(), integer(), binary()) -> {ok, gleam@http@response:response(binary())} | {error, binary()}. send_text(Token, Chat_id, Message) -> _pipe = build_send_text_request(Token, Chat_id, Message), gleam@result:then(_pipe, fun fetch/1).