Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel
Ezequiel Jadib 6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
..
74eb448cde
[Node] Update Emulator screenshots
7 years ago
7a204678e3
Node - Move environment vars for development to .env files
7 years ago
6b3df2c0b3
Update documentation links to point to new docs (#118)
7 years ago
7a204678e3
Node - Move environment vars for development to .env files
7 years ago
a2a0e1baa1
update azuredeploy.json paths
7 years ago
28d163ecf6
[Node] Update to botbuilder 3.7.0 (#50)
7 years ago

README.md

You have to be logged in to leave a comment. Sign In

Receive Attachment Bot Sample

A sample bot that receives attachments sent by the user and downloads them.

Deploy to Azure

Prerequisites

The minimum prerequisites to run this sample are:

  • Latest Node.js with NPM. Download it from here.
  • The Bot Framework Emulator. To install the Bot Framework Emulator, download it from here. Please refer to this documentation article to know more about the Bot Framework Emulator.
  • [Recommended] Visual Studio Code for IntelliSense and debugging, download it from here for free.

Code Highlights

When attachments are sent to the bot they can be found in the message activity attachments property. This property exposes a list of IAttachment representing each of the files sent. The attachment object contains information on the contentType of the file and a contentUrl which is a reference to the location of the attachment's content. In order to access the actual attachment file you will need to download the address from the contentUrl property. Check out the key code located in the dialog handler where the message.attachments property of the message activity is read to get the first attachment and download it.

Note: The Skype and MS Teams attachment URLs are secured by JwtToken; you should set the JwtToken of your bot as the authorization header for the HTTP GET request your bot initiates to fetch content. Below is the sample code that temporarily works around this issue and set the JwtToken on the HTTP request. You should be careful when you send the bot's JwtToken to a third party server and should always make sure to send it to trusted parties.

function (session) {

    var msg = session.message;
    if (msg.attachments.length) {

        // Message with attachment, proceed to download it.
        // Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
        var attachment = msg.attachments[0];
        var fileDownload = checkRequiresToken(msg)
            ? requestWithToken(attachment.contentUrl)
            : request(attachment.contentUrl);

        fileDownload.then(
            function (response) {

                // Send reply with attachment type & size
                var reply = new builder.Message(session)
                    .text('Attachment of %s type and size of %s bytes received.', attachment.contentType, response.length);
                session.send(reply);

            }).catch(function (err) {
                console.log('Error downloading attachment:', { statusCode: err.statusCode, message: err.response.statusMessage });
            });
    }
}

// Helper methods

// Request file with Authentication Header
var requestWithToken = function (url) {
    return obtainToken().then(function (token) {
        return request({
            url: url,
            headers: {
                'Authorization': 'Bearer ' + token,
                'Content-Type': 'application/octet-stream'
            }
        });
    });
};

// Promise for obtaining JWT Token (requested once)
var obtainToken = Promise.promisify(connector.getAccessToken.bind(connector));

var checkRequiresToken = function (message) {
    return message.source === 'skype' || message.source === 'msteams';
};

Outcome

You will see the following in the Bot Framework Emulator when opening and running the sample solution.

Sample Outcome

More Information

To get more information about how to get started in Bot Builder for Node and Attachments please review the following resources:

Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...