首页 > 解决方案 > 在 Microsoft Bot Framework 中以电子邮件形式接收和发送附件

问题描述

我目前正在构建一个能够接收附件并将其保存到本地目录的聊天机器人。我想了解如何使用相同的附件并通过电子邮件发送。

async downloadAttachmentAndWrite(attachment) {
    // Retrieve the attachment via the attachment's contentUrl.
    const url = attachment.contentUrl;
    console.log(attachment)

    // Local file path for the bot to save the attachment.
    const localFileName = path.join(__dirname, attachment.name);

    try {
        // arraybuffer is necessary for images
        const response = await axios.get(url, { responseType: 'arraybuffer' });
        console.log('#####')
        console.log(response.data)
        // If user uploads JSON file, this prevents it from being written as "{"type":"Buffer","data":[123,13,10,32,32,34,108..."
        if (response.headers['content-type'] === 'application/json') {
            response.data = JSON.parse(response.data, (key, value) => {
                return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
            });
        }
        fs.writeFile(localFileName, response.data, (fsError) => {
            console.log(localFileName)
            console.log(response.data)
            if (fsError) {
                throw fsError;
            }
        });
    } catch (error) {
        console.error(error);
        return undefined;
    }
    // If no error was thrown while writing to disk, return the attachment's name
    // and localFilePath for the response back to the user.
    return {
        fileName: attachment.name,
        localPath: localFileName
    };
}

这是目前接收并保存到目录的功能,但我如何实际捕获附件并将其发送到另一个功能?

标签: javascriptbotframework

解决方案


查看 BotBuilder-Samples 存储库中的24.bot-authentication-msgraph示例。此示例演示如何设置机器人以代表用户发送电子邮件。

使用该示例作为参考/模板,您可以推断此过程如何为您工作(如果您不使用 MS Graph)。此处的文档解释了如何将文件作为附件包含在电子邮件中。

如果您保留保存文件的位置,您应该能够从本地目录中读取文件,并使用上面引用的方法,在发送前附加文件。

希望得到帮助。


推荐阅读