GitHub

teams_chat

API reference for teams_chat.

The Microsoft Teams adapter for pyfect.chat (Effect's @effect/chat-teams/TeamsChat): the Registry layer, the Bot Framework messaging endpoint, the pure normaliser and address helpers.

  • Register a bot in Azure Bot Service with the Microsoft Teams channel, and point its messaging endpoint at layer_webhook's path.
  • The layer works in one tenant: tenant_id is the tenant the bot's app registration lives in, and activities from any other tenant are refused.
  • Buttons, modals, commands, start_thread and downloads are off until each is turned on in Unverified: their live checks against Teams have not passed yet.

Re-exports

Values

AttributeTypeDescription
PUBLIC_CLOUDCloud

The public Azure cloud: Connector smba.trafficmanager.net, Bot Framework's OpenID metadata, Entra at login.microsoftonline.com, and sharepoint.com for files. No extra redirect hosts.

Types

TypeDefinitionDescription
Credentialstype Credentials = Redacted[str] | Callable[[str], Effect[TokenGrant, ChatError]]

The bot's client secret, for client credentials against the tenant, or a provider that gets a token for a scope, for certificates or managed identities. A token is cached until five minutes before it expires.

Classes

Functions

layer

layer(*, app_id, tenant_id, credentials, cloud=PUBLIC_CLOUD, interaction_keys=(), modal_buttons=(), unverified=Unverified(), max_retry_after=None) -> Layer[TeamsChat, Never, HttpClient | Registry | Mac | Digest]

Registers the Teams adapter in the Registry, for one tenant, and provides TeamsChat.

  • post and edit without elements send the Markdown as text with textFormat: "extendedmarkdown", with every < and & as an entity, so no HTML and no <at> mention forms, and every $ outside code as \$, so Teams reads no math. Mentions notify nobody.
  • With elements, the message is one Adaptive Card and no text: TextBlock, CodeBlock and Table elements from the Markdown, then ActionSet rows. An edit may switch between the two forms.
  • limits.message_length is a budget, not Microsoft's number: 50,000 UTF-16 units of activity JSON (half the 100 KB Microsoft documents) less a fixed reservation for the card's structure, the other members and 4,000 units of mention entities. 44,288 today.
  • A button carries a signed token (an HS256 JWT) under the card data key effect, bound to the app, the tenant and the conversation. It is signed, not encrypted: anyone who sees the card can read the id and value, and a click can be sent again while the conversation exists. A dialog token also binds the user and expires after an hour.
  • The adapter's own caps: 6 elements per message, 10 fields per modal, 1,000 units of button token (about 500 ASCII characters of id plus value) and 4,000 units of dialog token for the whole modal.
  • The HttpClient must add no credentials of its own. The adapter decides per request which host gets the bot's token, turns redirects off and fails a response from any other URL than the one it sent to.
  • get_user needs in_, the thread whose roster to read; without it it fails InvalidRequest with thread_required. A user's id is the Entra object id when Teams sends one, and the Connector id (29:…, 28:…) otherwise; raw keeps the Connector record.
  • interaction_keys sign button and dialog tokens. The first signs and any verifies by its kid, so a rotation adds a key in front and drops the old one later. Each needs a distinct kid of 1 to 16 characters from [A-Za-z0-9_-] and at least 32 bytes. Without keys there are no buttons or modals.
  • modal_buttons are the ids of the buttons that open a dialog. Only these may answer OpenModal.
  • A request answered 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.

Interaction tokens use Mac and Digest (pyfect.crypto.native.layer_mac and layer_digest).

ParameterTypeDescription
app_idstr
tenant_idstr
credentialsCredentials
cloudCloud = PUBLIC_CLOUD
interaction_keysSequence[Redacted[SecretJwk]] = ()
modal_buttonsSequence[str] = ()
unverifiedUnverified = Unverified()
max_retry_afterDurationInput | None = None

Returns Layer[TeamsChat, Never, HttpClient | Registry | Mac | Digest]

def layer(
    *,
    app_id: str,
    tenant_id: str,
    credentials: Credentials,
    cloud: Cloud = PUBLIC_CLOUD,
    interaction_keys: Sequence[Redacted[SecretJwk]] = (),
    modal_buttons: Sequence[str] = (),
    unverified: Unverified = Unverified(),  # noqa: B008
    max_retry_after: DurationInput | None = None,
) -> Layer[TeamsChat, Never, HttpClient | Registry | Mac | Digest]:
    """Registers the Teams adapter in the ``Registry``, for one tenant, and
    provides ``TeamsChat``.

    - ``post`` and ``edit`` without elements send the Markdown as ``text``
      with ``textFormat: "extendedmarkdown"``, with every ``<`` and ``&`` as
      an entity, so no HTML and no ``<at>`` mention forms, and every ``$``
      outside code as ``\\$``, so Teams reads no math. Mentions notify
      nobody.
    - With elements, the message is one Adaptive Card and no ``text``:
      ``TextBlock``, ``CodeBlock`` and ``Table`` elements from the Markdown,
      then ``ActionSet`` rows. An ``edit`` may switch between the two forms.
    - ``limits.message_length`` is a budget, not Microsoft's number: 50,000
      UTF-16 units of activity JSON (half the 100 KB Microsoft documents)
      less a fixed reservation for the card's structure, the other members
      and 4,000 units of mention entities. 44,288 today.
    - A button carries a signed token (an HS256 JWT) under the card data key
      ``effect``, bound to the app, the tenant and the conversation. It is
      signed, not encrypted: anyone who sees the card can read the id and
      value, and a click can be sent again while the conversation exists.
      A dialog token also binds the user and expires after an hour.
    - The adapter's own caps: 6 elements per message, 10 fields per modal,
      1,000 units of button token (about 500 ASCII characters of id plus
      value) and 4,000 units of dialog token for the whole modal.
    - The ``HttpClient`` must add no credentials of its own. The adapter
      decides per request which host gets the bot's token, turns redirects
      off and fails a response from any other URL than the one it sent to.
    - ``get_user`` needs ``in_``, the thread whose roster to read; without it
      it fails ``InvalidRequest`` with ``thread_required``. A user's id is
      the Entra object id when Teams sends one, and the Connector id
      (``29:…``, ``28:…``) otherwise; ``raw`` keeps the Connector record.
    - ``interaction_keys`` sign button and dialog tokens. The first signs and
      any verifies by its ``kid``, so a rotation adds a key in front and
      drops the old one later. Each needs a distinct ``kid`` of 1 to 16
      characters from ``[A-Za-z0-9_-]`` and at least 32 bytes. Without keys
      there are no buttons or modals.
    - ``modal_buttons`` are the ids of the buttons that open a dialog. Only
      these may answer ``OpenModal``.
    - A request answered 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.

    Interaction tokens use ``Mac`` and ``Digest``
    (``pyfect.crypto.native.layer_mac`` and ``layer_digest``).
    """

    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, TeamsChatService]:
        http = yield from HttpClient()
        registry = yield from Registry()
        tokens = _connector.make_tokens(
            http=http,
            cloud=cloud,
            app_id=app_id,
            tenant_id=tenant_id,
            credentials=credentials,
            max_retry_after=retry_ceiling,
        )
        connector = _connector.Connector(
            http=http, cloud=cloud, token=tokens, max_retry_after=retry_ceiling
        )
        envelope = (
            None
            if not interaction_keys
            else (
                yield from _envelope.make(app_id=app_id, tenant_id=tenant_id, keys=interaction_keys)
            )
        )
        adapter = TeamsAdapter(
            connector=connector,
            http=http,
            cloud=cloud,
            app_id=app_id,
            envelope=envelope,
            unverified=unverified,
            token=tokens(cloud.connector_scope),
            modal_buttons=frozenset(modal_buttons),
        )
        yield from registry.register(adapter)
        return TeamsChatService(
            app_id=app_id,
            tenant_id=tenant_id,
            cloud=cloud,
            connector=connector,
            interactions=_interactions.Options(
                capabilities=adapter.capabilities,
                envelope=envelope,
                modal_buttons=adapter.modal_buttons,
                answer=adapter.answer,
                deadline=_interactions.DEFAULT_DEADLINE,
            ),
        )

    return _layer_effect(TeamsChat, build)

