Current section
Files
Jump to
Current section
Files
src/discord_webhook.erl
-module(discord_webhook).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/discord_webhook.gleam").
-export([new/1, send/2]).
-export_type([webhook/0, message/0]).
-opaque webhook() :: {webhook, binary()}.
-type message() :: {message, binary()}.
-file("src/discord_webhook.gleam", 19).
-spec new(binary()) -> webhook().
new(Url) ->
{webhook, Url}.
-file("src/discord_webhook.gleam", 24).
-spec convert_json(message()) -> binary().
convert_json(Message) ->
_pipe = gleam@json:object(
[{<<"content"/utf8>>, gleam@json:string(erlang:element(2, Message))}]
),
gleam@json:to_string(_pipe).
-file("src/discord_webhook.gleam", 30).
-spec send(webhook(), binary()) -> nil.
send(Webhook, Content) ->
Message_data = {message, Content},
Json_payload = convert_json(Message_data),
case gleam@http@request:to(erlang:element(2, Webhook)) of
{error, _} ->
gleam_stdlib:println(<<"Failed to parse webhook URL."/utf8>>);
{ok, Base_req} ->
Req = begin
_pipe = Base_req,
_pipe@1 = gleam@http@request:set_method(_pipe, post),
_pipe@2 = gleam@http@request:set_header(
_pipe@1,
<<"content-type"/utf8>>,
<<"application/json"/utf8>>
),
gleam@http@request:set_body(_pipe@2, Json_payload)
end,
case gleam@httpc:send(Req) of
{ok, Response} ->
gleam_stdlib:println(
<<"Response Status: "/utf8,
(erlang:integer_to_binary(
erlang:element(2, Response)
))/binary>>
);
{error, _} ->
gleam_stdlib:println(
<<"Failed to fire HTTP request entirely."/utf8>>
)
end
end.