Karta.Messaging.Plug behaviour (Karta v1.0.0)

The contract of a kind of connector (a "plug"). A plug reports; only Karta retries or falls back (Karta.Messaging.Router).

There are two kinds: :http plugs run inside Karta and call a provider (an SMS provider), :remote plugs are an agent on another machine that holds a WebSocket to Karta (Karta.Messaging.Plugs.Postduif, protocol in docs/suite/plans/postduif-protocol.md).

Error classes

permanent, retry, unknown_recipient, not_linked, rate_limited (with retry_after_s), unknown_outcome, unsupported, too_large, expired - the same for both kinds (Karta.Messaging.Errors).

Capabilities

send.text, send.media, check.recipient, receive, receipts, history, typing: a map from the capability to the contract version (or true). A request that needs a capability the connector lacks skips it with the class unsupported.

Summary

Types

What a plug is asked to send.

Callbacks

The capabilities the connector has now.

The channel the plug serves.

Whether the addresses are on the channel.

Verifies and parses a provider's webhook (:http plugs).

True when the plug can be asked to send the same attempt again with the same effect (the provider honours an idempotency key). Then a job that died in the middle, or an answer that asks for same_attempt, runs the send again instead of ending the attempt as unknown.

:http runs inside Karta, :remote is an agent holding a WebSocket.

Sends one attempt (:http plugs; a remote plug's attempts become commands).

Whether the connector's credentials are still valid.

Checks the configuration and the secrets of a new connector.

Types

attempt()

@type attempt() :: %{
  :id => Ecto.UUID.t(),
  :to => String.t(),
  :kind => :text | :media,
  :text => String.t() | nil,
  optional(:media) => map() | nil,
  optional(:sender) => String.t() | nil,
  optional(:category) => String.t() | nil,
  optional(:reference) => String.t() | nil
}

What a plug is asked to send.

error_class()

@type error_class() ::
  :permanent
  | :retry
  | :unknown_recipient
  | :not_linked
  | :rate_limited
  | :unknown_outcome
  | :unsupported
  | :too_large
  | :expired

error_detail()

@type error_detail() :: %{
  optional(:code) => String.t(),
  optional(:message) => String.t(),
  optional(:retry_after_s) => non_neg_integer()
}

Callbacks

capabilities(t)

@callback capabilities(Karta.Messaging.Connector.t()) :: map()

The capabilities the connector has now.

channel()

@callback channel() :: String.t()

The channel the plug serves.

check_recipient(t, list)

(optional)
@callback check_recipient(Karta.Messaging.Connector.t(), [String.t()]) ::
  {:ok, [map()]} | {:error, term()}

Whether the addresses are on the channel.

handle_webhook(t, list, binary)

(optional)
@callback handle_webhook(
  Karta.Messaging.Connector.t(),
  [{String.t(), String.t()}],
  binary()
) ::
  {:ok, [map()]} | {:error, :bad_signature | :ignored}

Verifies and parses a provider's webhook (:http plugs).

idempotent?()

(optional)
@callback idempotent?() :: boolean()

True when the plug can be asked to send the same attempt again with the same effect (the provider honours an idempotency key). Then a job that died in the middle, or an answer that asks for same_attempt, runs the send again instead of ending the attempt as unknown.

kind()

@callback kind() :: :http | :remote

:http runs inside Karta, :remote is an agent holding a WebSocket.

send(t, attempt)

(optional)
@callback send(Karta.Messaging.Connector.t(), attempt()) ::
  {:ok, %{:provider_id => String.t(), optional(:cost) => map()}}
  | {:error, error_class(), error_detail()}

Sends one attempt (:http plugs; a remote plug's attempts become commands).

status(t)

(optional)
@callback status(Karta.Messaging.Connector.t()) :: :ok | {:error, term()}

Whether the connector's credentials are still valid.

validate_config(config, secrets)

@callback validate_config(config :: map(), secrets :: map()) :: :ok | {:error, term()}

Checks the configuration and the secrets of a new connector.