Noizu.Discord.Interactions (Noizu.Discord v0.1.0)

Copy Markdown View Source

HTTP interactions (slash commands, components) — signature verification and callback responses. REST-only: interactions arrive at your HTTP endpoint, not via the gateway.

Discord signs each request with Ed25519 over timestamp <> raw_body, sending X-Signature-Ed25519 (hex) and X-Signature-Timestamp. Verify against the raw request body before decoding JSON, and answer PING (type 1) with respond(id, token, :pong) — or directly in the HTTP response body (%{type: 1}).

Summary

Functions

Builds the callback JSON body for respond/4.

POST /interactions/{interaction.id}/{interaction.token}/callback — respond to an interaction (no bot token needed).

Verifies an interaction request signature. Returns true only for a valid Ed25519 signature; malformed hex or keys return false (never raise).

Types

response()

@type response() ::
  :pong
  | {:message, map()}
  | {:deferred, map()}
  | %{:type => pos_integer(), optional(:data) => map()}
  | %{required(String.t()) => term()}

Functions

build_response(raw)

@spec build_response(response()) :: map()

Builds the callback JSON body for respond/4.

respond(interaction_id, interaction_token, response, opts \\ [])

POST /interactions/{interaction.id}/{interaction.token}/callback — respond to an interaction (no bot token needed).

response is one of:

  • :pong — type 1 (PONG).
  • {:message, data} — type 4 (CHANNEL_MESSAGE_WITH_SOURCE); data = message params.
  • {:deferred, data} — type 5 (DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE); data may be %{} or %{flags: 64} for ephemeral.
  • a raw map %{type: t, data: d} for any other callback type.

Opts: :client (base_url/req_options/retry source, token unused), or :base_url / :req_options / :retry; :with_response (query flag); :meta.

verify_signature(public_key_hex, signature_hex, timestamp, body)

@spec verify_signature(String.t(), String.t(), String.t(), iodata()) :: boolean()

Verifies an interaction request signature. Returns true only for a valid Ed25519 signature; malformed hex or keys return false (never raise).