繁体   English   中英

在 Microsoft Bot Framework 中以电子邮件形式接收和发送附件

[英]Receive and Send Attachment as Email within 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
    };
}

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

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

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

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

希望得到帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM