Develop Bot Application Using Visual Studio Mac

Introduction

 
Bot Framework enables you to build bots that support different types of interactions with users. You can design conversations in your bot to be free. Your bot can also have more guided interactions where it provides the users with choices or actions. The conversation can use simple text strings or more complex rich cards that contain text, images, and action buttons. And, you can add natural language interactions, which let your users interact with your bots in a natural and expressive way.
 
The Bot Builder SDK for .NET is an easy-to-use framework for developing bots using Visual Studio in Windows but for Visual Studio for Mac, it is not available in the official release. I have modified the Bot Framework template to work on Visual Studio for Mac and started using all the Bot Framework features on my Mac machine.
 
In this article, I am showing how to create, build, and test a Bot application using a Mac machine.
 
 Develop Bot Application using Visual Studio Mac
Prerequisites
  1. Download and install Visual Studio for Mac
  2. Clone and download the Bot Framework Project Template for Mac.
  3. Download and install the Bot Framework Emulator for Mac.
Configure and Register Project Template
 
Step 1
Clone and download the Bot Framework template for Mac from the following URL - https://goo.gl/9ivoov
 
Step 2
Open the *.Csproj file or Visual Studio solution.
 
Step 3
Select and right-click on “Project” from Visual Studio Mac > “Restore NuGet Packages”.
 
Step 4
Right-click on Project, select the Project Options, select XSP Web server and expand the Run option. Update the port number to 3978 like in the below screen.
 
Develop Bot Application using Visual Studio Mac 
 
Step 5
Build the solution. If it has successfully completed the Build, the project template gets added into the "Custom Folders" from Visual Studio Preference.
 
Develop Bot Application using Visual Studio Mac 
 
Create a Bot Application
Let's start with creating a new bot application in Visual Studio for Mac. Open Visual Studio 2017, create a new project with C#. Select the Bot applications template as below.
 
Develop Bot Application using Visual Studio Mac 
 
Provide the project name, solution name, and location as below.
 
Develop Bot Application using Visual Studio Mac 
 
The bot application gets created with all the components and all required NuGet references installed.
 
Develop Bot Application using Visual Studio Mac 
 
Update the code
 
The default application adds a simple code snippet and we have no need to change anything. If you want to test your custom message, you can change it like below.
You can find the messagereceiveAsync method from Dialogs/RootDialog.cs file. In this method, the activity.Text container will return the user text input so that you can reply to a message based on the input text.
  1. private async Task MessageReceivedAsync(IDialogContext context, IAwaitable < object > result) {    
  2.     var activity = await result as Activity;    
  3.     // calculate something for us to return    
  4.     int length = (activity.Text ? ? string.Empty).Length;    
  5.     // return our reply to the user    
  6.     //test    
  7.     if (activity.Text.Contains("technology")) {    
  8.         await context.PostAsync("Refer C# corner website for tecnology http://www.c-sharpcorner.com/");    
  9.     } else if (activity.Text.Contains("morning")) {    
  10.         await context.PostAsync("Hello !! Good Morning , Have a nice Day");    
  11.     }    
  12.     //test    
  13.     else if (activity.Text.Contains("night")) {    
  14.         await context.PostAsync(" Good night and Sweetest Dreams with Bot Application ");    
  15.     } else if (activity.Text.Contains("date")) {    
  16.         await context.PostAsync(DateTime.Now.ToString());    
  17.     } else {    
  18.         await context.PostAsync($ "You sent {activity.Text} which was {length} characters");    
  19.     }    
  20.     Wait(MessageReceivedAsync);    
  21. }  
Run Bot Application
 
Emulator is a desktop application that lets you test and debugs your bot on localhost or remotely. Now, you can click on "Run the application" in any browser.
 
Develop Bot Application using Visual Studio Mac 
 
Install Bot Emulator
 
If you have not installed the Bot Emulator on Mac, you need to download and install the emulator for testing the bot application. You can download the Bot Emulator from - https://goo.gl/kZkoJT
 
Develop Bot Application using Visual Studio Mac 
 
Follow the below steps to test your bot application on Mac.
  1. Open Bot Emulator.
  2. Click "New Bot Configuration".
  3. Copy the above localhost URL and paste it into the emulator. For example -  http://127.0.0.1:3978
  4. You can append the /api/message in the above URL; e.g. - http://127.0.0.1:3978/api/messages.
  5. You won't need to specify the MSA ID and MSA password for localhost testing. So, click on "Save and Connect".

    Develop Bot Application using Visual Studio Mac
You can send a message to the bot application. The bot will reply as per your guide/code.
 
Develop Bot Application using Visual Studio Mac 
 

Summary

 
In this article, you learned how to create a bot application using Visual Studio for Mac. If you have any question, feedback, or issues, please write in the comment box.


Similar Articles