在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 repo中的24.bot-authentication-msgraph示例。此示例演示了如何设置机器人程序以代表用户发送电子邮件。

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

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

希望得到帮助。

最新更新