Skip to content

Commit efa82c5

Browse files
authored
Merge pull request #90 from bradh11/adaptive_card_support
Adaptive card support
2 parents b9ac306 + ac337fa commit efa82c5

File tree

3 files changed

+149
-4
lines changed

3 files changed

+149
-4
lines changed

tests/api/example_card.json

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"type": "AdaptiveCard",
3+
"body": [
4+
{
5+
"type": "TextBlock",
6+
"size": "Medium",
7+
"weight": "Bolder",
8+
"text": "Publish Adaptive Card schema"
9+
},
10+
{
11+
"type": "ColumnSet",
12+
"columns": [
13+
{
14+
"type": "Column",
15+
"items": [
16+
{
17+
"type": "Image",
18+
"style": "Person",
19+
"url": "https://pbs.twimg.com/profile_images/3647943215/d7f12830b3c17a5a9e4afcc370e3a37e_400x400.jpeg",
20+
"size": "Small"
21+
}
22+
],
23+
"width": "auto"
24+
},
25+
{
26+
"type": "Column",
27+
"items": [
28+
{
29+
"type": "TextBlock",
30+
"weight": "Bolder",
31+
"text": "Matt Hidinger",
32+
"wrap": true
33+
},
34+
{
35+
"type": "TextBlock",
36+
"spacing": "None",
37+
"text": "Created {{DATE(2017-02-14T06:08:39Z,SHORT)}}",
38+
"isSubtle": true,
39+
"wrap": true
40+
}
41+
],
42+
"width": "stretch"
43+
}
44+
]
45+
},
46+
{
47+
"type": "TextBlock",
48+
"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.",
49+
"wrap": true
50+
},
51+
{
52+
"type": "FactSet",
53+
"facts": [
54+
{
55+
"title": "Board:",
56+
"value": "Adaptive Card"
57+
},
58+
{
59+
"title": "List:",
60+
"value": "Backlog"
61+
},
62+
{
63+
"title": "Assigned to:",
64+
"value": "Matt Hidinger"
65+
},
66+
{
67+
"title": "Due date:",
68+
"value": "Not set"
69+
}
70+
]
71+
}
72+
],
73+
"actions": [
74+
{
75+
"type": "Action.ShowCard",
76+
"title": "Set due date",
77+
"card": {
78+
"type": "AdaptiveCard",
79+
"body": [
80+
{
81+
"type": "Input.Date",
82+
"id": "dueDate"
83+
},
84+
{
85+
"type": "Input.Text",
86+
"id": "comment",
87+
"placeholder": "Add a comment",
88+
"isMultiline": true
89+
}
90+
],
91+
"actions": [
92+
{
93+
"type": "Action.OpenUrl",
94+
"title": "OK",
95+
"url": "http://adaptivecards.io"
96+
}
97+
],
98+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json"
99+
}
100+
},
101+
{
102+
"type": "Action.OpenUrl",
103+
"title": "View",
104+
"url": "http://adaptivecards.io"
105+
}
106+
],
107+
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
108+
"version": "1.0"
109+
}

tests/api/test_messages.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""
2424

2525
import itertools
26-
26+
import json, pathlib
2727
import pytest
2828

2929
import webexteamssdk
@@ -56,6 +56,25 @@ def direct_message_by_email(api, test_people):
5656
api.messages.delete(message.id)
5757

5858

59+
@pytest.fixture(scope="session")
60+
def direct_message_by_email_with_card(api, test_people):
61+
attachments = []
62+
attachment = {}
63+
attachment['contentType'] = "application/vnd.microsoft.card.adaptive"
64+
attachment["content"] = json.loads(open(pathlib.Path(__file__).parent / 'example_card.json').read())
65+
attachments.append(attachment)
66+
person = test_people["member_added_by_email"]
67+
message = api.messages.create(
68+
toPersonEmail=person.emails[0],
69+
text=create_string("Message"),
70+
attachments=attachments
71+
)
72+
73+
yield message
74+
75+
api.messages.delete(message.id)
76+
77+
5978
@pytest.fixture(scope="session")
6079
def direct_message_by_id(api, test_people):
6180
person = test_people["member_added_by_id"]
@@ -137,8 +156,8 @@ def group_room_messages(api, group_room,
137156

138157

139158
@pytest.fixture(scope="session")
140-
def direct_messages(api, direct_message_by_email, direct_message_by_id):
141-
return [direct_message_by_email, direct_message_by_id]
159+
def direct_messages(api, direct_message_by_email, direct_message_by_id, direct_message_by_email_with_card):
160+
return [direct_message_by_email, direct_message_by_id, direct_message_by_email_with_card]
142161

143162

144163
# Tests
@@ -191,6 +210,10 @@ def test_create_direct_messages_by_email(direct_message_by_email):
191210
assert is_valid_message(direct_message_by_email)
192211

193212

213+
def test_create_direct_messages_by_email_with_card(direct_message_by_email_with_card):
214+
assert is_valid_message(direct_message_by_email_with_card)
215+
216+
194217
def test_create_direct_messages_by_id(direct_message_by_id):
195218
assert is_valid_message(direct_message_by_id)
196219

webexteamssdk/api/messages.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def list(self, roomId, mentionedPeople=None, before=None,
135135
yield self._object_factory(OBJECT_TYPE, item)
136136

137137
def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
138-
text=None, markdown=None, files=None, **request_parameters):
138+
text=None, markdown=None, files=None, attachments=None, **request_parameters):
139139
"""Post a message, and optionally a attachment, to a room.
140140
141141
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,
154154
markdown(basestring): The message, in markdown format.
155155
files(`list`): A list of public URL(s) or local path(s) to files to
156156
be posted into the room. Only one file is allowed per message.
157+
attachments(`list`): A list comprised of properly formatted button
158+
and card data structure. This can be found at
159+
https://docs.microsoft.com/en-us/adaptive-cards/sdk/designer
157160
**request_parameters: Additional request parameters (provides
158161
support for parameters that may be added in the future).
159162
@@ -174,6 +177,7 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
174177
check_type(text, basestring)
175178
check_type(markdown, basestring)
176179
check_type(files, list)
180+
check_type(attachments, list)
177181
if files:
178182
if len(files) != 1:
179183
raise ValueError("The length of the `files` list is greater "
@@ -184,6 +188,14 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
184188
"message.")
185189
check_type(files[0], basestring)
186190

191+
if attachments:
192+
for attachment in attachments:
193+
try:
194+
content_type_exists = attachment['contentType']
195+
except Exception as e:
196+
# ensure a valid header is loaded for cards
197+
attachment['contentType'] = 'application/vnd.microsoft.card.adaptive'
198+
187199
post_data = dict_from_items_with_values(
188200
request_parameters,
189201
roomId=roomId,
@@ -192,6 +204,7 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
192204
text=text,
193205
markdown=markdown,
194206
files=files,
207+
attachments=attachments,
195208
)
196209

197210
# API request

0 commit comments

Comments
 (0)