layer_webhook

layer_webhook(*, path, deadline='10 seconds', interaction_deadline=_interactions.DEFAULT_DEADLINE, max_body_bytes=_webhook.DEFAULT_MAX_BODY_BYTES, on_unhandled=_ignore) -> Layer[Never, Never, HttpRouter | TeamsChat | ChatHandler | HttpClient | Signature]

POST {path}: the bot's messaging endpoint.

  • Checks the bearer token: RS256 with a key from Bot Framework's key set that is endorsed for msteams, the cloud's issuer, the app id as audience, five minutes of skew and an exp. Then the token's serviceurl must equal the activity's serviceUrl, channelId must be msteams and the tenant the layer's. Any failure is 403. A key set that can't be read is 503.
  • A targeted activity, one only the bot can see, is classified before anything else. A targeted message takes the command path: its first word names the command and the rest is its text. One that names no registered command is acked and nothing is sent. A targeted invoke goes to the interaction handler. Any other is answered 200 and dropped. None reaches the ChatHandler, on_unhandled, or anything public.
  • Invokes go to chat_interaction.Handler within interaction_deadline (4 seconds by default). Without a Handler in context, every invoke and every targeted message gets an empty 200.
  • Other activities go through normalize to the ChatHandler: 200 when it succeeds, 503 when it fails NotAccepted or a message can't be normalised. An activity with no event goes to on_unhandled and is answered 200. Past deadline (10 seconds by default) the handler is interrupted and the answer is 503. Each 503 is logged with its reason.
  • Answers 413 past max_body_bytes (1 MiB by default), and 400 for a body that can't be read or is not a JSON object, both before the token is checked.

