如何接收用户发送到Bot的音频附件?

2024-05-15 01:15:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我在瀑布中添加了一个步骤来从用户那里获取附件。 当使用BotFramework Emulator进行测试时,我能够将音频文件发送到bot,并将相同的文件再次回传给用户

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

该代码在emulator中运行良好

我不明白的是如何将音频文件从用户应用程序发送到bot? 我可以将音频文件作为base64编码字符串发送。如果可以,我需要在Bot端进行哪些更改

我可以在任何地方找到文件发送的内容URL,其中包含文件所在位置的URL。 我无法接收从用户应用程序发送到bot的文件

我附上一个对话流程的截图以供参考。 converstaion flow

enter image description here

编辑:-上一个问题已解决。我能够以Base64编码字符串的形式接收音频文件。问题在于发送的JSON。在contentUrl中,它应该是一个附件数组。我的错误

Edit1:-当尝试将音频文件作为base64编码字符串发送时,它会给出一些文件的错误,如下所示:

{
    "error": {
        "code": "MessageSizeTooBig",
        "message": "Activity body too large for storage. Try using attachments to reduce the activity size."
    }
}

这究竟意味着什么

正如我从这个{a3}中看到的,活动大小有一个上限。 有人可以建议最可行的方式发送音频文件到机器人吗


Tags: 文件the用户selfattachmentifstepconfluence

热门问题