Receive and Send Attachment as Email within Microsoft Bot Framework

Amokstakov :

I am currently building a chatbot that is able to receive attachments and save them to a local directory. I want to find out how to use that same attachment and send it via email.

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
    };
}

That is currently the function to receive and save to directory, but how do I actually capture the attachment and send it to another function?

Steven Kanberg :

Look over the 24.bot-authentication-msgraph sample in the BotBuilder-Samples repo. This sample demonstrates how a bot can be setup to send an email on the user's behalf.

Using that sample as a reference/template, you can infer how this process could work for you (in case you are not using MS Graph). The docs here explain how a file can be included as an attachment in an email.

If you preserve the location of the saved file, you should be able to read the file from the local directory and, using the method referenced above, attach the file before sending.

Hope of help.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342388&siteId=1