fapost-lab/connector-slack.
Channel contracts maps each contract to their classes.
The manifest
A connector describes itself in its owncomposer.json: "type": "fapost-connector"
and an extra.fapost block. Core reads it on platform:update without running your code.
Any other field is an error. When two packages claim one id, or two connectors one
channel type, none of them is booted and
platform:update stops with nothing changed.
The rules live in Foundation (Fapost\Foundation\Connector\Manifest\ConnectorManifestParser),
a pure function you can run in your package’s CI.
The service provider
registerChannels() once per process, after every provider’s register()
and before any boot(). registerBindings() runs in register(), bootConnector()
in boot(). Core boots connectors before Solutions.
A ConnectorChannel names classes, not instances: Core builds each service inside the
request or job that uses it and never shares one across requests or jobs, so a service
may depend on tenant-scoped bindings. Core also builds one instance at boot to check
it, so constructors must be cheap and must not touch tenant data.
A sender gets the channel with each call:
deliver(OutboundMessage $message, ChannelInterface $channel).
Read the token and the channel’s settings from $channel->getConfig() (token, and the
values your profile declares under options, such as sender_name for Viber) while you
call the provider, and keep none of them.
Boot fails, naming your connector, when you register a type your manifest does not
declare, a type Core serves itself (such as telegram), a type twice, a class that does
not implement its interface, a media class or a profile whose channelType() is another
type, or a profile whose settings contradict themselves.
platform:update also refuses a declared type you never register.
Buttons and capabilities
A channel that declares nocapabilities is text and media only, and Core sends it
nothing else. Declare what the channel can do with ChannelCapability:
CallbackButtons, ReplyButtons, ShareContact, ShareLocation, RemoveButtons
(strip the buttons of a message already sent) and PersistentButtons (callback buttons
of older messages stay pressable).
Core describes the buttons of a message by what a press does, not by where a channel
draws them: MessagePayload::$buttons is a ButtonSet of rows of Button. Each button
has a label and an action: Callback (the press returns a hidden payload),
Reply (the press sends the label as the user’s message), ShareContact or
ShareLocation. A set holds either callback buttons only or none. Translate it into the
provider’s format in your sender. Core’s flow messages carry buttons and leave
MessagePayload::$keyboard null; keyboard is deprecated, so a sender that reads only
it shows no buttons.
The flow builder reads these declarations: when a flow uses something one of the
assistant’s channels does not declare (for example a contact request on a channel without
ShareContact), validation shows a warning naming the channel and the capability. A warning
never blocks publishing, so an undeclared capability only means that part of the flow
degrades on that channel.
Button limits
ChannelCapabilities takes a ButtonLimits as its second argument: maxButtons (in one
message), maxButtonsPerRow and maxLabelLength (in UTF-8 characters). null, the default,
means the channel declares no limit and nothing is checked. When a flow’s keyboard is larger
than a declared limit, validation shows a channel_button_limit_exceeded warning naming the
channel, the limit and the actual size. It is a warning only and never blocks publishing, so
declare what the provider documents or what you have observed it to enforce. You need no limit
for the payload length: it is at most 64 bytes on every channel.
Telegram, for example, declares 100 buttons and 8 per row and leaves the label length open:
its Bot API documents no keyboard counts or label length, and clients truncate long labels.
When the user presses a Callback button, report it as an IncomingMessage of type
IncomingMessageType::CallbackQuery whose text is the button’s payload, unchanged.
How the provider delivers the press is the connector’s business. The payload is at most
Button::MAX_PAYLOAD_BYTES (64) bytes and Core treats it as opaque to you.
Settings, public link and media limits
Core knows nothing about your provider, so the channel form, the rules that check it and the link to your bot come from a profile you declare:Text, Integer, Boolean, Choice (one value of
choices) and Choices (a list of them). Core builds the validation rules and the form control
from the type, min, max, required and choices; you never validate a value yourself.
Values live under their key in the channel’s config. A field is never secret, because
config is plain JSON that reaches queued jobs: keep secrets in the channel’s token and secret
token.
- Texts.
label,helpand the labels ofchoicesare English. SettranslationKeyto show a translation: Core looks for{translationKey}.label,.helpand.choices.{value}, and falls back to your English text. Load your translation files withloadTranslationsFromin your provider. A help text may use:min,:maxand:default. - Managed fields. A field with
managed: trueholds a value the integration reads from the provider, such as Telegram’s bot name or Viber’s account URI. The form neither shows nor accepts it, and saving the form keeps the stored value. You never write it yourself: injectFapost\Foundation\Channel\Settings\ManagedChannelSettingsInterfaceinto your webhook registrar and callreport($payload->channelId, ['account_uri' => $uri])after the provider answers (see below). It needs the channel’s id and runs in the channel’s tenant, which the registrar has; the inbound adapter has neither, so it cannot report a value yet. Anullremoves the key. Core checks each value against the field’s type,min,maxandchoices, stores it, and does not synchronize the provider again because of it. A key that is not a managed field of your schema, or a value that does not fit, throwsInvalidManagedSettingException: that is a mistake in the connector, and it shows in the failed job. A channel deleted in the meantime is ignored. - Secret token. Set
usesSecretToken: falsewhen the provider has no webhook secret; the form then does not ask for one. AsecretTokenPattern(PCRE, no delimiters) restricts what an operator may enter. - Public link.
publicLink()reads the storedconfig, managed fields included, and returns how a person finds the bot: the handle, and a link that opens the chat. Core shows it in the channel list and gives it to flows assystem.channel. - No profile. A channel without one has no settings, any secret token and no link.
- Your own configuration. A connector is an ordinary Laravel package: publish and merge
its configuration file with
mergeConfigFromin your provider.
mediaLimits names a class that says how large a file the provider accepts for each kind of
media; Core warns before someone attaches a larger one.
Inbound webhooks
The inbound adapter does not verify signatures in code. It declares how in anIngressSpec, which Core and the ingress gateway both execute, and turns a verified
body into an IncomingMessage in the worker:
SignatureScheme covers, the scheme is
extended in Foundation and the gateway; a connector cannot ship its own check.
Signed timestamps, forms and challenges (IngressSpec version 2)
Some providers sign more than the body, send the event inside a form field or ask the
URL to prove it is yours first. These are optional parameters of the IngressSpec
constructor; a spec that sets none of them stays version 1:
A template placeholder may list alternatives:
{body.event_id|body.trigger_id} takes the
first one that is not empty. {body.*} placeholders read the request document: the JSON
body, or the JSON in the form field. That document is also what your adapter’s
normalize() receives. A refused request, as before, is answered 200 {"ok":true}.
Every delivery your adapter accepts must give the idempotency key a non-empty id. A key
whose placeholders all resolve empty (acme:{channel}:) is one shared slot for 24 hours,
so every later delivery of that shape is dropped as a duplicate. Add an alternative, or
treat such a delivery as one to ignore.
Deliveries that are not messages
Providers also send the bot’s own messages, edits and system events. Whennormalize()
meets one, throw Fapost\Foundation\Channel\InboundEventIgnoredException with a short
reason. Core drops the delivery: no contact is created, nothing reaches the transcript
and the job is not retried. The reason is written to the debug log.
The setup guide
A person connecting a channel often has work to do at the provider: paste the webhook URL, add a manifest. Let your profile also implementChannelSetupGuideInterface and the
console shows those steps beside the channel form:
$webhookUrl is the channel’s public webhook URL, or null while the channel is not yet
created; $config is its stored configuration. The method does no I/O. A step has a
title, an optional text, an optional value to copy (Text, Url or Code, such as a
manifest) and an optional translation key: Core looks for {key}.title and {key}.text
in its translations (add yours with loadTranslationsFrom) and falls back to the English
text of the step. The URL contains the channel’s public hash, so only users who can
manage channels see the guide.
Testing a connector
Fapost\Testing\ConnectorTestCase boots Core with its test database and gives a
connector’s own tests what they need, without importing anything from App\. Nothing
reaches a real Redis or queue. It is experimental and may change in minor versions
before 1.0.
Fake the provider’s HTTP API with
Http::fake().
When a connector is removed
Channels of a type no connector serves any more stay in the database untouched. The console marks them unavailable and lets you delete them; their incoming webhooks are logged and acknowledged without being processed, andphp artisan platform:update
lists them per tenant. Installing the connector again brings them back.
Installing
The connectors that exist today are listed in Connectors. Add the package to your image through the build overlay (Building your own images), then runphp artisan platform:update: it lists the installed connectors with their channel
types, or refuses with every manifest problem it found.