Class WebhookHandler

java.lang.Object
lv.id.jc.dicechess.runtime.WebhookHandler

public final class WebhookHandler extends Object
Orchestrates one webhook delivery: the ownership handshake, signature verification, and dispatch to the bot's move-choosing function.

handle(Map, String, long) never throws — every failure mode, including an exception from the strategy function, becomes a Response with an appropriate status code, so a caller can always write its result straight back to the HTTP client.

  • Field Details

  • Constructor Details

    • WebhookHandler

      public WebhookHandler(String secret, Function<TurnContext, List<String>> strategy)
      Creates a handler bound to one secret and one strategy, without the GET /games/{id}/moves fallback — TurnContext.legalMoves() will be null on the rare turn where the envelope's inline tree is capped.
      Parameters:
      secret - the webhook secret issued by the platform when the bot registered
      strategy - chooses moves for a turn: a TurnContext in, a UCI move path out
    • WebhookHandler

      public WebhookHandler(String secret, String playApiBaseUrl, Function<TurnContext, List<String>> strategy)
      Creates a handler bound to one secret and one strategy, fetching GET /games/{id}/moves from playApiBaseUrl whenever the envelope's inline legal-move tree is capped — see TurnContext.legalMoves(). That endpoint is public, so no additional credential is needed.
      Parameters:
      secret - the webhook secret issued by the platform when the bot registered
      playApiBaseUrl - play-api's base URL (e.g. https://play-api.jc.id.lv); a trailing slash is tolerated
      strategy - chooses moves for a turn: a TurnContext in, a UCI move path out
  • Method Details

    • handle

      public Response handle(Map<String,String> headers, String rawBody, long nowEpochSeconds)
      Handles one delivery.
      Parameters:
      headers - the request headers; lookup is case-insensitive, so any casing works
      rawBody - the raw request body, exactly as received (the signature covers these exact bytes)
      nowEpochSeconds - the current time, Unix epoch seconds
      Returns:
      the response to send back — status 200 (handshake echoed, or moves chosen), 400 (unparseable or unrecognized body), 401 (missing, expired, or wrong signature), or 500 (the strategy function threw)