The Slack adapter for pyfect.chat (Effect's @effect/chat-slack/SlackChat):
the Registry layer, the Events API webhook, the Interactivity route for
buttons, modals and slash commands, the pure normalisers, address helpers and
the scoped post options.
- Bot token scopes:
chat:write,channels:history,groups:history,im:history,mpim:history,channels:read,groups:read,im:read,mpim:read,users:read,reactions:read,files:readandfiles:write(andcommandsfor slash commands). - Event subscriptions, sent to
layer_webhook's path:message.channels,message.groups,message.im,message.mpim,app_mention,reaction_added,reaction_removedandagent_session_stopped. - Buttons, selects, modals and slash commands need Interactivity, with its
Request URL at
layer_interactivity's path. With Socket Mode, pass each payload toSlackChatService.interactionand send its result as the envelope ack'spayload. - Slack's stop button needs the app declared as an agent
(
features.agent_view). Without it the session status calls answernot_authorized; the adapter then stops making them.
Re-exports#
Types#
| Type | Definition | Description |
|---|---|---|
SessionStatus | type SessionStatus = Literal['active', 'processing', 'suspended', 'closed'] |
Classes#
Functions#
layer#
layer(*, bot_token, team_id, base_url=DEFAULT_BASE_URL, max_retry_after=None) -> Layer[SlackChat | SlackWebApi, Never, HttpClient | Registry]Registers the Slack adapter in the Registry, for one workspace,
and provides SlackChat and SlackWebApi.
-
Web API calls go to
{base_url}/{method}, as withslack_web_api.layer:"https://slack-gov.com/api"for GovSlack. -
postandeditsend the Markdown as onemarkdownblock, in which a<outside code goes as<, plus atextthe adapter builds, in which every<,>and&goes as an entity. Slack decides who a message mentions fromtext, so no token notifies unlesswith_post_optionsallows it. -
That
textis cut to 16,000 code points on a post and 3,800 UTF-8 bytes on an edit, on a grapheme cluster boundary, ending in…. -
Stream appends go as
markdown_text; token shapes get a zero-width space after their<, which one cleanup edit takes out when the stream stops. -
post,editand stream writes failTooLargebefore calling Slack past 12,000 code points as sent. -
A post with files uploads them, posts, then attaches them, all or nothing; see
SlackAttachFailed. -
Edits to one message are at least 3 seconds apart; after a 429 a reply's writes back off.
-
replyTois not supported. -
A call Slack answers 429 is retried after its
Retry-After, however long, unlessmax_retry_afteris set: a longer wait then fails the op at once withRateLimited, itsretry_afterthe wait asked for.
| Parameter | Type | Description |
|---|---|---|
bot_token | Redacted[str] | |
team_id | str | |
base_url | str = DEFAULT_BASE_URL | |
max_retry_after | DurationInput | None = None |
Returns Layer[SlackChat | SlackWebApi, Never, HttpClient | Registry]
def layer(
*,
bot_token: Redacted[str],
team_id: str,
base_url: str = DEFAULT_BASE_URL,
max_retry_after: DurationInput | None = None,
) -> Layer[SlackChat | SlackWebApi, Never, HttpClient | Registry]:
"""Registers the Slack adapter in the ``Registry``, for one workspace,
and provides ``SlackChat`` and ``SlackWebApi``.
- Web API calls go to ``{base_url}/{method}``, as with
``slack_web_api.layer``: ``"https://slack-gov.com/api"`` for GovSlack.
- ``post`` and ``edit`` send the Markdown as one ``markdown`` block, in
which a ``<`` outside code goes as ``<``, plus a ``text`` the adapter
builds, in which every ``<``, ``>`` and ``&`` goes as an entity. Slack
decides who a message mentions from ``text``, so no token notifies
unless ``with_post_options`` allows it.
- That ``text`` is cut to 16,000 code points on a post and 3,800 UTF-8
bytes on an edit, on a grapheme cluster boundary, ending in ``…``.
- Stream appends go as ``markdown_text``; token shapes get a zero-width
space after their ``<``, which one cleanup edit takes out when the
stream stops.
- ``post``, ``edit`` and stream writes fail ``TooLarge`` before calling
Slack past 12,000 code points as sent.
- A post with files uploads them, posts, then attaches them, all or
nothing; see ``SlackAttachFailed``.
- Edits to one message are at least 3 seconds apart; after a 429 a
reply's writes back off.
- ``replyTo`` is not supported.
- A call Slack answers 429 is retried after its ``Retry-After``, however
long, unless ``max_retry_after`` is set: a longer wait then fails the
op at once with ``RateLimited``, its ``retry_after`` the wait asked
for.
"""
retry_ceiling = None if max_retry_after is None else from_input_unsafe(max_retry_after)
@fx.gen
def build() -> Generator[Effect[Any, Never, Any], Any, SlackChatService]:
api = yield from SlackWebApi()
http = yield from HttpClient()
registry = yield from Registry()
cached: list[str] = []
def read_bot_user_id() -> Effect[str, ChatError]:
if cached:
return fx.succeed(cached[0])
def found(answer: object) -> Effect[str, ChatError]:
id = _json.string(answer, "user_id")
if id is None:
return fx.fail(
_errors.chat_error(
"botUserId", _errors.invalid_response(False, "missing_user_id")
)
)
cached.append(id)
return fx.succeed(id)
return (
api.call("auth.test", {})
.map_error(lambda error: _errors.with_op("botUserId", error))
.flat_map(found)
)
bot_user_id = fx.suspend(read_bot_user_id)
adapter = SlackAdapter(
api=api,
http=http,
bot_token=bot_token,
team_id=team_id,
bot_user_id=bot_user_id,
attach_failed=lambda thread, message_id, file_ids, reason: SlackAttachFailed(
thread=thread, message_id=message_id, file_ids=file_ids, reason=reason
),
max_retry_after=retry_ceiling,
)
yield from registry.register(adapter)
interaction = _interactions.make(
call_views_open=lambda body: api.call("views.open", body, write=True, retry=False),
team_id=team_id,
answer=adapter.answer,
)
return SlackChatService(
team_id=team_id, bot_user_id=bot_user_id, adapter=adapter, interaction=interaction
)
return _layer_effect(SlackChat, build).pipe(
_provide_merge(
_web_api_layer(bot_token=bot_token, base_url=base_url, max_retry_after=retry_ceiling)
)
)thread#
thread(*, team, channel, thread_ts=None) -> ChatThreadThe address of a Slack channel, direct conversation or thread:
{team}/{channel}, or {team}/{channel}/{thread_ts}.
| Parameter | Type | Description |
|---|---|---|
team | str | |
channel | str | |
thread_ts | str | None = None |
Returns ChatThread
def thread(*, team: str, channel: str, thread_ts: str | None = None) -> ChatThread:
"""The address of a Slack channel, direct conversation or thread:
``{team}/{channel}``, or ``{team}/{channel}/{thread_ts}``."""
return _address.thread(Parts(team, channel, thread_ts))parts#
parts(address) -> Parts | NoneThe parts of a Slack address, or None for another platform's or a
malformed one.
| Parameter | Type | Description |
|---|---|---|
address | ChatThread |
Returns Parts | None
def parts(address: ChatThread) -> Parts | None:
"""The parts of a Slack address, or ``None`` for another platform's or a
malformed one."""
return _address.parts(address)layer_webhook#
layer_webhook(*, path, signing_secret, app_id, deadline='2500 millis', max_body_bytes=_webhook.DEFAULT_MAX_BODY_BYTES) -> Layer[Never, Never, HttpRouter | SlackChat | ChatHandler | Mac]POST {path}: the Events API webhook.
- Checks the
v0signature with the signing secret (constant time) and a five minute window, then answersurl_verification. - An envelope for another app, another workspace or of another type is answered 200 and dropped.
- The events go through
normalizeto theChatHandler, one at a time. - Answers 503, so Slack retries, when the handler fails
NotAccepted, when the bot user can't be read, or when the request takes longer thandeadline; each is logged with its own message. - Answers 413 past
max_body_bytes(1 MiB by default), before the signature is checked. - Slack redelivers on a timeout or an error;
chat_event.dedupe_keygives the two deliveries equal keys.
Signature checks use Mac (pyfect.crypto.native.layer_mac).
| Parameter | Type | Description |
|---|---|---|
path | str | |
signing_secret | Redacted[str] | |
app_id | str | |
deadline | DurationInput = '2500 millis' | |
max_body_bytes | int = _webhook.DEFAULT_MAX_BODY_BYTES |
Returns Layer[Never, Never, HttpRouter | SlackChat | ChatHandler | Mac]
def layer_webhook(
*,
path: str,
signing_secret: Redacted[str],
app_id: str,
deadline: DurationInput = "2500 millis",
max_body_bytes: int = _webhook.DEFAULT_MAX_BODY_BYTES,
) -> Layer[Never, Never, HttpRouter | SlackChat | ChatHandler | Mac]:
"""``POST {path}``: the Events API webhook.
- Checks the ``v0`` signature with the signing secret (constant time) and
a five minute window, then answers ``url_verification``.
- An envelope for another app, another workspace or of another type is
answered 200 and dropped.
- The events go through ``normalize`` to the ``ChatHandler``, one at a
time.
- Answers 503, so Slack retries, when the handler fails ``NotAccepted``,
when the bot user can't be read, or when the request takes longer than
``deadline``; each is logged with its own message.
- Answers 413 past ``max_body_bytes`` (1 MiB by default), before the
signature is checked.
- Slack redelivers on a timeout or an error; ``chat_event.dedupe_key``
gives the two deliveries equal keys.
Signature checks use ``Mac`` (``pyfect.crypto.native.layer_mac``).
"""
max_body_size = _max_body_size(max_body_bytes)
limit = from_input_unsafe(deadline)
def register(router: HttpRouterService) -> Effect[None, Never, Any]:
@fx.gen
def body() -> Generator[Effect[Any, Never, Any], Any]:
slack = yield from SlackChat()
handler = yield from ChatHandler()
verify = yield from _webhook.make_verifier(signing_secret)
options = _webhook.Options(
verify=verify,
app_id=app_id,
team_id=slack.team_id,
bot_user_id=slack.bot_user_id,
handle=handler.handle,
deadline=limit,
max_body_size=max_body_size,
)
yield from router.add(
"POST", path, lambda request: _webhook.handle_request(request, options)
)
return body
return _router_use(register)layer_interactivity#
layer_interactivity(*, path, signing_secret, app_id, deadline=_interactions.DEFAULT_DEADLINE, max_body_bytes=_webhook.DEFAULT_MAX_BODY_BYTES) -> Layer[Never, Never, HttpRouter | SlackChat | Handler | Mac]POST {path}: the Interactivity Request URL, which Slack posts
form-encoded, and the Request URL of slash commands.
- Checks the
v0signature aslayer_webhookdoes, decodes the form'spayload(or takes a command's fields) and drops one for another app with a 200. - Hands the payload to
SlackChatService.interactionwithdeadline. Its body is the response, andLateis answered 503, so a modal stays open with Slack's own error. - Answers 413 past
max_body_bytes. Slack never redelivers an interaction.
| Parameter | Type | Description |
|---|---|---|
path | str | |
signing_secret | Redacted[str] | |
app_id | str | |
deadline | DurationInput = _interactions.DEFAULT_DEADLINE | |
max_body_bytes | int = _webhook.DEFAULT_MAX_BODY_BYTES |
Returns Layer[Never, Never, HttpRouter | SlackChat | Handler | Mac]
def layer_interactivity(
*,
path: str,
signing_secret: Redacted[str],
app_id: str,
deadline: DurationInput = _interactions.DEFAULT_DEADLINE,
max_body_bytes: int = _webhook.DEFAULT_MAX_BODY_BYTES,
) -> Layer[Never, Never, HttpRouter | SlackChat | Handler | Mac]:
"""``POST {path}``: the Interactivity Request URL, which Slack posts
form-encoded, and the Request URL of slash commands.
- Checks the ``v0`` signature as ``layer_webhook`` does, decodes the
form's ``payload`` (or takes a command's fields) and drops one for
another app with a 200.
- Hands the payload to ``SlackChatService.interaction`` with
``deadline``. Its body is the response, and ``Late`` is answered 503,
so a modal stays open with Slack's own error.
- Answers 413 past ``max_body_bytes``. Slack never redelivers an
interaction.
"""
max_body_size = _max_body_size(max_body_bytes)
limit = from_input_unsafe(deadline)
def register(router: HttpRouterService) -> Effect[None, Never, Any]:
@fx.gen
def body() -> Generator[Effect[Any, Never, Any], Any]:
slack = yield from SlackChat()
handler = yield from Handler()
verify = yield from _webhook.make_verifier(signing_secret)
options = _webhook.InteractivityOptions(
verify=verify,
app_id=app_id,
interaction=lambda payload, until: slack.interaction(payload, deadline=until).pipe(
fx.provide_service(Handler, handler)
),
deadline=limit,
max_body_size=max_body_size,
)
yield from router.add(
"POST", path, lambda request: _webhook.handle_interactivity(request, options)
)
return body
return _router_use(register)normalize_interaction#
normalize_interaction(payload, *, team_id) -> Interaction | NoneThe normaliser behind SlackChatService.interaction: pure, None
for any payload that is not from an element this adapter built. Every
command gives a CommandInvoked, known or not. A submit's thread and
message are those of the click that opened the modal. raw is the
payload without its verification token; a command's holds its
response_url, so it is a credential for 30 minutes.
| Parameter | Type | Description |
|---|---|---|
payload | object | |
team_id | str |
Returns Interaction | None
def normalize_interaction(payload: object, *, team_id: str) -> Interaction | None:
"""The normaliser behind ``SlackChatService.interaction``: pure, ``None``
for any payload that is not from an element this adapter built. Every
command gives a ``CommandInvoked``, known or not. A submit's thread and
message are those of the click that opened the modal. ``raw`` is the
payload without its verification ``token``; a command's holds its
``response_url``, so it is a credential for 30 minutes."""
return _interactions.normalize(payload, team_id=team_id)normalize#
normalize(envelope, *, team_id, bot_user_id) -> list[ChatEvent]The normaliser, for Socket Mode or a consumer's own transport: pure,
an event_callback envelope to zero or more events.
- An envelope for another workspace gives none.
messageandapp_mentionfor one message give equal events. Anapp_mentionthat carrieseditedgives none.- A message subtype other than
file_share,thread_broadcast,bot_messageandme_messagegives none. - Mentions come from the message's
rich_textblock when it has one, and only for elements whose token is intext. - The bot's own messages with a
rich_textblock read back withtextrendered from the blocks as Markdown. agent_session_stoppedgives aStopRequested.rawis the envelope without its verificationtoken.
| Parameter | Type | Description |
|---|---|---|
envelope | object | |
team_id | str | |
bot_user_id | str |
Returns list[ChatEvent]
def normalize(envelope: object, *, team_id: str, bot_user_id: str) -> list[ChatEvent]:
"""The normaliser, for Socket Mode or a consumer's own transport: pure,
an ``event_callback`` envelope to zero or more events.
- An envelope for another workspace gives none.
- ``message`` and ``app_mention`` for one message give equal events. An
``app_mention`` that carries ``edited`` gives none.
- A message subtype other than ``file_share``, ``thread_broadcast``,
``bot_message`` and ``me_message`` gives none.
- Mentions come from the message's ``rich_text`` block when it has one,
and only for elements whose token is in ``text``.
- The bot's own messages with a ``rich_text`` block read back with
``text`` rendered from the blocks as Markdown.
- ``agent_session_stopped`` gives a ``StopRequested``.
- ``raw`` is the envelope without its verification ``token``.
"""
return _normalize.normalize(envelope, team_id=team_id, bot_user_id=bot_user_id)with_post_options#
with_post_options[A, E, R](options) -> Callable[[Effect[A, E, R]], Effect[A, E, R]]Scoped Slack options for every write inside an effect; use with
.pipe. Nested calls merge, the inner one winning field by field.
| Parameter | Type | Description |
|---|---|---|
options | SlackPostOptions |
Returns Callable[[Effect[A, E, R]], Effect[A, E, R]]
def with_post_options[A, E, R](
options: SlackPostOptions,
) -> Callable[[Effect[A, E, R]], Effect[A, E, R]]:
"""Scoped Slack options for every write inside an effect; use with
``.pipe``. Nested calls merge, the inner one winning field by field."""
def apply(effect: Effect[A, E, R]) -> Effect[A, E, R]:
require_effect(effect, "effect")
return CurrentPostOptions().flat_map(
lambda current: effect.pipe(
fx.provide_service(CurrentPostOptions, current.merge(options))
)
)
return applyescape_markdown#
escape_markdown(text) -> strtext, such as a display name, as Markdown that shows it as typed:
no mention, link, emphasis, code or block forms from it, whatever the
post options let notify. Escape what users write before putting it into
a post's Markdown, within a line.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns str
def escape_markdown(text: str) -> str:
"""``text``, such as a display name, as Markdown that shows it as typed:
no mention, link, emphasis, code or block forms from it, whatever the
post options let notify. Escape what users write before putting it into
a post's Markdown, within a line."""
return _rich_text.escape_markdown(text.translate(_CONTROL))emoji_from_name#
emoji_from_name(name) -> str | NoneA Slack emoji name, with or without colons and with at most one skin
tone suffix (+1::skin-tone-4), as Unicode; None for a name the
table doesn't know, such as a custom emoji, and for two tones.
| Parameter | Type | Description |
|---|---|---|
name | str |
Returns str | None
def emoji_from_name(name: str) -> str | None:
"""A Slack emoji name, with or without colons and with at most one skin
tone suffix (``+1::skin-tone-4``), as Unicode; ``None`` for a name the
table doesn't know, such as a custom emoji, and for two tones."""
return _emoji.from_name(name)emoji_to_name#
emoji_to_name(emoji) -> str | NoneAn emoji as its Slack name, without colons.
| Parameter | Type | Description |
|---|---|---|
emoji | str |
Returns str | None
def emoji_to_name(emoji: str) -> str | None:
"""An emoji as its Slack name, without colons."""
return _emoji.to_name(emoji)parse_mention#
parse_mention(text) -> MentionToken | NoneReads one Slack mention token, such as <@U123>,
<!subteam^S123|@team>, <#C123|name> or <!here>, for
chat_test's checks and conformance suite.
| Parameter | Type | Description |
|---|---|---|
text | str |
Returns MentionToken | None
def parse_mention(text: str) -> MentionToken | None:
"""Reads one Slack mention token, such as ``<@U123>``,
``<!subteam^S123|@team>``, ``<#C123|name>`` or ``<!here>``, for
``chat_test``'s checks and conformance suite."""
target = _mentions.parse_token(text)
return None if target is None else MentionToken(target.kind, target.id)