簡體   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