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.
Prerequisites
- Download and install Visual Studio for Mac
- Clone and download the Bot Framework Project Template for Mac.
- Download and install the Bot Framework Emulator for Mac.
Configure and Register the 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.
Step 5. Build the solution. If it has successfully completed the Build, the project template gets added to the "Custom Folders" from Visual Studio Preference.
Create a Bot Application
Let's start with creating a new bot application in Visual Studio for Mac. Open Visual Studio 2017, and create a new project with C#. Select the Bot applications template as below.
Provide the project name, solution name, and location as below.
The bot application gets created with all the components and all required NuGet references installed.
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 the 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.
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
// Calculate something for us to return
int length = (activity.Text ?? string.Empty).Length;
// Return our reply to the user
// Test
if (activity.Text.Contains("technology"))
{
await context.PostAsync("Refer C# corner website for technology http://www.c-sharpcorner.com/");
}
else if (activity.Text.Contains("morning"))
{
await context.PostAsync("Hello !! Good Morning, Have a nice Day");
}
// Test
else if (activity.Text.Contains("night"))
{
await context.PostAsync("Good night and Sweetest Dreams with Bot Application");
}
else if (activity.Text.Contains("date"))
{
await context.PostAsync(DateTime.Now.ToString());
}
else
{
await context.PostAsync($"You sent {activity.Text} which was {length} characters");
}
Wait(MessageReceivedAsync);
}
Run Bot Application
The emulator is a desktop application that lets you test and debug your bot on localhost or remotely. Now, you can click on "Run the application" in any browser.
Install Bot Emulator
If you have not installed the Bot Emulator on Mac, you need to download and install the emulator to test the bot application. You can download the Bot Emulator from - https://goo.gl/kZkoJT
Follow the below steps to test your bot application on Mac.
- Open Bot Emulator.
- Click "New Bot Configuration".
- Copy the above localhost URL and paste it into the emulator. For example - http://127.0.0.1:3978
- You can append the /api/message in the above URL; e.g. - http://127.0.0.1:3978/api/messages.
- You won't need to specify the MSA ID and MSA password for localhost testing. So, click on "Save and Connect".
You can send a message to the bot application. The bot will reply as per your guide/code.
Summary
In this article, you learned how to create a bot application using Visual Studio for Mac. If you have any questions, feedback, or issues, please write in the comment box.