Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In the previous article we saw how to send a file to the Bot. There will be cases where the Bot may send some files to the User. For example, if you are building a Payment Bot once the payment is complete the Bot would send back the receipt to the user. This article demonstrates the Bot sending a file to the end user. File exchanges between the Bot and the end user is an important concept and we will be using it when we are building Intelligent Bots that work on files using Cognitive Services in the future articles.
Prerequisites
Before deep diving into this article I would strongly recommend you to go through all the previous articles of this series.
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
Microsoft Bot Framework: Getting Started – Sending a File to the Bot
Here in this article we will not be focusing on the basic concepts of Bot Framework. We will concentrate here on how the Bot can send file to the user.
Sending Attachment
Bot sending file to the user can be achieved very easily. The steps demonstrated to achieve this would be one of the easiest steps among all the articles in this series. An Attachment object has to be created and added to the message Activity object. The Attachment object should contain the file type and url.
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)
{
//File that Bot will send to the user. You can keep a file in your local directory
//and can paste complete path here.
string filePath = @"C:\BotDirectory\Image\IMG_4821.jpg";
//Convert to Uri. Bot can send absolute Uri path only as attachment.
var uri = new System.Uri(filePath);
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity message = activity.CreateReply("Please check the image you have requested for.");
//Create attachment
Attachment attachment = new Attachment();
attachment.ContentType = "image/jpg";
//Content Url for the attachment can only be Absolute Uri
attachment.ContentUrl = uri.AbsoluteUri;
//Add attachment to the message
message.Attachments.Add(attachment);
//Send Reply message along with the attachment
await connector.Conversations.ReplyToActivityAsync(message);
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
Step 3
Place a file in a local folder. Bot will pick up this file and send to the user. Here I have kept my photo in a local folder that the Bot will send across to the user.
http://i.imgur.com/P4yXFBW.jpg
Step 4
Build the solution and run. The service starts up.
http://i.imgur.com/RsGlpFQ.jpg
Step 5
Open Bot framework emulator and communicate with Bot. User sends Hi to the Bot and the Bot replies back with the image.
http://i.imgur.com/fEAIZvV.jpg
Source Code
This solution is built using Visual Studio 2017 Community Edition. You can get the source from GitHub repository here.
Next Steps
Watch out this step for next set of articles in this series.