Microsoft Artificial Intelligence

New Version of Devenvexe Windows 10 App Released by Suthahar

Friday, May 17, 2019

The Bot Framework enables you to build bots that support different types of interactions with users. You can design conversations in your bot to be freeform. Your bot can also have more guided interactions where it provides the user choices or actions.The conversation can use simple text strings, message with attachment like image, File(pdf,word,excel,ppt) , mp3, Video or more complex rich cards .

In this article, will help you to sending reply message with different type of media file attachment.

Prerequisite:

I have explained about Bot framework Installation, deployment and implementation in the below article

The Bot application template was created with all the components and all required NuGet references installed in the solutions.

Create New AttachmentDialog Class:

Step 1:

You can Create new AttachmentDialog class for show the attachment dialog. Right Click project > Select Add New Item > Create a class that is marked with the [Serializable] attribute (so the dialog can be serialized to state) and implement the IDialog interface.

using System;

using System.Threading.Tasks;

using Microsoft.Bot.Builder.Dialogs;

using Microsoft.Bot.Connector;

using System.IO;

using System.Web;

using System.Collections.Generic;

namespace BotAttachment.Dialogs

{

[Serializable]

publicclassAttachmentDialog : IDialog<object>

{

Step 2

IDialog interface has only StartAsync() method. StartAsync() is called when the dialog becomes active. The method is passed the IDialogContext object, used to manage the conversation.

publicasyncTask StartAsync(IDialogContext context)

{

context.Wait(this.MessageReceivedAsync);

}

Step 3:

Createa MessageReceivedAsync method and write following code for welcome message and show the list of demo options dialog.

The following output screen showing, after user provide the input, bot will fetch image from internet and display in emulator

Internet File Attachment:

You can refer following code for add hyperlink to fetch file from internet and attach to reply message, same code will be reuse for all the type of document but need to change content type and content url

The emulator is a desktop application that lets us test and debug our bot on localhost. Now, you can click on "Run the application" in Visual studio and execute in the browser

Test Application on Bot Emulator

You can follow the below steps to test your bot application.

Open Bot Emulator.

Copy the above localhost url and paste it in emulator e.g. - http://localHost:3979

You can append the /api/messages in the above url; e.g. - http://localHost:3979/api/messages.

You won't need to specify Microsoft App ID and Microsoft App Password for localhost testing, so click on "Connect".

Summary

In this article, you learned how to create a Bot application using Visual Studio 2017 and sending reply message with different type of media file attachment. If you have any questions/feedback/ issues, please write in the comment box.

About Me

Suthahar J is an industry luminary focused on AI, Computer Vision, Bot, Machine Learning, UWP, Azure, Xamarin and Web & Emerging User Experiences. He is a Technical Architect and C# Corner MVP. During his 12 years in the IT industry he has had roles in development, Lead and Architect using different Microsoft technologies on a number of large and complex projects. He is Author of Xamarin Q &A Book and published in Amazon. He is published Xamarin Q & A Bot to the Facebook Messenger, Blog, Skype and Cortana. He also enjoys sharing his passion with other people, thus actively contributing to the community via his presentations, writing technical Article and blogs which helps him becomes a better technology leader. He takes a keen interest in sharing his knowledge and solving readers’ technology-related problems. He is Founder of popular technical blog http://devenvexe.com.He was completed many Microsoft certification and received many awards. Whenever he gets the time he will go nearest orphans and stay with the child sometime and share computer skill.