Share via


Microsoft Bot Framework: Getting Started – Sending a File to the Bot

Now that we have a fare understanding of how to create a Bot, let us look into some real time user scenarios. One of them can be sending a file attachment to the Bot. For example, if you are building a Medical Insurance Bot then the Bot should be capable enough to ask for bills. User should be able to send the bills to the Bot as attachment. This article demonstrates how to send a file to the Bot.

Prerequisites

Before deep diving into this article I would strongly recommend you to go through all the previous articles of this series. Here in this article we will not be focusing on the basic concepts of Bot Framework.
Microsoft Bot Framework : Getting Started - Building the first Hello Bot
Microsoft Bot Framework: Getting Started – Making the Bot Intelligent using Text Analytics Sentiment API from Microsoft Cognitive Services
Microsoft Bot Framework: Getting Started – Understanding Dialogs and Maintaining states between conversations
We will concentrate here on how to send attachments to Bot.

Sending Attachment

Sending an attachment to Bot is very simple. The steps demonstrated to achieve this would be one of the easiest steps among all the articles in this series. Whenever user sends attachments in the cha thet Bot can access the attachments in Attachment list property of Activity parameter to the Post method in the MessageController.

Step 1

Open Visual Studio and create a project of type Bot Application.

Step 2

Go to Controllers\MessagesController.cs and replace Post method with below.

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new  ConnectorClient(new  Uri(activity.ServiceUrl));
                //Path where files will be saved. Please provide the path where you would like to
                //save the files.
                string saveFileInPath = @"C:\BotDirectory\";
                //Loop through all attachments sent by user
                foreach (Attachment attachment in activity.Attachments)
                {
                    //Get the url from where to download the file that user has sent
                    string url = attachment.ContentUrl;
                    //Code to download file
                    Uri uri = new  Uri(url);
                    using (var webClient = new WebClient())
                    {
                        //attachment.Name gives the name of the file. Save the file with the smae name
                        webClient.DownloadFile(url, saveFileInPath+attachment.Name);
                    }
                }
                Activity message = activity.CreateReply("Thank you. We have received the files.");
                //Send Reply message
                await connector.Conversations.ReplyToActivityAsync(message);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }

Step 3

Build the solution and run. The service starts up.

http://i.imgur.com/QXsRWoB.jpg

Step 4

Open Bot framework emulator and communicate with Bot. Click on highlighted button below to attach files.

http://i.imgur.com/7DuNwee.jpg

Select files to send across to the Bot.

http://i.imgur.com/vwXFDWR.jpg

Bot receives the file and confirms as in chat below.

http://i.imgur.com/7ff6IMs.jpg

Bot has downloaded the files in the destination folder.

http://i.imgur.com/tym09tJ.jpg

Source Code

This solution is built using Visual Studio 2017 Community Edition. You can get the source from GitHub repository here.

Next Steps

Please watch out this section for next set of articles for this series.