Skip to main content

Module http_interactions

Module http_interactions 

Source
Expand description

HTTP interactions (D-395, D-398–D-403): answering the interactions Discord posts to the app’s Interactions Endpoint URL with the same routes and handlers as a gateway bot.

An app with an Interactions Endpoint URL receives its commands, components, autocomplete and modals as HTTP POST requests instead of gateway events, so it needs no long-lived connection: it can run in any HTTP server, or on a serverless platform such as Cloudflare Workers. Client::interactions_endpoint makes an InteractionsEndpoint from a client; its handle takes a request’s X-Signature-Ed25519 and X-Signature-Timestamp headers and its body, and returns the HttpResponse to send back. There is no HTTP server in djs-rs: mount handle in the one the bot already uses.

What handle does, in order:

  1. Verifies the request (Discord’s security rules): the Ed25519 signature of the timestamp and body under the app’s public key, and a timestamp at most 5 minutes from now against replayed requests (D-401). Otherwise 401.
  2. Answers a PING (sent when the URL is saved in the Developer Portal) with a PONG.
  3. Dispatches the interaction like a gateway INTERACTION_CREATE: the cache, the event streams and collectors, the InteractionCreate handlers, then the routes (Client::command and the rest).
  4. Returns as soon as a handler gives the initial response (reply, defer_reply, update, show_modal, …): it becomes the HTTP body, with no REST request (D-399). The handlers’ remaining work (for example the follow-up after a deferral) is the returned future, which the server must run: tokio::spawn natively, ctx.wait_until on Cloudflare Workers.
StatusWhenBody
200the initial response, or the PONGits JSON (Content-Type: application/json)
202the initial response had files: it went through REST insteadempty
400the body is not an interactionempty
401the signature or the timestamp is invalidempty
500the handlers finished without an initial responseempty

Every handler runs one after another for the request, whatever the client’s HandlerMode. Gateway-only parts stay silent: no other event reaches the handlers, and Context::shard is the guild’s shard only if the client is also logged in (else a detached sender, D-403). Collectors only see the interactions this same client handles, so on a platform that spreads requests over many instances they may never see the next one.

§On Cloudflare Workers (wasm32-unknown-unknown)

Build djs-rs with default-features = false, features = ["http-interactions"] and give the client a Rest with a Transport over fetch (the repository’s examples/cloudflare-workers is a complete bot). Handlers must be Send, so a JS future awaited in one (a fetch, worker::Delay) is wrapped in worker::send::SendFuture. tokio’s timers do not run there: collectors and the gateway requests are not available, and a handler waits with the platform’s timers instead.

discord.js counterpart: none (discord.js only receives interactions over the gateway); the official discord-interactions package has the verification (verifyKey).

§Examples

use djs_rs::prelude::*;

let client = Client::builder(std::env::var("DISCORD_TOKEN")?).build();
client.command("ping", |ctx, cmd| async move {
    cmd.reply(&ctx, "Pong!").await?;
    Ok(())
});
let endpoint = client.interactions_endpoint(&std::env::var("DISCORD_PUBLIC_KEY")?)?;

// In the HTTP server's `POST` handler, with the request's headers and body:
let (response, background) = endpoint.handle(signature, timestamp, body).await;
tokio::spawn(background);
// … answer with `response.status` and `response.body`.

Structs§

HttpResponse
The response to an HTTP interaction request, returned by InteractionsEndpoint::handle: send status and body, with Content-Type: application/json when body is not empty.
InteractionsEndpoint
Answers the interactions Discord posts to the app’s Interactions Endpoint URL with a Client’s routes and handlers; made by Client::interactions_endpoint. Cheap to clone.
InvalidPublicKey
The error of Client::interactions_endpoint: the public key is not 64 hexadecimal digits of a valid Ed25519 key.