The metadata and the key set are fetched with redirects off, and the metadata is read up to 64 KiB. Token checks use Signature (pyfect.crypto.native.layer_signature).

ParameterTypeDescription
pathstr
deadlineDurationInput = '10 seconds'
interaction_deadlineDurationInput = _interactions.DEFAULT_DEADLINE
max_body_bytesint = _webhook.DEFAULT_MAX_BODY_BYTES
on_unhandledCallable[[JsonValue], Effect[None]] = _ignore

Returns Layer[Never, Never, HttpRouter | TeamsChat | ChatHandler | HttpClient | Signature]

def layer_webhook(
    *,
    path: str,
    deadline: DurationInput = "10 seconds",
    interaction_deadline: DurationInput = _interactions.DEFAULT_DEADLINE,
    max_body_bytes: int = _webhook.DEFAULT_MAX_BODY_BYTES,
    on_unhandled: Callable[[JsonValue], Effect[None]] = _ignore,
) -> Layer[Never, Never, HttpRouter | TeamsChat | ChatHandler | HttpClient | Signature]:
    """``POST {path}``: the bot's messaging endpoint.

    - Checks the bearer token: RS256 with a key from Bot Framework's key set
      that is endorsed for ``msteams``, the cloud's issuer, the app id as
      audience, five minutes of skew and an ``exp``. Then the token's
      ``serviceurl`` must equal the activity's ``serviceUrl``, ``channelId``
      must be ``msteams`` and the tenant the layer's. Any failure is 403. A
      key set that can't be read is 503.
    - A targeted activity, one only the bot can see, is classified before
      anything else. A targeted message takes the command path: its first
      word names the command and the rest is its ``text``. One that names no
      registered command is acked and nothing is sent. A targeted invoke
      goes to the interaction handler. Any other is answered 200 and
      dropped. None reaches the ``ChatHandler``, ``on_unhandled``, or
      anything public.
    - Invokes go to ``chat_interaction.Handler`` within
      ``interaction_deadline`` (4 seconds by default). Without a ``Handler``
      in context, every invoke and every targeted message gets an empty 200.
    - Other activities go through ``normalize`` to the ``ChatHandler``: 200
      when it succeeds, 503 when it fails ``NotAccepted`` or a message can't
      be normalised. An activity with no event goes to ``on_unhandled`` and
      is answered 200. Past ``deadline`` (10 seconds by default) the handler
      is interrupted and the answer is 503. Each 503 is logged with its
      reason.
    - Answers 413 past ``max_body_bytes`` (1 MiB by default), and 400 for a
      body that can't be read or is not a JSON object, both before the token
      is checked.

    The metadata and the key set are fetched with redirects off, and the
    metadata is read up to 64 KiB. Token checks use ``Signature``
    (``pyfect.crypto.native.layer_signature``).
    """
    max_body_size = _max_body_size(max_body_bytes)
    limit = from_input_unsafe(deadline)
    interaction_limit = from_input_unsafe(interaction_deadline)

    def register(router: HttpRouterService) -> Effect[None, Never, Any]:
        @fx.gen
        def body() -> Generator[Effect[Any, Never, Any], Any]:
            teams = yield from TeamsChat()
            internals = teams._interactions  # pyright: ignore[reportPrivateUsage]
            handler = yield from ChatHandler()
            interaction_handler = yield from service_option(Handler)
            interaction_registry = yield from service_option(InteractionRegistry)
            verify = yield from _auth.make_verifier(
                cloud=teams.cloud, app_id=teams.app_id, tenant_id=teams.tenant_id
            )
            options = _interactions.Options(
                capabilities=internals.capabilities,
                envelope=internals.envelope,
                modal_buttons=internals.modal_buttons,
                answer=internals.answer,
                deadline=interaction_limit,
            )

            def invoke(activity: JsonObject) -> Effect[_interactions.InvokeResponse]:
                if interaction_handler is None:
                    return fx.succeed(_interactions.InvokeResponse(200))
                return _interactions.invoke(activity, options).pipe(
                    fx.provide_service(Handler, interaction_handler.value)
                )

            def command(activity: JsonObject) -> Effect[None]:
                if interaction_handler is None or interaction_registry is None:
                    return fx.void
                registry = interaction_registry.value

                def is_registered(id: str) -> Effect[bool]:
                    return registry.get(id).map(
                        lambda entry: entry is not None and entry.definition.kind == "command"
                    )

                return _interactions.command(activity, options, is_registered).pipe(
                    fx.provide_service(Handler, interaction_handler.value)
                )

            webhook = _webhook.Options(
                verify=verify,
                context=Context(
                    app_id=teams.app_id,
                    connector_hosts=frozenset(teams.cloud.connector_hosts),
                    file_host_suffixes=teams.cloud.file_host_suffixes,
                ),
                handle=handler.handle,
                on_unhandled=lambda activity: _call_user(on_unhandled, cast("JsonValue", activity)),
                invoke=invoke,
                command=command,
                deadline=limit,
                max_body_size=max_body_size,
            )
            yield from router.add(
                "POST", path, lambda request: _webhook.handle_request(request, webhook)
            )

        return body

    return _router_use(register)

