简体   繁体   中英

How to send a proactive adaptive card to MS Teams Channel

My code:

# load card .json
card_path = os.path.join(os.getcwd(), "resources", "test.json")
with open(card_path, "rb") as in_file:
    card_data = json.load(in_file)

# create card
card = CardFactory.adaptive_card(card_data)

# target channel
thread_id = "19:95a25b4767123e29f835d3c10e88f1ee@thread.tacv2"

# create connector client
client = await ADAPTER.create_connector_client(service_url="https://smba.trafficmanager.net/emea/")

message = Activity(
    text="Here is an Adaptive Card:",
    type=ActivityTypes.message,
    attachments=[card],
)

params = ConversationParameters(
    is_group=True,
    channel_data={"channel": {"id": thread_id}},
    activity=message
)

# send card
return await client.conversations.create_conversation(params)

Error: (BadSyntax) Activity resulted into multiple skype activities

Traceback (most recent call last):
  File "/home/user/apps/pycharm-2018.1.3/helpers/pydev/pydevd.py", line 1664, in <module>
    main()
[...]
  File "/home/user/PycharmProjects/foo/07.using-adaptive-cards/venv/lib/python3.8/site-packages/aiohttp/signals.py", line 34, in send
    await receiver(*args, **kwargs)  # type: ignore
  File "/home/user/PycharmProjects/foo/07.using-adaptive-cards/app.py", line 114, in startup
    return await client.conversations.create_conversation(params)
  File "/home/user/PycharmProjects/foo/07.using-adaptive-cards/venv/lib/python3.8/site-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 176, in create_conversation
    raise models.ErrorResponseException(self._deserialize, response)
botbuilder.schema._models_py3.ErrorResponseException: (BadSyntax) Activity resulted into multiple skype activities

My adaptive card is correct. I tried the example project 07.using-adaptive-cards provided by Microsoft, however this example is not for a proactive message. It (just) responds to chat messages.

class AdaptiveCardsBot(ActivityHandler):
    async def on_message_activity(self, turn_context: TurnContext):
        card_path = os.path.join(os.getcwd(), "resources", "test.json")
        with open(card_path, "rb") as in_file:
            card_data = json.load(in_file)
    
        card = CardFactory.adaptive_card(card_data)
    
        message = Activity(
            text="Here is an Adaptive Card:",
            type=ActivityTypes.message,
            attachments=[card],
        )
    
        await turn_context.send_activity(message)

I need to proactively send an adaptive card.

I can send a regular proactive Teams message like below, so why not a card?

message = "Hello channel"
thread_id = "19:95a25b4767123e29f835d3c10e88f1ee@thread.tacv2"

client = await ADAPTER.create_connector_client(service_url="https://smba.trafficmanager.net/emea/")

params = ConversationParameters(
    is_group=True,
    channel_data={"channel": {"id": thread_id}},
    activity=MessageFactory.text(message)
)

await client.conversations.create_conversation(params)

I have looked at the answer given here https://stackoverflow.com/a/62099293/14997633 but this seems to send raw data to an URL, I would prefer to use the utilities/tools provided by the Botframework v4.

This SO answer helped me figure out the solution.

I should be using client.send_to_conversation instead of client.create_conversation for proactive messages.

Example:

# load card .json
card_path = os.path.join(os.getcwd(), "resources", "test.json")
with open(card_path, "rb") as in_file:
    card_data = json.load(in_file)

# create card
card = CardFactory.adaptive_card(card_data)

# target channel
thread_id = "19:95a25b4767123e29f835d3c10e88f1ee@thread.tacv2"

# create connector client
client = await ADAPTER.create_connector_client(service_url="https://smba.trafficmanager.net/emea/")

# create activity
message = Activity(
    type=ActivityTypes.message,
    attachments=[card],
)


# send proactive adaptive card
response = await client.conversations.send_to_conversation(thread_id, message)

The resulting response variable will be of type ResourceResponse and it will have a id parameter. This id parameter you may use whenever people respond/post actions in the case the adaptive card has form inputs (actions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM