Introduction
In my previous Bot framework articles, I have explained about
Now in this article, I will explain how we can attach images and files with Bot framework's reply message.
Many messaging channels possess an ability to attach rich objects like images, media files and other files. In the Bot Connector, we map our attachment data structure to media attachments and rich cards on each channel.
Let's start, step by step.
First of all open, Visual Studio and create a Bot Application. Follow the steps to create a Bot Application.
- Go to File > New > Project (CTRL + SHIFT +N).
- Now one dialog box will open. Select Visual C# Project and you will find an option, Bot Application. Choose this and give the name of the project.
- After creating the project, you will get some files in Solution Explorer, as you can see in the figure shown below:
Now open MessagesController, which is located under controllers folder. In this, you will get following code, by default.
- using System;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Web.Http;
- using System.Web.Http.Description;
- using Microsoft.Bot.Connector;
- using Microsoft.Bot.Connector.Utilities;
- using Newtonsoft.Json;
-
- namespace AttachImagesAndFiles
- {
- [BotAuthentication]
- public class MessagesController : ApiController
- {
-
-
-
-
- public async Task<Message> Post([FromBody]Message message)
- {
- if (message.Type == "Message")
- {
-
- int length = (message.Text ?? string.Empty).Length;
-
-
- return message.CreateReplyMessage($"You sent {length} characters");
- }
- else
- {
- return HandleSystemMessage(message);
- }
- }
-
- private Message HandleSystemMessage(Message message)
- {
- if (message.Type == "Ping")
- {
- Message reply = message.CreateReplyMessage();
- reply.Type = "Ping";
- return reply;
- }
- else if (message.Type == "DeleteUserData")
- {
-
-
- }
- else if (message.Type == "BotAddedToConversation")
- {
- }
- else if (message.Type == "BotRemovedFromConversation")
- {
- }
- else if (message.Type == "UserAddedToConversation")
- {
- }
- else if (message.Type == "UserRemovedFromConversation")
- {
- }
- else if (message.Type == "EndOfConversation")
- {
- }
-
- return null;
- }
- }
- }
If you want to attach a simple image or file with reply message, then you have to add a simple attachment data structure with a link to the content which is depicted below:
- ContentType- In this property you can set Content Type / MIME Type, which describes which type of content you are sending.
- ContentUrl- This property contains actual file link.
Sample Code for sending Files and Images
- Attachment attachment = new Attachment();
- attachment.ContentType = "image/png";
- attachment.ContentUrl = "http://www.c-sharpcorner.com/Images/csharp-corner.png";
- message.Attachments.Add(attachment);
Example for sending an Image
Output
Run this program in Bot emulator and you will get the output, as shown below:
Example for sending Files
- Attachment attachment1 = new Attachment();
- attachment1.ContentType = "application/pdf";
- attachment1.ContentUrl = "http://fastandfluid.com/publicdownloads/AngularJSIn60MinutesIsh_DanWahlin_May2013.pdf";
-
- message.Attachments.Add(attachment1);
-
- message.Text = "Angular JS";
- return message;
Output