【问题标题】:Microsoft Bot Framework Python During a WaterfallStepContext using adaptive card attachment get submit value from adaptive card actionMicrosoft Bot Framework Python 在 WaterfallStepContext 期间使用自适应卡片附件从自适应卡片操作中获取提交值
【发布时间】:2020-11-07 19:04:51
【问题描述】:

在使用 WaterfallDialog 的对话框期间,我希望通过允许用户从选择器中进行选择来提示用户输入 DateTime。 DateTimePrompt 的提示只等待用户提交代表 DateTime 的字符串。 :(

我宁愿拥有一个 DateTimePickerPrompt,机器人在其中发送日历,用户可以从中选择。那根本不存在。

阅读后:https://blog.botframework.com/2019/07/02/using-adaptive-cards-with-the-microsoft-bot-framework/。我希望这是一种能力。特别是部分:“对话框中的自适应卡片”。

这是我尝试过的: 自适应卡片json

{
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "type": "AdaptiveCard",
  "version": "1.0",
  "body": [
    {
      "type": "TextBlock",
      "text": "Select Start Date and Time."
    },
    {
      "type": "Input.Date",
      "id": "start_date",
      "placeholder": "Enter a date"
    },
    {
      "type": "Input.Time",
      "id": "start_time",
      "placeholder": "Enter a time"
    }
  ],
  "actions": [
    {
      "type": "Action.Submit",
      "title": "OK",
      "data": {
        "key": "still replies with nothing given document says only object types can be returned"
      }
    }
  ]
}

瀑布日期时间步长法

    def _create_adaptive_card_attachment(self) -> Attachment:
        """
        Load a random adaptive card attachment from file.
        :return:
        """

        card_path = os.path.join(os.getcwd(), 'resources/datetime_picker.json')
        with open(card_path, "rb") as in_file:
            card_data = json.load(in_file)

        return CardFactory.adaptive_card(card_data)

    async def waterfall_start_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
        prompt_options = PromptOptions(
                prompt=MessageFactory.attachment(
                    self._create_adaptive_card_attachment()
                ),
                choices=[Choice("0"), Choice("1")],
                style=ListStyle.none
        )
        return await step_context.prompt(
            TextPrompt.__name__,
            prompt_options
        )

由于 DialogTurnResult.result == None,这被设置为无限循环。

此外,step_context.context.activity 确实表示有响应,但值为 None。

{
  'additional_properties': {},
  'type': 'message',
  'id': '68c3f2f0-c881-11ea-827f-25034e37bd5f',
  'timestamp': datetime.datetime(2020, 7, 17, 23, 1, 12, 223000, tzinfo=<isodate.tzinfo.Utc object at 0x10b39b9e8>),
  'local_timestamp': datetime.datetime(2020, 7, 17, 18, 1, 12, tzinfo=<FixedOffset '-09:00'>),
  'local_timezone': None,
  'service_url': 'http://localhost:60945',
  'channel_id': 'emulator',
  'from_property': <botbuilder.schema._models_py3.ChannelAccount object at 0x10c0d9b38>,
  'conversation': <botbuilder.schema._models_py3.ConversationAccount object at 0x10c0d9ac8>,
  'recipient': <botbuilder.schema._models_py3.ChannelAccount object at 0x10c0fc240>,
  'text_format': 'plain',
  'attachment_layout': None,
  'members_added': None,
  'members_removed': None,
  'reactions_added': None,
  'reactions_removed': None,
  'topic_name': None,
  'history_disclosed': None,
  'locale': 'en-US',
  'text': 'asdg',
  'speak': None,
  'input_hint': None,
  'summary': None,
  'suggested_actions': None,
  'attachments': None,
  'entities': None,
  'channel_data':
  {
    'clientActivityID': '1595026872220mnbwlxl2k5',
    'clientTimestamp': '2020-07-17T23:01:12.220Z'
  },
  'action': None,
  'reply_to_id': None,
  'label': None,
  'value_type': None,
  'value': None,
  'name': None,
  'relates_to': None,
  'code': None,
  'expiration': None,
  'importance': None,
  'delivery_mode': None,
  'listen_for': None,
  'text_highlights': None,
  'semantic_action': None,
  'caller_id': None
}

我的“第二次”尝试使用相同的 _create_adaptive_card_attachment 方法:

    async def waterfall_start_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
        message = Activity(
            text = "Here is an Adaptive Card:",
            type = ActivityTypes.message,
            attachments = [self._create_adaptive_card_attachment()],
        )

        await step_context.context.send_activity(message)
        return DialogTurnResult(status=DialogTurnStatus.Empty,result={})

这会返回相同的上下文活动。

我看到一个非常相似的问题:How to retrieve Adaptive Card's form submission in subsequent waterfall step

C# 中的这个逻辑似乎是文档中描述的内容。我相信我在 python 中实现了这一点。但我似乎遗漏了什么。

因此,如果文档属实,那么我应该能够从自适应卡片提交操作中获取数据。这里的任何帮助都会很棒。感谢您的时间和精力。

【问题讨论】:

  • 欢迎来到 Stack Overflow。你的问题是什么?请查看方便的指南,了解您可以采取哪些步骤来更快地获得更好的答案:stackoverflow.com/help/how-to-ask
  • 另外,你用的是什么渠道?
  • 你还在做这个吗?

标签: python botframework


【解决方案1】:

我正在做同样的事情,但我有一个表单而不是按钮。我已经关注了blog 和您指出的 C# 答案。卡片渲染的实现是正确的。但是,我们需要将 Activity 的文本设置为 Prompt 可以看到的结果,而不是 None 或 Blank 值。

async def on_turn(self, turn_context: TurnContext):
    if turn_context.activity.type == 'message':
        if turn_context.activity.text == None and turn_context.activity.value != None:
            turn_context.activity.text = json.dumps(turn_context.activity.value)      
    await super().on_turn(turn_context)               
    # Save any state changes that might have occurred during the turn.
    await self.conversation_state.save_changes(turn_context, False)
    await self.user_state.save_changes(turn_context, False)

使用自适应卡片输入中的值更新文本后,这些将在下一步中以step_context.result 的形式提供。您将能够处理其中的值。请注意,在 super 之前放置 value check 的位置很重要。

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 2022-01-08
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2020-05-30
    • 2020-12-09
    相关资源
    最近更新 更多