【问题标题】:How to send a proactive adaptive card to MS Teams Channel如何将主动自适应卡发送到 MS Teams 频道
【发布时间】:2021-02-25 15:13:18
【问题描述】:

我的代码:

# 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)

错误:(BadSyntax)活动导致多个 Skype 活动

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

我的自适应卡是正确的。我尝试了 Microsoft 提供的示例项目07.using-adaptive-cards,但是此示例不适用于主动消息。它(只是)响应聊天消息。

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)

我需要主动发送一张自适应卡片。

我可以像下面这样定期发送主动的 Teams 消息,那为什么不发送卡片呢?

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)

我查看了https://stackoverflow.com/a/62099293/14997633 此处给出的答案,但这似乎将原始数据发送到 URL,我更喜欢使用 Botframework v4 提供的实用程序/工具。

【问题讨论】:

    标签: python-3.x botframework microsoft-teams


    【解决方案1】:

    This SO answer 帮助我找到了解决方案。

    对于主动消息,我应该使用 client.send_to_conversation 而不是 client.create_conversation

    例子:

    # 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)
    

    生成的response 变量将是ResourceResponse 类型,并且具有id 参数。在自适应卡具有表单输入(操作)的情况下,每当人们响应/发布操作时,您都可以使用此 id 参数。

    【讨论】:

      猜你喜欢
      • 2020-04-30
      • 2020-02-17
      • 2021-02-06
      • 1970-01-01
      • 2021-06-19
      • 2021-06-17
      • 2021-03-04
      • 2021-12-05
      • 1970-01-01
      相关资源
      最近更新 更多