thread

thread(conversation_id) -> ChatThread

The address of a Teams conversation: the Bot Framework conversation id as it is. A channel thread is the channel's id followed by ;messageid={root}.

ParameterTypeDescription
conversation_idstr

Returns ChatThread

def thread(conversation_id: str) -> ChatThread:
    """The address of a Teams conversation: the Bot Framework conversation id
    as it is. A channel thread is the channel's id followed by
    ``;messageid={root}``."""
    return _address.thread(conversation_id)

parts

parts(address) -> Parts | None

The parts of a Teams address, or None for another platform's or a malformed one.

ParameterTypeDescription
addressChatThread

Returns Parts | None

def parts(address: ChatThread) -> Parts | None:
    """The parts of a Teams address, or ``None`` for another platform's or a
    malformed one."""
    return _address.parts(address)

normalize

normalize(activity, *, app_id, cloud=PUBLIC_CLOUD) -> list[ChatEvent]

The normaliser, for a consumer's own transport. It checks no token: that is the transport's job.

  • Pure: takes an activity, gives zero or more events.
  • A message gives a MessageCreated. Its text is Teams' text as delivered, with <at>…</at> for each mention, where mentions point. A channel post is addressed to the channel, a reply to the post's thread.
  • messageReaction gives a ReactionAdded or ReactionRemoved per reaction. The six types Teams documents are Unicode emoji; any other is custom:{type}.
  • A targeted activity, a card submit and anything else give none.
  • raw is the activity.
ParameterTypeDescription
activityobject
app_idstr
cloudCloud = PUBLIC_CLOUD

Returns list[ChatEvent]

def normalize(activity: object, *, app_id: str, cloud: Cloud = PUBLIC_CLOUD) -> list[ChatEvent]:
    """The normaliser, for a consumer's own transport. It checks no token:
    that is the transport's job.

    - Pure: takes an activity, gives zero or more events.
    - A ``message`` gives a ``MessageCreated``. Its ``text`` is Teams' text
      as delivered, with ``<at>…</at>`` for each mention, where ``mentions``
      point. A channel post is addressed to the channel, a reply to the
      post's thread.
    - ``messageReaction`` gives a ``ReactionAdded`` or ``ReactionRemoved``
      per reaction. The six types Teams documents are Unicode emoji; any
      other is ``custom:{type}``.
    - A targeted activity, a card submit and anything else give none.
    - ``raw`` is the activity.
    """
    return _normalize(
        activity,
        Context(
            app_id=app_id,
            connector_hosts=frozenset(cloud.connector_hosts),
            file_host_suffixes=cloud.file_host_suffixes,
        ),
    )

escape_markdown

escape_markdown(text) -> str

text, such as a display name, as Markdown that shows it as typed: no link, image, emphasis or code forms from it. Escape what users write before putting it into a post's Markdown, within a line.

ParameterTypeDescription
textstr

Returns str

def escape_markdown(text: str) -> str:
    """``text``, such as a display name, as Markdown that shows it as typed:
    no link, image, emphasis or code forms from it. Escape what users write
    before putting it into a post's Markdown, within a line."""
    return _render.escape_markdown(text)

parse_mention

parse_mention(text) -> MentionToken | None

Reads one Teams mention, <at>…</at>, for chat_test. The tag holds a display name and no id, so this is always None; a message's mentions come from its entities.

ParameterTypeDescription
textstr

Returns MentionToken | None

def parse_mention(text: str) -> MentionToken | None:
    """Reads one Teams mention, ``<at>…</at>``, for ``chat_test``. The tag
    holds a display name and no id, so this is always ``None``; a message's
    ``mentions`` come from its entities."""
    return None