【发布时间】:2020-05-12 06:14:05
【问题描述】:
我在瀑布中添加了一个步骤来获取用户的附件。 使用 BotFramework Emulator 进行测试时,我可以将音频文件发送到机器人并将相同的文件再次回显给用户。以下是瀑布步骤和代码
self.add_dialog(
WaterfallDialog(
WaterfallDialog.__name__,
[
self.project_step,
self.name_step,
self.description_step,
**self.attachment_step**,
self.confirm_step,
self.final_step,
],
)
)
以下是附件步骤的代码:
async def attachment_step(self, step_context: WaterfallStepContext) -> DialogTurnResult:
confluence_details = step_context.options
# Capture the results of the previous step
confluence_details.description = step_context.result
message_text = "please add an attachment"
prompt_options = PromptOptions(
prompt=MessageFactory.text(
"add an attachment"
),
retry_prompt=MessageFactory.text(
"The attachment must be a mp4/wav audio file."
),
)
return await step_context.prompt(AttachmentPrompt.__name__, prompt_options)
async def confirm_step(
self, step_context: WaterfallStepContext
) -> DialogTurnResult:
confluence_details = step_context.options
confluence_details.audioFile = (
None if not step_context.result else step_context.result[0]
)
if confluence_details.audioFile:
await step_context.context.send_activity(
MessageFactory.attachment(
confluence_details.audioFile, "This is your audio file."
)
)
@staticmethod
async def file_prompt_validator(prompt_context: PromptValidatorContext) -> bool:
if not prompt_context.recognized.succeeded:
await prompt_context.context.send_activity(
"No attachments received. Proceeding without attachment..."
)
# We can return true from a validator function even if recognized.succeeded is false.
return True
attachments = prompt_context.recognized.value
valid_file = [
attachment
for attachment in attachments
if attachment.content_type in ["audio/mp3", "audio/mp4","audio/wav"]
]
prompt_context.recognized.value = valid_file
# If none of the attachments are valid images, the retry prompt should be sent.
return len(valid_file) > 0
代码在模拟器中运行良好。
我不明白如何将音频文件从 User Application 发送到机器人? 我可以将音频文件作为 base64 编码字符串发送吗?如果可以,我需要在 Bot 端进行哪些更改。
我可以在包含文件托管位置 URL 的内容 URL 中找到要发送该文件的任何地方。 我无法接收从 User Application 发送到机器人的文件。
编辑:- 上一个问题已解决。我能够接收作为 Base64 编码字符串的音频文件。问题在于发送的 JSON。在 contentUrl 中,它应该是附件数组。我的错
Edit1:- 尝试将音频文件作为 base64 编码字符串发送时,某些文件会出错:
{
"error": {
"code": "MessageSizeTooBig",
"message": "Activity body too large for storage. Try using attachments to reduce the activity size."
}
}
这究竟是什么意思?
正如我从link 看到的那样,活动大小有一个上限。 有人可以建议将音频文件发送到机器人的最可行方法吗?
【问题讨论】:
-
如果您的问题得到解决,您愿意发布该解决方案作为答案吗?
-
@KyleDelaney 我已经发布了答案。也许你可以提供一些关于相同的见解。
-
您是否像在其他问题中一样使用自定义 Direct Line iOS 客户端? stackoverflow.com/questions/61635984/…
-
@KyleDelaney 是的,我们正在使用自定义 Direct Line iOS 客户端。
标签: python json botframework attachment azure-bot-service