In this post, I’ll be walking threw my steps I took to develop and run the Bot Framework locally on my Mac. Now my first decision was which text editor/IDE to use. VS Code would have been a fine choice, but since I’ve been doing a fair amount of Xamarin development lately, I thought I would try to VS of Mac since it supports .Net Core.

As you would expect with creating a project in Visual Studio, it all starts with New Project …

Selected .Net Core 2.0 …

And gave it a name…

Next I had to add some Nuget packages provided by Microsoft. I installed the following packages…

Microsoft.Bot.Connector
Microsoft.Bot.Connector.AspNetCore

Next I needed to add the AppId and AppPassword which you receive from Microsoft and is how your bot is registered. I’ve created a post that shows this process in Azure. Once you have the AppId and AppPassword, open the appsettings.json, which should have been generate when creating the project, and app the following settings…

Next, open the startup.cs file and within the ConfigureServices method remove services.AddMvc(); and add the following code…

services.AddSingleton(_ => Configuration);
var credentialProvider = new StaticCredentialProvider(
    Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value,
    Configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value);
services.AddAuthentication(

options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }
    )
    .AddBotAuthentication(credentialProvider);
services.AddSingleton(typeof(ICredentialProvider), credentialProvider);
services.AddMvc(options =>
{
    options.Filters.Add(typeof(TrustServiceUrlAttribute));
});

And within the Configure method in the same file, add the following two lines…

app.UseStaticFiles();
app.UseAuthentication();

Should look something like this…

Now that we have the startup code set, we now need to add a WebApi Controller. Within the controller folder I removed the one that’s generated with the project and added my own which I called MassagesController. I then added the following code..

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Connector;
using Microsoft.Extensions.Configuration;

namespace CoreBot.Controllers
{
    [Route("api/[controller]")]
    public class MessagesController : Controller
    {
        private IConfiguration _configuration;

        public MessagesController(IConfiguration Configuration)
        {
            _configuration = Configuration;
        }

        [Authorize(Roles = "Bot")]
        [HttpPost]
        public async Task<OkResult> Post([FromBody] Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                var appCredentials = new MicrosoftAppCredentials(_configuration);
                var connector = new ConnectorClient(new Uri(activity.ServiceUrl), appCredentials);
                var reply = activity.CreateReply("This is a test");
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                //HandleSystemMessage(activity);
            }
            return Ok();
        }
    }
}

As you can see above, the controller simply replies back with “This is a test”. Which this controller in place, we can now run the code to see if we can test the bot. Now in order to test the bot, we need to download a utility Microsoft has provided call the Bot Framework Enulator, which can be download from GitHub here. Once that is install, simply run the app by clicking the play button…

As the app runs, look at the Application Output to see the url and port number the bot is being run on. Plug that url in into the Bot Framework Emulator, along with the App ID and App Password…

Now let take this on a test run. Once running, type in anything and lets see if our bot responses…

Success!!! But wait, what about debugging? We’ll we are using Visual Studio, so let put a break point in the MessagesController.cs within the Post method and see if you can debug this puppy…

Apparently debugging is not a problem and we can easily see all of the activity properties as we would expect.

So in conclusion, not only can I build and run the bot framwork on a mac, but using Visual Studio for Mac, I also have a rich debugging experience. What more can you ask for? Well… how about running this in a Docker container? I’ll save that for my next post. Cheers!


Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: