Skip to content

The implementation of #1208 is different from the node-slack-sdk version #1302

@mar3mar3

Description

@mar3mar3

Original specification in node-slack-sdk
slackapi/node-slack-sdk#1476 (comment)

The implementation of the pull request created with reference to the above was different.
#1208

The following condition in node-slack-sdk is different.
Even if there is no top-level text, the warning is not displayed if there is a fallback in all attachments.
https://github.com/slackapi/node-slack-sdk/blob/bcbe7cc8753a134d73daf88ac66eec0f359c2df7/packages/web-api/src/WebClient.ts#L868

Reproducible in:

The Slack SDK version

slack-sdk==3.19.2

Python runtime version

Python 3.11.0

OS info

ProductName: macOS
ProductVersion: 12.6
BuildVersion: 21G115
Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64

Steps to reproduce:

For example...

from slack_sdk import WebClient
import os

slack_token = os.environ["SLACK_BOT_TOKEN"]
client = WebClient(token=slack_token)

attachments = [
    {
        'fallback': 'title-message-fallback',
        'color': 'red',
        'fields': [
            {
                'title': 'title',
                'value': 'message',
            }]
    }
]

result = client.chat_postMessage(
    channel="C0XXXXXX",
    attachments=attachments,
    as_user=True)

Expected result:

No warnings are displayed.

Actual result:

The following warning is displayed.

UserWarning: The top-level `text` argument is missing in the request payload for a chat.postMessage call - It’s a best practice to always provide a `text` argument when posting a message. The `text` argument is used in places where content cannot be rendered such as: system push notifications, assistive technology such as screen readers, etc.
  warnings.warn(message, UserWarning)

maybe...

the following implementation?

    # if text argument is missing, Warn the user about this.
    # However, do not warn if a fallback field exists for all attachments, since this can be substituted.
    missing_text_message  = (
        f"The top-level `text` argument is missing in the request payload for a {endpoint} call - "
        f"It's a best practice to always provide a `text` argument when posting a message. "
        f"The `text` argument is used in places where content cannot be rendered such as: "
        "system push notifications, assistive technology such as screen readers, etc."
    )

    # https://api.slack.com/reference/messaging/attachments
    # Check if the fallback field exists for all the attachments
    # Not all attachments have a fallback property; warn about this too!
    missing_fallback_message = (
        f"Additionally, the attachment-level `fallback` argument is missing in the request payload for a {endpoint} call"
        f" - To avoid this warning, it is recommended to always provide a top-level `text` argument when posting a"
        f" message. Alternatively you can provide an attachment-level `fallback` argument, though this is now considered"
        f" a legacy field (see https://api.slack.com/reference/messaging/attachments#legacy_fields for more details)."
    )

    # Additionally, specifically for attachments, there is a legacy field available at the attachment level called `fallback`
    # Even with a missing text, one can provide a `fallback` per attachment.
    # More details here: https://api.slack.com/reference/messaging/attachments#legacy_fields
    attachments = kwargs.get("attachments")
    # Note that this method does not verify attachments
    # if the value is already serialized as a single str value.
    if (
        attachments is not None
        and isinstance(attachments, list)
    ):
        if (not all(
                [isinstance(attachment, dict)
                and len(attachment.get("fallback", "").strip()) > 0 for attachment in attachments]
            )
        ):
            warnings.warn(missing_text_message, UserWarning)
            warnings.warn(missing_fallback_message, UserWarning)
    else:
        warnings.warn(missing_text_message, UserWarning)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Version: 3xbugM-T: A confirmed bug report. Issues are confirmed when the reproduction steps are documentedweb-client

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions