diff --git a/integration_tests/web/test_message_metadata.py b/integration_tests/web/test_message_metadata.py index bb3a4de7b..dc2626d27 100644 --- a/integration_tests/web/test_message_metadata.py +++ b/integration_tests/web/test_message_metadata.py @@ -2,9 +2,32 @@ import os import time import unittest +import json from integration_tests.env_variable_names import SLACK_SDK_TEST_BOT_TOKEN -from slack_sdk.models.metadata import Metadata +from slack_sdk.models.metadata import ( + Metadata, + EventAndEntityMetadata, + EntityMetadata, + ExternalRef, + EntityPayload, + EntityAttributes, + EntityTitle, + TaskEntityFields, + EntityStringField, + EntityTitle, + EntityAttributes, + EntityFullSizePreview, + TaskEntityFields, + EntityTypedField, + EntityStringField, + EntityTimestampField, + EntityEditSupport, + EntityEditTextConfig, + EntityCustomField, + EntityUserIDField, + ExternalRef, +) from slack_sdk.web import WebClient @@ -125,3 +148,97 @@ def test_publishing_message_metadata_using_models(self): ) self.assertIsNone(scheduled.get("error")) self.assertIsNotNone(scheduled.get("message").get("metadata")) + + def test_publishing_entity_metadata(self): + client: WebClient = WebClient(token=self.bot_token) + new_message = client.chat_postMessage( + channel="C014KLZN9M0", + text="Message with entity metadata", + metadata={ + "entities": [ + { + "entity_type": "slack#/entities/task", + "url": "https://abc.com/123", + "external_ref": {"id": "123"}, + "entity_payload": { + "attributes": {"title": {"text": "My task"}, "product_name": "We reference only"}, + "fields": { + "due_date": {"value": "2026-06-06", "type": "slack#/types/date", "edit": {"enabled": True}}, + "created_by": {"type": "slack#/types/user", "user": {"user_id": "U014KLZE350"}}, + "date_created": {"value": 1760629278}, + }, + "custom_fields": [ + { + "label": "img", + "key": "img", + "type": "slack#/types/image", + "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg", + } + ], + }, + } + ] + }, + ) + + self.assertIsNone(new_message.get("error")) + self.assertIsNone(new_message.get("warning")) + + def test_publishing_entity_metadata_using_models(self): + # Build the metadata + + title = EntityTitle(text="My title") + full_size_preview = EntityFullSizePreview( + is_supported=True, + preview_url="https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg", + mime_type="image/jpeg", + ) + attributes = EntityAttributes(title=title, product_name="My Product", full_size_preview=full_size_preview) + description = EntityStringField( + value="Description of the task.", + long=True, + edit=EntityEditSupport(enabled=True, text=EntityEditTextConfig(min_length=5, max_length=100)), + ) + due_date = EntityTypedField(value="2026-06-06", type="slack#/types/date", edit=EntityEditSupport(enabled=True)) + created_by = EntityTypedField( + type="slack#/types/user", + user=EntityUserIDField(user_id="USLACKBOT"), + ) + date_created = EntityTimestampField(value=1762450663, type="slack#/types/timestamp") + date_updated = EntityTimestampField(value=1762450663, type="slack#/types/timestamp") + fields = TaskEntityFields( + description=description, + due_date=due_date, + created_by=created_by, + date_created=date_created, + date_updated=date_updated, + ) + custom_fields = [] + custom_fields.append( + EntityCustomField( + label="My Image", + key="my-image", + type="slack#/types/image", + image_url="https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg", + ) + ) + entity = EntityPayload(attributes=attributes, fields=fields, custom_fields=custom_fields) + + client: WebClient = WebClient(token=self.bot_token) + new_message = client.chat_postMessage( + channel="#random", + text="Message with entity metadata", + metadata=EventAndEntityMetadata( + entities=[ + EntityMetadata( + entity_type="slack#/entities/task", + external_ref=ExternalRef(id="abc123"), + url="https://myappdomain.com", + entity_payload=entity, + ) + ] + ), + ) + + self.assertIsNone(new_message.get("error")) + self.assertIsNone(new_message.get("warning")) diff --git a/slack_sdk/models/basic_objects.py b/slack_sdk/models/basic_objects.py index 339358790..4feefe3f6 100644 --- a/slack_sdk/models/basic_objects.py +++ b/slack_sdk/models/basic_objects.py @@ -40,6 +40,9 @@ def validate_json(self) -> None: if callable(method) and hasattr(method, "validator"): method() + def get_object_attribute(self, key: str): + return getattr(self, key, None) + def get_non_null_attributes(self) -> dict: """ Construct a dictionary out of non-null keys (from attributes property) @@ -57,7 +60,7 @@ def to_dict_compatible(value: Union[dict, list, object, tuple]) -> Union[dict, l return value def is_not_empty(self, key: str) -> bool: - value = getattr(self, key, None) + value = self.get_object_attribute(key) if value is None: return False @@ -75,7 +78,9 @@ def is_not_empty(self, key: str) -> bool: return value is not None return { - key: to_dict_compatible(getattr(self, key, None)) for key in sorted(self.attributes) if is_not_empty(self, key) + key: to_dict_compatible(value=self.get_object_attribute(key)) + for key in sorted(self.attributes) + if is_not_empty(self, key) } def to_dict(self, *args) -> dict: diff --git a/slack_sdk/models/metadata/__init__.py b/slack_sdk/models/metadata/__init__.py index ddf24f1ae..7e4918401 100644 --- a/slack_sdk/models/metadata/__init__.py +++ b/slack_sdk/models/metadata/__init__.py @@ -1,5 +1,5 @@ -from typing import Dict, Any -from slack_sdk.models.basic_objects import JsonObject +from typing import Dict, Any, Union, Optional, List +from slack_sdk.models.basic_objects import JsonObject, EnumValidator class Metadata(JsonObject): @@ -28,3 +28,1228 @@ def __str__(self): def __repr__(self): return self.__str__() + + +# +# Work object entity metadata +# https://docs.slack.dev/messaging/work-objects/ +# + + +"""Entity types""" +EntityType = { + "slack#/entities/task", + "slack#/entities/file", + "slack#/entities/item", + "slack#/entities/incident", + "slack#/entities/content_item", +} + + +"""Custom field types""" +CustomFieldType = { + "integer", + "string", + "array", + "boolean", + "slack#/types/date", + "slack#/types/timestamp", + "slack#/types/image", + "slack#/types/channel_id", + "slack#/types/user", + "slack#/types/entity_ref", + "slack#/types/link", + "slack#/types/email", +} + + +class ExternalRef(JsonObject): + """Reference (and optional type) used to identify an entity within the developer's system""" + + attributes = { + "id", + "type", + } + + def __init__( + self, + id: str, + type: Optional[str] = None, + **kwargs, + ): + self.id = id + self.type = type + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class FileEntitySlackFile(JsonObject): + """Slack file reference for file entities""" + + attributes = { + "id", + "type", + } + + def __init__( + self, + id: str, + type: Optional[str] = None, + **kwargs, + ): + self.id = id + self.type = type + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityIconSlackFile(JsonObject): + """Slack file reference for entity icon""" + + attributes = { + "id", + "url", + } + + def __init__( + self, + id: Optional[str] = None, + url: Optional[str] = None, + **kwargs, + ): + self.id = id + self.url = url + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityIconField(JsonObject): + """Icon field for entity attributes""" + + attributes = { + "alt_text", + "url", + "slack_file", + } + + def __init__( + self, + alt_text: str, + url: Optional[str] = None, + slack_file: Optional[Union[Dict[str, Any], EntityIconSlackFile]] = None, + **kwargs, + ): + self.alt_text = alt_text + self.url = url + self.slack_file = slack_file + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityEditSelectConfig(JsonObject): + """Select configuration for entity edit support""" + + attributes = { + "current_value", + "current_values", + "static_options", + "fetch_options_dynamically", + "min_query_length", + } + + def __init__( + self, + current_value: Optional[str] = None, + current_values: Optional[List[str]] = None, + static_options: Optional[List[Dict[str, Any]]] = None, # Option[] + fetch_options_dynamically: Optional[bool] = None, + min_query_length: Optional[int] = None, + **kwargs, + ): + self.current_value = current_value + self.current_values = current_values + self.static_options = static_options + self.fetch_options_dynamically = fetch_options_dynamically + self.min_query_length = min_query_length + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityEditNumberConfig(JsonObject): + """Number configuration for entity edit support""" + + attributes = { + "is_decimal_allowed", + "min_value", + "max_value", + } + + def __init__( + self, + is_decimal_allowed: Optional[bool] = None, + min_value: Optional[Union[int, float]] = None, + max_value: Optional[Union[int, float]] = None, + **kwargs, + ): + self.is_decimal_allowed = is_decimal_allowed + self.min_value = min_value + self.max_value = max_value + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityEditTextConfig(JsonObject): + """Text configuration for entity edit support""" + + attributes = { + "min_length", + "max_length", + } + + def __init__( + self, + min_length: Optional[int] = None, + max_length: Optional[int] = None, + **kwargs, + ): + self.min_length = min_length + self.max_length = max_length + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityEditSupport(JsonObject): + """Edit support configuration for entity fields""" + + attributes = { + "enabled", + "placeholder", + "hint", + "optional", + "select", + "number", + "text", + } + + def __init__( + self, + enabled: bool, + placeholder: Optional[Dict[str, Any]] = None, # PlainTextElement + hint: Optional[Dict[str, Any]] = None, # PlainTextElement + optional: Optional[bool] = None, + select: Optional[Union[Dict[str, Any], EntityEditSelectConfig]] = None, + number: Optional[Union[Dict[str, Any], EntityEditNumberConfig]] = None, + text: Optional[Union[Dict[str, Any], EntityEditTextConfig]] = None, + **kwargs, + ): + self.enabled = enabled + self.placeholder = placeholder + self.hint = hint + self.optional = optional + self.select = select + self.number = number + self.text = text + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityFullSizePreviewError(JsonObject): + """Error information for full-size preview""" + + attributes = { + "code", + "message", + } + + def __init__( + self, + code: str, + message: Optional[str] = None, + **kwargs, + ): + self.code = code + self.message = message + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityFullSizePreview(JsonObject): + """Full-size preview configuration for entity""" + + attributes = { + "is_supported", + "preview_url", + "mime_type", + "error", + } + + def __init__( + self, + is_supported: bool, + preview_url: Optional[str] = None, + mime_type: Optional[str] = None, + error: Optional[Union[Dict[str, Any], EntityFullSizePreviewError]] = None, + **kwargs, + ): + self.is_supported = is_supported + self.preview_url = preview_url + self.mime_type = mime_type + self.error = error + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityUserIDField(JsonObject): + """User ID field for entity""" + + attributes = { + "user_id", + } + + def __init__( + self, + user_id: str, + **kwargs, + ): + self.user_id = user_id + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityUserField(JsonObject): + """User field for entity""" + + attributes = { + "text", + "url", + "email", + "icon", + } + + def __init__( + self, + text: str, + url: Optional[str] = None, + email: Optional[str] = None, + icon: Optional[Union[Dict[str, Any], EntityIconField]] = None, + **kwargs, + ): + self.text = text + self.url = url + self.email = email + self.icon = icon + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityRefField(JsonObject): + """Entity reference field""" + + attributes = { + "entity_url", + "external_ref", + "title", + "display_type", + "icon", + } + + def __init__( + self, + entity_url: str, + external_ref: Union[Dict[str, Any], ExternalRef], + title: str, + display_type: Optional[str] = None, + icon: Optional[Union[Dict[str, Any], EntityIconField]] = None, + **kwargs, + ): + self.entity_url = entity_url + self.external_ref = external_ref + self.title = title + self.display_type = display_type + self.icon = icon + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityTypedField(JsonObject): + """Typed field for entity with various display options""" + + attributes = { + "type", + "label", + "value", + "link", + "icon", + "long", + "format", + "image_url", + "slack_file", + "alt_text", + "edit", + "tag_color", + "user", + "entity_ref", + } + + def __init__( + self, + type: str, + label: Optional[str] = None, + value: Optional[Union[str, int]] = None, + link: Optional[str] = None, + icon: Optional[Union[Dict[str, Any], EntityIconField]] = None, + long: Optional[bool] = None, + format: Optional[str] = None, + image_url: Optional[str] = None, + slack_file: Optional[Dict[str, Any]] = None, + alt_text: Optional[str] = None, + edit: Optional[Union[Dict[str, Any], EntityEditSupport]] = None, + tag_color: Optional[str] = None, + user: Optional[Union[Dict[str, Any], EntityUserIDField, EntityUserField]] = None, + entity_ref: Optional[Union[Dict[str, Any], EntityRefField]] = None, + **kwargs, + ): + self.type = type + self.label = label + self.value = value + self.link = link + self.icon = icon + self.long = long + self.format = format + self.image_url = image_url + self.slack_file = slack_file + self.alt_text = alt_text + self.edit = edit + self.tag_color = tag_color + self.user = user + self.entity_ref = entity_ref + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityStringField(JsonObject): + """String field for entity""" + + attributes = { + "value", + "label", + "format", + "link", + "icon", + "long", + "type", + "tag_color", + "edit", + } + + def __init__( + self, + value: str, + label: Optional[str] = None, + format: Optional[str] = None, + link: Optional[str] = None, + icon: Optional[Union[Dict[str, Any], EntityIconField]] = None, + long: Optional[bool] = None, + type: Optional[str] = None, + tag_color: Optional[str] = None, + edit: Optional[Union[Dict[str, Any], EntityEditSupport]] = None, + **kwargs, + ): + self.value = value + self.label = label + self.format = format + self.link = link + self.icon = icon + self.long = long + self.type = type + self.tag_color = tag_color + self.edit = edit + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityTimestampField(JsonObject): + """Timestamp field for entity""" + + attributes = { + "value", + "label", + "type", + "edit", + } + + def __init__( + self, + value: int, + label: Optional[str] = None, + type: Optional[str] = None, + edit: Optional[Union[Dict[str, Any], EntityEditSupport]] = None, + **kwargs, + ): + self.value = value + self.label = label + self.type = type + self.edit = edit + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityImageField(JsonObject): + """Image field for entity""" + + attributes = { + "alt_text", + "label", + "image_url", + "slack_file", + "title", + "type", + } + + def __init__( + self, + alt_text: str, + label: Optional[str] = None, + image_url: Optional[str] = None, + slack_file: Optional[Dict[str, Any]] = None, + title: Optional[str] = None, + type: Optional[str] = None, + **kwargs, + ): + self.alt_text = alt_text + self.label = label + self.image_url = image_url + self.slack_file = slack_file + self.title = title + self.type = type + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityBooleanCheckboxField(JsonObject): + """Boolean checkbox properties""" + + attributes = {"type", "text", "description"} + + def __init__( + self, + type: str, + text: str, + description: Optional[str], + **kwargs, + ): + self.type = type + self.text = text + self.description = description + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityBooleanTextField(JsonObject): + """Boolean text properties""" + + attributes = {"type", "true_text", "false_text", "true_description", "false_description"} + + def __init__( + self, + type: str, + true_text: str, + false_text: str, + true_description: Optional[str], + false_description: Optional[str], + **kwargs, + ): + self.type = type + self.true_text = (true_text,) + self.false_text = (false_text,) + self.true_description = (true_description,) + self.false_description = (false_description,) + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityArrayItemField(JsonObject): + """Array item field for entity (similar to EntityTypedField but with optional type)""" + + attributes = { + "type", + "label", + "value", + "link", + "icon", + "long", + "format", + "image_url", + "slack_file", + "alt_text", + "edit", + "tag_color", + "user", + "entity_ref", + } + + def __init__( + self, + type: Optional[str] = None, + label: Optional[str] = None, + value: Optional[Union[str, int]] = None, + link: Optional[str] = None, + icon: Optional[Union[Dict[str, Any], EntityIconField]] = None, + long: Optional[bool] = None, + format: Optional[str] = None, + image_url: Optional[str] = None, + slack_file: Optional[Dict[str, Any]] = None, + alt_text: Optional[str] = None, + edit: Optional[Union[Dict[str, Any], EntityEditSupport]] = None, + tag_color: Optional[str] = None, + user: Optional[Union[Dict[str, Any], EntityUserIDField, EntityUserField]] = None, + entity_ref: Optional[Union[Dict[str, Any], EntityRefField]] = None, + **kwargs, + ): + self.type = type + self.label = label + self.value = value + self.link = link + self.icon = icon + self.long = long + self.format = format + self.image_url = image_url + self.slack_file = slack_file + self.alt_text = alt_text + self.edit = edit + self.tag_color = tag_color + self.user = user + self.entity_ref = entity_ref + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityCustomField(JsonObject): + """Custom field for entity with flexible types""" + + attributes = { + "label", + "key", + "type", + "value", + "link", + "icon", + "long", + "format", + "image_url", + "slack_file", + "alt_text", + "tag_color", + "edit", + "item_type", + "user", + "entity_ref", + "boolean", + } + + def __init__( + self, + label: str, + key: str, + type: str, + value: Optional[Union[str, int, List[Union[Dict[str, Any], EntityArrayItemField]]]] = None, + link: Optional[str] = None, + icon: Optional[Union[Dict[str, Any], EntityIconField]] = None, + long: Optional[bool] = None, + format: Optional[str] = None, + image_url: Optional[str] = None, + slack_file: Optional[Dict[str, Any]] = None, + alt_text: Optional[str] = None, + tag_color: Optional[str] = None, + edit: Optional[Union[Dict[str, Any], EntityEditSupport]] = None, + item_type: Optional[str] = None, + user: Optional[Union[Dict[str, Any], EntityUserIDField, EntityUserField]] = None, + entity_ref: Optional[Union[Dict[str, Any], EntityRefField]] = None, + boolean: Optional[Union[Dict[str, Any], EntityBooleanCheckboxField, EntityBooleanTextField]] = None, + **kwargs, + ): + self.label = label + self.key = key + self.type = type + self.value = value + self.link = link + self.icon = icon + self.long = long + self.format = format + self.image_url = image_url + self.slack_file = slack_file + self.alt_text = alt_text + self.tag_color = tag_color + self.edit = edit + self.item_type = item_type + self.user = user + self.entity_ref = entity_ref + self.boolean = boolean + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + @EnumValidator("type", CustomFieldType) + def type_valid(self): + return self.type is None or self.type in CustomFieldType + + +class FileEntityFields(JsonObject): + """Fields specific to file entities""" + + attributes = { + "preview", + "created_by", + "date_created", + "date_updated", + "last_modified_by", + "file_size", + "mime_type", + "full_size_preview", + } + + def __init__( + self, + preview: Optional[Union[Dict[str, Any], EntityImageField]] = None, + created_by: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + date_created: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + date_updated: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + last_modified_by: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + file_size: Optional[Union[Dict[str, Any], EntityStringField]] = None, + mime_type: Optional[Union[Dict[str, Any], EntityStringField]] = None, + full_size_preview: Optional[Union[Dict[str, Any], EntityFullSizePreview]] = None, + **kwargs, + ): + self.preview = preview + self.created_by = created_by + self.date_created = date_created + self.date_updated = date_updated + self.last_modified_by = last_modified_by + self.file_size = file_size + self.mime_type = mime_type + self.full_size_preview = full_size_preview + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class TaskEntityFields(JsonObject): + """Fields specific to task entities""" + + attributes = { + "description", + "created_by", + "date_created", + "date_updated", + "assignee", + "status", + "due_date", + "priority", + } + + def __init__( + self, + description: Optional[Union[Dict[str, Any], EntityStringField]] = None, + created_by: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + date_created: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + date_updated: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + assignee: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + status: Optional[Union[Dict[str, Any], EntityStringField]] = None, + due_date: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + priority: Optional[Union[Dict[str, Any], EntityStringField]] = None, + **kwargs, + ): + self.description = description + self.created_by = created_by + self.date_created = date_created + self.date_updated = date_updated + self.assignee = assignee + self.status = status + self.due_date = due_date + self.priority = priority + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class IncidentEntityFields(JsonObject): + """Fields specific to incident entities""" + + attributes = { + "status", + "priority", + "urgency", + "created_by", + "assigned_to", + "date_created", + "date_updated", + "description", + "service", + } + + def __init__( + self, + status: Optional[Union[Dict[str, Any], EntityStringField]] = None, + priority: Optional[Union[Dict[str, Any], EntityStringField]] = None, + urgency: Optional[Union[Dict[str, Any], EntityStringField]] = None, + created_by: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + assigned_to: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + date_created: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + date_updated: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + description: Optional[Union[Dict[str, Any], EntityStringField]] = None, + service: Optional[Union[Dict[str, Any], EntityStringField]] = None, + **kwargs, + ): + self.status = status + self.priority = priority + self.urgency = urgency + self.created_by = created_by + self.assigned_to = assigned_to + self.date_created = date_created + self.date_updated = date_updated + self.description = description + self.service = service + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class ContentItemEntityFields(JsonObject): + """Fields specific to content item entities""" + + attributes = { + "preview", + "description", + "created_by", + "date_created", + "date_updated", + "last_modified_by", + } + + def __init__( + self, + preview: Optional[Union[Dict[str, Any], EntityImageField]] = None, + description: Optional[Union[Dict[str, Any], EntityStringField]] = None, + created_by: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + date_created: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + date_updated: Optional[Union[Dict[str, Any], EntityTimestampField]] = None, + last_modified_by: Optional[Union[Dict[str, Any], EntityTypedField]] = None, + **kwargs, + ): + self.preview = preview + self.description = description + self.created_by = created_by + self.date_created = date_created + self.date_updated = date_updated + self.last_modified_by = last_modified_by + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityActionProcessingState(JsonObject): + """Processing state configuration for entity action button""" + + attributes = { + "enabled", + "interstitial_text", + } + + def __init__( + self, + enabled: bool, + interstitial_text: Optional[str] = None, + **kwargs, + ): + self.enabled = enabled + self.interstitial_text = interstitial_text + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityActionButton(JsonObject): + """Action button for entity""" + + attributes = { + "text", + "action_id", + "value", + "style", + "url", + "accessibility_label", + "processing_state", + } + + def __init__( + self, + text: str, + action_id: str, + value: Optional[str] = None, + style: Optional[str] = None, + url: Optional[str] = None, + accessibility_label: Optional[str] = None, + processing_state: Optional[Union[Dict[str, Any], EntityActionProcessingState]] = None, + **kwargs, + ): + self.text = text + self.action_id = action_id + self.value = value + self.style = style + self.url = url + self.accessibility_label = accessibility_label + self.processing_state = processing_state + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityTitle(JsonObject): + """Title for entity attributes""" + + attributes = { + "text", + "edit", + } + + def __init__( + self, + text: str, + edit: Optional[Union[Dict[str, Any], EntityEditSupport]] = None, + **kwargs, + ): + self.text = text + self.edit = edit + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityAttributes(JsonObject): + """Attributes for an entity""" + + attributes = { + "title", + "display_type", + "display_id", + "product_icon", + "product_name", + "locale", + "full_size_preview", + "metadata_last_modified", + } + + def __init__( + self, + title: Union[Dict[str, Any], EntityTitle], + display_type: Optional[str] = None, + display_id: Optional[str] = None, + product_icon: Optional[Union[Dict[str, Any], EntityIconField]] = None, + product_name: Optional[str] = None, + locale: Optional[str] = None, + full_size_preview: Optional[Union[Dict[str, Any], EntityFullSizePreview]] = None, + metadata_last_modified: Optional[int] = None, + **kwargs, + ): + self.title = title + self.display_type = display_type + self.display_id = display_id + self.product_icon = product_icon + self.product_name = product_name + self.locale = locale + self.full_size_preview = full_size_preview + self.metadata_last_modified = metadata_last_modified + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityActions(JsonObject): + """Actions configuration for entity""" + + attributes = { + "primary_actions", + "overflow_actions", + } + + def __init__( + self, + primary_actions: Optional[List[Union[Dict[str, Any], EntityActionButton]]] = None, + overflow_actions: Optional[List[Union[Dict[str, Any], EntityActionButton]]] = None, + **kwargs, + ): + self.primary_actions = primary_actions + self.overflow_actions = overflow_actions + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityPayload(JsonObject): + """Payload schema for an entity""" + + attributes = { + "attributes", + "fields", + "custom_fields", + "slack_file", + "display_order", + "actions", + } + + def __init__( + self, + attributes: Union[Dict[str, Any], EntityAttributes], + fields: Optional[ + Union[Dict[str, Any], ContentItemEntityFields, FileEntityFields, IncidentEntityFields, TaskEntityFields] + ] = None, + custom_fields: Optional[List[Union[Dict[str, Any], EntityCustomField]]] = None, + slack_file: Optional[Union[Dict[str, Any], FileEntitySlackFile]] = None, + display_order: Optional[List[str]] = None, + actions: Optional[Union[Dict[str, Any], EntityActions]] = None, + **kwargs, + ): + # Store entity attributes data with a different internal name to avoid + # shadowing the class-level 'attributes' set used for JSON serialization + self._entity_attributes = attributes + self.fields = fields + self.custom_fields = custom_fields + self.slack_file = slack_file + self.display_order = display_order + self.actions = actions + self.additional_attributes = kwargs + + @property + def entity_attributes(self) -> Union[Dict[str, Any], EntityAttributes]: + """Get the entity attributes data. + + Note: Use this property to access the attributes data. The class-level + 'attributes' is reserved for the JSON serialization schema. + """ + return self._entity_attributes + + @entity_attributes.setter + def entity_attributes(self, value: Union[Dict[str, Any], EntityAttributes]): + """Set the entity attributes data.""" + self._entity_attributes = value + + def get_object_attribute(self, key: str): + if key == "attributes": + return self._entity_attributes + else: + return getattr(self, key, None) + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + +class EntityMetadata(JsonObject): + """Work object entity metadata + + https://docs.slack.dev/messaging/work-objects/ + """ + + attributes = { + "entity_type", + "entity_payload", + "external_ref", + "url", + "app_unfurl_url", + } + + def __init__( + self, + entity_type: str, + entity_payload: Union[Dict[str, Any], EntityPayload], + external_ref: Union[Dict[str, Any], ExternalRef], + url: str, + app_unfurl_url: Optional[str] = None, + **kwargs, + ): + self.entity_type = entity_type + self.entity_payload = entity_payload + self.external_ref = external_ref + self.url = url + self.app_unfurl_url = app_unfurl_url + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() + + @EnumValidator("entity_type", EntityType) + def entity_type_valid(self): + return self.entity_type is None or self.entity_type in EntityType + + +class EventAndEntityMetadata(JsonObject): + """Message metadata with entities + + https://docs.slack.dev/messaging/message-metadata/ + https://docs.slack.dev/messaging/work-objects/ + """ + + attributes = {"event_type", "event_payload", "entities"} + + def __init__( + self, + event_type: Optional[str] = None, + event_payload: Optional[Dict[str, Any]] = None, + entities: Optional[List[Union[Dict[str, Any], EntityMetadata]]] = None, + **kwargs, + ): + self.event_type = event_type + self.event_payload = event_payload + self.entities = entities + self.additional_attributes = kwargs + + def __str__(self): + return str(self.get_non_null_attributes()) + + def __repr__(self): + return self.__str__() diff --git a/slack_sdk/web/async_client.py b/slack_sdk/web/async_client.py index 7b82a0923..050e93294 100644 --- a/slack_sdk/web/async_client.py +++ b/slack_sdk/web/async_client.py @@ -22,7 +22,7 @@ from ..models.attachments import Attachment from ..models.blocks import Block -from ..models.metadata import Metadata +from ..models.metadata import Metadata, EventAndEntityMetadata from .async_base_client import AsyncBaseClient, AsyncSlackResponse from .internal_utils import ( _parse_web_class_objects, @@ -2769,7 +2769,7 @@ async def chat_postMessage( link_names: Optional[bool] = None, username: Optional[str] = None, parse: Optional[str] = None, # none, full - metadata: Optional[Union[Dict, Metadata]] = None, + metadata: Optional[Union[Dict, Metadata, EventAndEntityMetadata]] = None, markdown_text: Optional[str] = None, **kwargs, ) -> AsyncSlackResponse: @@ -2998,6 +2998,7 @@ async def chat_unfurl( source: Optional[str] = None, unfurl_id: Optional[str] = None, unfurls: Optional[Dict[str, Dict]] = None, # or user_auth_* + metadata: Optional[Union[Dict, EventAndEntityMetadata]] = None, user_auth_blocks: Optional[Union[str, Sequence[Union[Dict, Block]]]] = None, user_auth_message: Optional[str] = None, user_auth_required: Optional[bool] = None, @@ -3014,6 +3015,7 @@ async def chat_unfurl( "source": source, "unfurl_id": unfurl_id, "unfurls": unfurls, + "metadata": metadata, "user_auth_blocks": user_auth_blocks, "user_auth_message": user_auth_message, "user_auth_required": user_auth_required, @@ -3645,6 +3647,29 @@ async def emoji_list( kwargs.update({"include_categories": include_categories}) return await self.api_call("emoji.list", http_verb="GET", params=kwargs) + async def entity_presentDetails( + self, + trigger_id: str, + metadata: Optional[dict] = None, + user_auth_required: Optional[bool] = None, + user_auth_url: Optional[str] = None, + error: Optional[Dict[str, str]] = None, + **kwargs, + ) -> AsyncSlackResponse: + """Provides entity details for the flexpane. + https://docs.slack.dev/reference/methods/entity.presentDetails/ + """ + kwargs.update({"trigger_id": trigger_id}) + if metadata is not None: + kwargs.update({"metadata": metadata}) + if user_auth_required is not None: + kwargs.update({"user_auth_required": user_auth_required}) + if user_auth_url is not None: + kwargs.update({"user_auth_url": user_auth_url}) + if error is not None: + kwargs.update({"error": error}) + return await self.api_call("entity.presentDetails", json=kwargs) + async def files_comments_delete( self, *, diff --git a/slack_sdk/web/client.py b/slack_sdk/web/client.py index 8410ffee2..830f481d0 100644 --- a/slack_sdk/web/client.py +++ b/slack_sdk/web/client.py @@ -12,7 +12,7 @@ from ..models.attachments import Attachment from ..models.blocks import Block -from ..models.metadata import Metadata +from ..models.metadata import Metadata, EventAndEntityMetadata from .base_client import BaseClient, SlackResponse from .internal_utils import ( _parse_web_class_objects, @@ -2759,7 +2759,7 @@ def chat_postMessage( link_names: Optional[bool] = None, username: Optional[str] = None, parse: Optional[str] = None, # none, full - metadata: Optional[Union[Dict, Metadata]] = None, + metadata: Optional[Union[Dict, Metadata, EventAndEntityMetadata]] = None, markdown_text: Optional[str] = None, **kwargs, ) -> SlackResponse: @@ -2988,6 +2988,7 @@ def chat_unfurl( source: Optional[str] = None, unfurl_id: Optional[str] = None, unfurls: Optional[Dict[str, Dict]] = None, # or user_auth_* + metadata: Optional[Union[Dict, EventAndEntityMetadata]] = None, user_auth_blocks: Optional[Union[str, Sequence[Union[Dict, Block]]]] = None, user_auth_message: Optional[str] = None, user_auth_required: Optional[bool] = None, @@ -3004,6 +3005,7 @@ def chat_unfurl( "source": source, "unfurl_id": unfurl_id, "unfurls": unfurls, + "metadata": metadata, "user_auth_blocks": user_auth_blocks, "user_auth_message": user_auth_message, "user_auth_required": user_auth_required, @@ -3635,6 +3637,29 @@ def emoji_list( kwargs.update({"include_categories": include_categories}) return self.api_call("emoji.list", http_verb="GET", params=kwargs) + def entity_presentDetails( + self, + trigger_id: str, + metadata: Optional[dict] = None, + user_auth_required: Optional[bool] = None, + user_auth_url: Optional[str] = None, + error: Optional[Dict[str, str]] = None, + **kwargs, + ) -> SlackResponse: + """Provides entity details for the flexpane. + https://docs.slack.dev/reference/methods/entity.presentDetails/ + """ + kwargs.update({"trigger_id": trigger_id}) + if metadata is not None: + kwargs.update({"metadata": metadata}) + if user_auth_required is not None: + kwargs.update({"user_auth_required": user_auth_required}) + if user_auth_url is not None: + kwargs.update({"user_auth_url": user_auth_url}) + if error is not None: + kwargs.update({"error": error}) + return self.api_call("entity.presentDetails", json=kwargs) + def files_comments_delete( self, *, diff --git a/slack_sdk/web/internal_utils.py b/slack_sdk/web/internal_utils.py index fadda929f..57afc6be4 100644 --- a/slack_sdk/web/internal_utils.py +++ b/slack_sdk/web/internal_utils.py @@ -17,7 +17,7 @@ from slack_sdk.errors import SlackRequestError from slack_sdk.models.attachments import Attachment from slack_sdk.models.blocks import Block -from slack_sdk.models.metadata import Metadata +from slack_sdk.models.metadata import Metadata, EventAndEntityMetadata, EntityMetadata def convert_bool_to_0_or_1(params: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]: @@ -187,13 +187,17 @@ def _build_req_args( def _parse_web_class_objects(kwargs) -> None: - def to_dict(obj: Union[Dict, Block, Attachment, Metadata]): + def to_dict(obj: Union[Dict, Block, Attachment, Metadata, EventAndEntityMetadata, EntityMetadata]): if isinstance(obj, Block): return obj.to_dict() if isinstance(obj, Attachment): return obj.to_dict() if isinstance(obj, Metadata): return obj.to_dict() + if isinstance(obj, EventAndEntityMetadata): + return obj.to_dict() + if isinstance(obj, EntityMetadata): + return obj.to_dict() return obj for blocks_name in ["blocks", "user_auth_blocks"]: @@ -208,7 +212,7 @@ def to_dict(obj: Union[Dict, Block, Attachment, Metadata]): kwargs.update({"attachments": dict_attachments}) metadata = kwargs.get("metadata", None) - if metadata is not None and isinstance(metadata, Metadata): + if metadata is not None and (isinstance(metadata, Metadata) or isinstance(metadata, EventAndEntityMetadata)): kwargs.update({"metadata": to_dict(metadata)}) diff --git a/slack_sdk/web/legacy_client.py b/slack_sdk/web/legacy_client.py index 88c0fcf1a..cce4bd1ad 100644 --- a/slack_sdk/web/legacy_client.py +++ b/slack_sdk/web/legacy_client.py @@ -23,7 +23,7 @@ from ..models.attachments import Attachment from ..models.blocks import Block -from ..models.metadata import Metadata +from ..models.metadata import Metadata, EventAndEntityMetadata from .legacy_base_client import LegacyBaseClient, SlackResponse from .internal_utils import ( _parse_web_class_objects, @@ -2770,7 +2770,7 @@ def chat_postMessage( link_names: Optional[bool] = None, username: Optional[str] = None, parse: Optional[str] = None, # none, full - metadata: Optional[Union[Dict, Metadata]] = None, + metadata: Optional[Union[Dict, Metadata, EventAndEntityMetadata]] = None, markdown_text: Optional[str] = None, **kwargs, ) -> Union[Future, SlackResponse]: @@ -2936,6 +2936,7 @@ def chat_unfurl( source: Optional[str] = None, unfurl_id: Optional[str] = None, unfurls: Optional[Dict[str, Dict]] = None, # or user_auth_* + metadata: Optional[Union[Dict, EventAndEntityMetadata]] = None, user_auth_blocks: Optional[Union[str, Sequence[Union[Dict, Block]]]] = None, user_auth_message: Optional[str] = None, user_auth_required: Optional[bool] = None, @@ -2952,6 +2953,7 @@ def chat_unfurl( "source": source, "unfurl_id": unfurl_id, "unfurls": unfurls, + "metadata": metadata, "user_auth_blocks": user_auth_blocks, "user_auth_message": user_auth_message, "user_auth_required": user_auth_required, @@ -3583,6 +3585,29 @@ def emoji_list( kwargs.update({"include_categories": include_categories}) return self.api_call("emoji.list", http_verb="GET", params=kwargs) + def entity_presentDetails( + self, + trigger_id: str, + metadata: Optional[dict] = None, + user_auth_required: Optional[bool] = None, + user_auth_url: Optional[str] = None, + error: Optional[Dict[str, str]] = None, + **kwargs, + ) -> Union[Future, SlackResponse]: + """Provides entity details for the flexpane. + https://docs.slack.dev/reference/methods/entity.presentDetails/ + """ + kwargs.update({"trigger_id": trigger_id}) + if metadata is not None: + kwargs.update({"metadata": metadata}) + if user_auth_required is not None: + kwargs.update({"user_auth_required": user_auth_required}) + if user_auth_url is not None: + kwargs.update({"user_auth_url": user_auth_url}) + if error is not None: + kwargs.update({"error": error}) + return self.api_call("entity.presentDetails", json=kwargs) + def files_comments_delete( self, *, diff --git a/tests/slack_sdk/models/test_metadata.py b/tests/slack_sdk/models/test_metadata.py new file mode 100644 index 000000000..14635c661 --- /dev/null +++ b/tests/slack_sdk/models/test_metadata.py @@ -0,0 +1,307 @@ +import unittest + +from slack_sdk.models.metadata import ( + EventAndEntityMetadata, + EntityMetadata, + ExternalRef, + FileEntitySlackFile, + EntityIconField, + EntityEditTextConfig, + EntityEditSupport, + EntityFullSizePreviewError, + EntityFullSizePreview, + EntityUserIDField, + EntityUserField, + EntityTypedField, + EntityStringField, + EntityTimestampField, + EntityImageField, + EntityCustomField, + FileEntityFields, + TaskEntityFields, + EntityActionButton, + EntityTitle, + EntityAttributes, + EntityActions, + EntityPayload, +) + + +class EntityMetadataTests(unittest.TestCase): + maxDiff = None + + # ============================================================================ + # Entity JSON + # ============================================================================ + + task_entity_json = { + "app_unfurl_url": "https://myappdomain.com/123?myquery=param", + "entity_type": "slack#/entities/task", + "url": "https://myappdomain.com/123", + "external_ref": {"id": "123"}, + "entity_payload": { + "attributes": { + "title": {"text": "My Title"}, + "display_type": "Incident", + "display_id": "123", + "product_name": "My Product", + }, + "fields": { + "date_created": {"value": 1741164235}, + "status": {"value": "In Progress"}, + "description": { + "value": "My Description", + "long": True, + "edit": {"enabled": True, "text": {"min_length": 5, "max_length": 100}}, + }, + "due_date": {"value": "2026-06-06", "type": "slack#/types/date"}, + "created_by": {"type": "slack#/types/user", "user": {"user_id": "USLACKBOT"}}, + }, + "custom_fields": [ + { + "label": "My Users", + "key": "my-users", + "type": "array", + "item_type": "slack#/types/user", + "value": [ + {"type": "slack#/types/user", "user": {"user_id": "USLACKBOT"}}, + { + "type": "slack#/types/user", + "user": { + "text": "John Smith", + "email": "j@example.com", + "icon": {"alt_text": "Avatar", "url": "https://my-hosted-icon.com"}, + }, + }, + ], + } + ], + }, + } + + file_entity_json = { + "app_unfurl_url": "https://myappdomain.com/file/456?view=preview", + "entity_type": "slack#/entities/file", + "url": "https://myappdomain.com/file/456", + "external_ref": {"id": "456", "type": "DOC"}, + "entity_payload": { + "attributes": { + "title": {"text": "Q4 Product Roadmap"}, + "display_type": "PDF Document", + "display_id": "DOC-456", + "product_icon": {"alt_text": "Product Logo", "url": "https://myappdomain.com/icons/logo.png"}, + "product_name": "FileVault Pro", + "locale": "en-US", + "full_size_preview": { + "is_supported": True, + "preview_url": "https://myappdomain.com/previews/456/full.png", + "mime_type": "image/png", + }, + }, + "fields": { + "preview": { + "alt_text": "Document preview thumbnail", + "label": "Preview", + "image_url": "https://myappdomain.com/previews/456/thumb.png", + "type": "slack#/types/image", + }, + "date_created": {"value": 1709554321, "type": "slack#/types/timestamp"}, + "mime_type": {"value": "application/pdf"}, + }, + "slack_file": {"id": "F123ABC456", "type": "pdf"}, + "display_order": ["date_created", "mime_type", "preview"], + "actions": { + "primary_actions": [ + { + "text": "Open", + "action_id": "open_file", + "value": "456", + "style": "primary", + "url": "https://myappdomain.com/file/456/view", + } + ], + "overflow_actions": [{"text": "Delete", "action_id": "delete_file", "value": "456", "style": "danger"}], + }, + }, + } + + # ============================================================================ + # Methods returning re-usable metadata components + # ============================================================================ + + def attributes(self): + return EntityAttributes( + title=EntityTitle(text="My Title"), + product_name="My Product", + display_type="Incident", + display_id="123", + ) + + def sample_file_attributes(self): + return EntityAttributes( + title=EntityTitle(text="Q4 Product Roadmap"), + display_type="PDF Document", + display_id="DOC-456", + product_icon=EntityIconField(alt_text="Product Logo", url="https://myappdomain.com/icons/logo.png"), + product_name="FileVault Pro", + locale="en-US", + full_size_preview=EntityFullSizePreview( + is_supported=True, preview_url="https://myappdomain.com/previews/456/full.png", mime_type="image/png" + ), + ) + + def user_array_custom_field(self): + return EntityCustomField( + label="My Users", + key="my-users", + type="array", + item_type="slack#/types/user", + value=[ + EntityTypedField(type="slack#/types/user", user=EntityUserIDField(user_id="USLACKBOT")), + EntityTypedField( + type="slack#/types/user", + user=EntityUserField( + text="John Smith", + email="j@example.com", + icon=EntityIconField(alt_text="Avatar", url="https://my-hosted-icon.com"), + ), + ), + ], + ) + + def task_fields(self): + return TaskEntityFields( + date_created=EntityTimestampField(value=1741164235), + status=EntityStringField(value="In Progress"), + description=EntityStringField( + value="My Description", + long=True, + edit=EntityEditSupport(enabled=True, text=EntityEditTextConfig(min_length=5, max_length=100)), + ), + due_date=EntityTypedField(value="2026-06-06", type="slack#/types/date"), + created_by=EntityTypedField( + type="slack#/types/user", + user=EntityUserIDField(user_id="USLACKBOT"), + ), + ) + + def file_fields(self): + return FileEntityFields( + preview=EntityImageField( + type="slack#/types/image", + alt_text="Document preview thumbnail", + label="Preview", + image_url="https://myappdomain.com/previews/456/thumb.png", + ), + date_created=EntityTimestampField(value=1709554321, type="slack#/types/timestamp"), + mime_type=EntityStringField(value="application/pdf"), + ) + + def supported_full_size_preview(self): + return EntityFullSizePreview( + is_supported=True, preview_url="https://example.com/preview.jpg", mime_type="image/jpeg" + ) + + def sample_file_actions(self): + return EntityActions( + primary_actions=[ + EntityActionButton( + text="Open", + action_id="open_file", + value="456", + style="primary", + url="https://myappdomain.com/file/456/view", + ) + ], + overflow_actions=[EntityActionButton(text="Delete", action_id="delete_file", value="456", style="danger")], + ) + + # ============================================================================ + # Tests + # ============================================================================ + + def test_entity_full_size_preview_error(self): + error = EntityFullSizePreviewError(code="not_found", message="File not found") + self.assertDictEqual(error.to_dict(), {"code": "not_found", "message": "File not found"}) + + def test_entity_full_size_preview_with_error(self): + preview = EntityFullSizePreview( + is_supported=False, error=EntityFullSizePreviewError(code="invalid_format", message="File not found") + ) + result = preview.to_dict() + self.assertFalse(result["is_supported"]) + self.assertIn("error", result) + + def test_attributes(self): + self.assertDictEqual( + self.attributes().to_dict(), + self.task_entity_json["entity_payload"]["attributes"], + ) + + def test_sample_file_attributes(self): + self.assertDictEqual( + self.sample_file_attributes().to_dict(), + self.file_entity_json["entity_payload"]["attributes"], + ) + + def test_array_custom_field(self): + self.assertDictEqual( + self.user_array_custom_field().to_dict(), + self.task_entity_json["entity_payload"]["custom_fields"][0], + ) + + def test_task_fields(self): + self.assertDictEqual( + self.task_fields().to_dict(), + self.task_entity_json["entity_payload"]["fields"], + ) + + def test_file_fields(self): + self.assertDictEqual( + self.file_fields().to_dict(), + self.file_entity_json["entity_payload"]["fields"], + ) + + def test_sample_file_actions(self): + self.assertDictEqual( + self.sample_file_actions().to_dict(), + self.file_entity_json["entity_payload"]["actions"], + ) + + def test_complete_task_entity_metadata(self): + entity_metadata = EventAndEntityMetadata( + entities=[ + EntityMetadata( + entity_type="slack#/entities/task", + external_ref=ExternalRef(id="123"), + url="https://myappdomain.com/123", + app_unfurl_url="https://myappdomain.com/123?myquery=param", + entity_payload=EntityPayload( + attributes=self.attributes(), + fields=self.task_fields(), + custom_fields=[self.user_array_custom_field()], + ), + ) + ] + ) + self.assertDictEqual(entity_metadata.to_dict(), {"entities": [self.task_entity_json]}) + + def test_complete_file_entity_metadata(self): + entity_metadata = EventAndEntityMetadata( + entities=[ + EntityMetadata( + entity_type="slack#/entities/file", + external_ref=ExternalRef(id="456", type="DOC"), + url="https://myappdomain.com/file/456", + app_unfurl_url="https://myappdomain.com/file/456?view=preview", + entity_payload=EntityPayload( + attributes=self.sample_file_attributes(), + fields=self.file_fields(), + slack_file=FileEntitySlackFile(id="F123ABC456", type="pdf"), + display_order=["date_created", "mime_type", "preview"], + actions=self.sample_file_actions(), + ), + ) + ] + ) + self.assertDictEqual(entity_metadata.to_dict(), {"entities": [self.file_entity_json]})