Skip to contents

Starts an HTTP server (powered by plumber) that listens for incoming mixi2 events on POST /events and responds to health checks on GET /healthz.

Usage

start_webhook_server(
  handler,
  port = NULL,
  public_key = Sys.getenv("MIXI2_SIGNATURE_PUBLIC_KEY"),
  host = "0.0.0.0"
)

Arguments

handler

A function with signature function(event) where event is a named list parsed from the JSON body. The function should return NULL or a scalar value; its return value is ignored.

port

Port to listen on (default 8080). Can also be set via the PORT environment variable.

public_key

Raw Ed25519 public key bytes, or a Base64-encoded string. Defaults to the value of Sys.getenv("MIXI2_SIGNATURE_PUBLIC_KEY").

host

Host to bind to (default "0.0.0.0").

Value

Called for its side effect (blocks until interrupted). Invisibly returns NULL.

Details

Incoming requests are verified using an Ed25519 signature (x-mixi2-application-event-signature header) and a timestamp check (x-mixi2-application-event-timestamp, allowed skew +/-300 s).

Ping events (EVENT_TYPE_PING) are handled internally and are not passed to handler.

Examples

if (FALSE) { # \dontrun{
my_handler <- function(event) {
  message("Received event type: ", event$eventType)

  if (!is.null(event$postCreatedEvent)) {
    post <- event$postCreatedEvent$post
    message("New post: ", post$text)
  }

  if (!is.null(event$chatMessageReceivedEvent)) {
    msg <- event$chatMessageReceivedEvent$message
    # echo back with the client if needed
  }
}

start_webhook_server(
  handler    = my_handler,
  port       = 8080,
  public_key = Sys.getenv("MIXI2_SIGNATURE_PUBLIC_KEY")
)
} # }