diff --git a/tests/api/example_card.json b/tests/api/example_card.json new file mode 100644 index 0000000..4d426b4 --- /dev/null +++ b/tests/api/example_card.json @@ -0,0 +1,109 @@ +{ + "type": "AdaptiveCard", + "body": [ + { + "type": "TextBlock", + "size": "Medium", + "weight": "Bolder", + "text": "Publish Adaptive Card schema" + }, + { + "type": "ColumnSet", + "columns": [ + { + "type": "Column", + "items": [ + { + "type": "Image", + "style": "Person", + "url": "https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg", + "size": "Small" + } + ], + "width": "auto" + }, + { + "type": "Column", + "items": [ + { + "type": "TextBlock", + "weight": "Bolder", + "text": "Matt Hidinger", + "wrap": true + }, + { + "type": "TextBlock", + "spacing": "None", + "text": "Created {{DATE(2017-02-14T06:08:39Z,SHORT)}}", + "isSubtle": true, + "wrap": true + } + ], + "width": "stretch" + } + ] + }, + { + "type": "TextBlock", + "text": "Now that we have defined the main rules and features of the format, we need to produce a schema and publish it to GitHub. The schema will be the starting point of our reference documentation.", + "wrap": true + }, + { + "type": "FactSet", + "facts": [ + { + "title": "Board:", + "value": "Adaptive Card" + }, + { + "title": "List:", + "value": "Backlog" + }, + { + "title": "Assigned to:", + "value": "Matt Hidinger" + }, + { + "title": "Due date:", + "value": "Not set" + } + ] + } + ], + "actions": [ + { + "type": "Action.ShowCard", + "title": "Set due date", + "card": { + "type": "AdaptiveCard", + "body": [ + { + "type": "Input.Date", + "id": "dueDate" + }, + { + "type": "Input.Text", + "id": "comment", + "placeholder": "Add a comment", + "isMultiline": true + } + ], + "actions": [ + { + "type": "Action.OpenUrl", + "title": "OK", + "url": "http://adaptivecards.io" + } + ], + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json" + } + }, + { + "type": "Action.OpenUrl", + "title": "View", + "url": "http://adaptivecards.io" + } + ], + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "version": "1.0" +} diff --git a/tests/api/test_messages.py b/tests/api/test_messages.py index 0615648..240fbd4 100644 --- a/tests/api/test_messages.py +++ b/tests/api/test_messages.py @@ -23,7 +23,7 @@ """ import itertools - +import json, pathlib import pytest import webexteamssdk @@ -56,6 +56,25 @@ def direct_message_by_email(api, test_people): api.messages.delete(message.id) +@pytest.fixture(scope="session") +def direct_message_by_email_with_card(api, test_people): + attachments = [] + attachment = {} + attachment['contentType'] = "application/vnd.microsoft.card.adaptive" + attachment["content"] = json.loads(open(pathlib.Path(__file__).parent / 'example_card.json').read()) + attachments.append(attachment) + person = test_people["member_added_by_email"] + message = api.messages.create( + toPersonEmail=person.emails[0], + text=create_string("Message"), + attachments=attachments + ) + + yield message + + api.messages.delete(message.id) + + @pytest.fixture(scope="session") def direct_message_by_id(api, test_people): person = test_people["member_added_by_id"] @@ -137,8 +156,8 @@ def group_room_messages(api, group_room, @pytest.fixture(scope="session") -def direct_messages(api, direct_message_by_email, direct_message_by_id): - return [direct_message_by_email, direct_message_by_id] +def direct_messages(api, direct_message_by_email, direct_message_by_id, direct_message_by_email_with_card): + return [direct_message_by_email, direct_message_by_id, direct_message_by_email_with_card] # Tests @@ -191,6 +210,10 @@ def test_create_direct_messages_by_email(direct_message_by_email): assert is_valid_message(direct_message_by_email) +def test_create_direct_messages_by_email_with_card(direct_message_by_email_with_card): + assert is_valid_message(direct_message_by_email_with_card) + + def test_create_direct_messages_by_id(direct_message_by_id): assert is_valid_message(direct_message_by_id) diff --git a/webexteamssdk/api/messages.py b/webexteamssdk/api/messages.py index 025dfae..882d766 100644 --- a/webexteamssdk/api/messages.py +++ b/webexteamssdk/api/messages.py @@ -135,7 +135,7 @@ def list(self, roomId, mentionedPeople=None, before=None, yield self._object_factory(OBJECT_TYPE, item) def create(self, roomId=None, toPersonId=None, toPersonEmail=None, - text=None, markdown=None, files=None, **request_parameters): + text=None, markdown=None, files=None, attachments=None, **request_parameters): """Post a message, and optionally a attachment, to a room. The files parameter is a list, which accepts multiple values to allow @@ -154,6 +154,9 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None, markdown(basestring): The message, in markdown format. files(`list`): A list of public URL(s) or local path(s) to files to be posted into the room. Only one file is allowed per message. + attachments(`list`): A list comprised of properly formatted button + and card data structure. This can be found at + https://docs.microsoft.com/en-us/adaptive-cards/sdk/designer **request_parameters: Additional request parameters (provides support for parameters that may be added in the future). @@ -174,6 +177,7 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None, check_type(text, basestring) check_type(markdown, basestring) check_type(files, list) + check_type(attachments, list) if files: if len(files) != 1: raise ValueError("The length of the `files` list is greater " @@ -184,6 +188,14 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None, "message.") check_type(files[0], basestring) + if attachments: + for attachment in attachments: + try: + content_type_exists = attachment['contentType'] + except Exception as e: + # ensure a valid header is loaded for cards + attachment['contentType'] = 'application/vnd.microsoft.card.adaptive' + post_data = dict_from_items_with_values( request_parameters, roomId=roomId, @@ -192,6 +204,7 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None, text=text, markdown=markdown, files=files, + attachments=attachments, ) # API request