Skip to content

Commit 432169a

Browse files
committed
Added ability to send "pythonic" cards to message create method
You can now send a card like this: ``` from webexteamssdk import WebexTeamsAPI from webexteamssdk.cards.card import AdaptiveCard from webexteamssdk.cards.components import TextBlock from webexteamssdk.cards.options import Colors block = TextBlock("Hey hello there! I am a adaptive card", color=Colors.GOOD) card = AdaptiveCard(body=[block]) api = WebexTeamsAPI() api.messages.create(text="fallback", roomId="...", cards=card) ```
1 parent 574ae5b commit 432169a

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

webexteamssdk/api/messages.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
from ..restsession import RestSession
4040
from ..utils import (
4141
check_type, dict_from_items_with_values, is_local_file, is_web_url,
42-
open_local_file,
42+
open_local_file, make_card_attachment
4343
)
44+
from ..cards.card import AdaptiveCard
4445

4546

4647
API_ENDPOINT = 'messages'
@@ -135,7 +136,7 @@ def list(self, roomId, mentionedPeople=None, before=None,
135136
yield self._object_factory(OBJECT_TYPE, item)
136137

137138
def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
138-
text=None, markdown=None, files=None, **request_parameters):
139+
text=None, markdown=None, files=None, cards=None, **request_parameters):
139140
"""Post a message, and optionally a attachment, to a room.
140141
141142
The files parameter is a list, which accepts multiple values to allow
@@ -154,6 +155,8 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
154155
markdown(basestring): The message, in markdown format.
155156
files(`list`): A list of public URL(s) or local path(s) to files to
156157
be posted into the room. Only one file is allowed per message.
158+
cards(`list`): A list of adaptive cards objects that will be send
159+
with this message.
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(cards, (list, AdaptiveCard))
177181
if files:
178182
if len(files) != 1:
179183
raise ValueError("The length of the `files` list is greater "
@@ -194,6 +198,17 @@ def create(self, roomId=None, toPersonId=None, toPersonEmail=None,
194198
files=files,
195199
)
196200

201+
# Add cards
202+
if cards is not None:
203+
cards_list = []
204+
205+
if isinstance(cards, list):
206+
cards_list = [make_card_attachment(c) for c in cards]
207+
else:
208+
cards_list = [make_card_attachment(cards)]
209+
210+
post_data['attachments'] = cards_list
211+
197212
# API request
198213
if not files or is_web_url(files[0]):
199214
# Standard JSON post

webexteamssdk/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,22 @@ def json_dict(json_data):
254254
"received: {!r}".format(json_data)
255255
)
256256

257+
def make_card_attachment(card):
258+
"""Given a card, makes a card attachment by attaching the correct
259+
content type and content.
260+
261+
Args:
262+
card(AdaptiveCard): Adaptive Card object that should be attached
263+
264+
Returns:
265+
A Python dictionary containing the card attachment dictionary
266+
"""
267+
ret = {
268+
"contentType": "application/vnd.microsoft.card.adaptive",
269+
"content": card.to_dict()
270+
}
271+
272+
return ret
257273

258274
class ZuluTimeZone(tzinfo):
259275
"""Zulu Time Zone."""

0 commit comments

Comments
 (0)