Wardrik.Channel behaviour (wardrik_protocol v0.1.0-rc.6)

Copy Markdown

A behaviour for chat apps, like Slack, that people reach agents through.

channels: [slack: {WardrikSlack, token: token, signing_secret: secret}]

Requests to POST /v1/channels/{channel}/{path} reach request/2 unchanged, so the channel can check their signature. To deliver one yourself:

{:ok, answer} = Wardrik.Channel.deliver(scope,
  channel: "slack",
  path: "events",
  body: body,
  headers: headers
)

Every callback's options also hold:

  • :wardrik - the instance the channel runs under.
  • :channel - the channel's name.
  • :store - Wardrik's store. Prefix your keys with your package's name.

Summary

Types

A request from the chat app. headers are keyed by lowercase name.

The response sent back to the chat app.

Callbacks

Returns the child spec the channel runs as.

Posts a Wardrik.Channel.Notification, or returns {:skip, reason}. Optional.

Handles a request from the chat app and returns the response.

Functions

Deliver a chat app request to a channel.

Types

request()

@type request() :: %{
  path: String.t(),
  body: String.t(),
  headers: %{required(String.t()) => String.t()}
}

A request from the chat app. headers are keyed by lowercase name.

response()

@type response() :: %{
  status: pos_integer(),
  headers: %{required(String.t()) => String.t()},
  body: String.t()
}

The response sent back to the chat app.

Callbacks

child_spec(opts)

@callback child_spec(opts :: keyword()) :: Supervisor.child_spec()

Returns the child spec the channel runs as.

notify(t, opts)

(optional)
@callback notify(Wardrik.Channel.Notification.t(), opts :: keyword()) ::
  :ok | {:skip, String.t()} | {:error, term()}

Posts a Wardrik.Channel.Notification, or returns {:skip, reason}. Optional.

request(request, opts)

@callback request(request(), opts :: keyword()) :: response()

Handles a request from the chat app and returns the response.

Functions

deliver(scope, input)

@spec deliver(Wardrik.Scope.t(), keyword()) ::
  term() | :ok | {:error, Wardrik.Error.t()}

Deliver a chat app request to a channel.

Wardrik.Channel.deliver(scope, channel: "slack", path: "events", body: body, headers: headers)

Passes a request from a chat app such as Slack, unchanged, to the channel with this name. The channel verifies it, for example by its signature, and responds in the format the chat app expects. Returns not_found if no channel with this name accepts requests.

POST /v1/channels/{channel}/{path}