It’s Alive! My First Azure Bot Service

Microsoft CEO Satya Nadella says “bots are the new apps.” If that’s the case, then developers are going to need a cross-platform way to build those bots, just like they have cross-platform frameworks for building mobile app.
Good news, because Microsoft has built just such a framework in their Azure Bot Service. Think of it as fulfilling the same role Xamarin or React Native currently fill for cross-platform mobile app development, but for chat bots instead.

Many Chat Platforms for the Price of One

Let’s say you want to build a chat bot and make it available on Skype, Facebook Messenger, Slack, and SMS (via Twillio). You could either build 4 different bots on those 4 different platforms, or you could build just a single REST API service, and use Azure Bot Services to integrate it into all four platforms. Obviously, it is less work and maintenance for you if you build and maintain just one codebase.
Azure Bot Services allow you do build a single bot as a REST service, something most web and mobile developers are already very familiar with. You then expose that service to a Bot you set up on your Azure account and wire up to the various services, or channels, such as Facebook messenger, Twillio, Slack, and Skype. Microsoft calls this their “Bot-as-a-Service” offering, the first in the industry, but you could simply think of it as a “connector” that wires your REST service bot implementation to all the different chat channels.

Bus Position Service: Step-by-Step Example

To show how easy it is, I wrote a simple Bot Service using ASP.NET which gives you the position of a bus in my local system, The Rapid, in Grand Rapids, MI. It does this by using a public API for realtime bus positions. Using the position it returns, I’m generating a map image using Google Map’s static image API. Here’s how I did it:

  1. If you don’t have an Azure account, go sign up for a free one by starting here: https://azure.microsoft.com/en-us/free
  2. Once you’ve sign up and you’re in the Azure Portal, create a new resource.

  3. Search for “bot” to see the Bot Service resource.

  4. Click on Bot Service, then Create.
  5. You’ll answer a few questions about hosting your Bot Service, then click Create again.
  6. Azure will create your service for you in the background and alert you when it’s complete.
  7. The Azure portal will create an ASP.NET project template for you to download, but I created my own from scratch.
  8. In Visual Studio, I created a new ASP.NET (.NET Framework) project, and added only Web API to it. No .NET Core support just yet for Bot Framework as of the time of this posting!
  9. Then, I added the Microsoft.Bot.Builder NuGet package.
  10. Now, add a Dialog class to your project. This is the class that manages the conversation. You can use this starter template:
    public class BusDialog : IDialog<object>
    {
    	public Task StartAsync(IDialogContext context)
    	{
    		context.Wait(MessageReceivedAsync);
    		return Task.CompletedTask;
    	}
    	private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    	{
    		// return our reply to the user
    		await context.PostAsync("Hello");
    		context.Wait(MessageReceivedAsync);
    	}
    }
  11. Finally, add a MessagesController to be the entry point for REST calls into your bot. The Controller can handle system events here and pass new messages off to the Dialog class.
    [BotAuthentication]
    public class MessagesController : ApiController
    {
    	/// <summary>
    	/// POST: api/Messages
    	/// Receive a message from a user and reply to it
    	/// </summary>
    	public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    	{
    		if (activity.Type == ActivityTypes.Message)
    		{
    			await Conversation.SendAsync(activity, () => new BusDialog());
    		}
    		else
    		{
    			HandleSystemMessage(activity);
    		}
    		var response = Request.CreateResponse(HttpStatusCode.OK);
    		return response;
    	}
    	private Activity HandleSystemMessage(Activity message)
    	{
    		if (message.Type == ActivityTypes.DeleteUserData)
    		{
    			// Implement user deletion here
    			// If we handle user deletion, return a real message
    		}
    		else if (message.Type == ActivityTypes.ConversationUpdate)
    		{
    			// Handle conversation state changes, like members being added and removed
    			// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
    			// Not available in all channels
    		}
    		else if (message.Type == ActivityTypes.ContactRelationUpdate)
    		{
    			// Handle add/remove from contact lists
    			// Activity.From + Activity.Action represent what happened
    		}
    		else if (message.Type == ActivityTypes.Typing)
    		{
    			// Handle knowing tha the user is typing
    		}
    		else if (message.Type == ActivityTypes.Ping)
    		{
    		}
    		return null;
    	}
    }
  12. Notice the Post method, which gets called whenever you get a new message. It is also called when you get a system message such as a state change, a user typing a message, or a user being removed. Notice that if the activity is a new Message, it’s passed to the BusDialog class we created earlier.
  13. You may have noticed that our Controller was secured with a BotAuthentication attribute. To make sure only your Azure Bot Service is able to call your API, go to the Settings tab under your bot service in the Azure portal and click the Manage Microsoft App ID and Password button. Copy the AppId and Password you generate on this page and put it in the AppSettings section of your App.config file like this:
    <add key="MicrosoftAppId" value="[YOUR APPID HERE]" />
    <add key="MicrosoftAppPassword" value="[YOUR PASSWORD HERE]" />
  14. Now, deploy your app to the Azure web service you already configured when you setup the new Bot Service. If you used the downloaded project template, it should be connected already, if not, you will need to create a new publish profile.


  15. With your Bot Service live on Azure, you now simply need to set up the platforms (i.e., “channels”) you’ll publish this bot through. I choose Skype, Slack, and Twillio (SMS) for my service. Click the Channels tab and the UI will walk you through the sets for each channel. Typically, you’ll need to sign up as an app on each platform and get an ID and a Password/Secret from each one to paste into your Azure Bot’s channel configuration so it knows how to securely communicate with that platform.
  16. You now have everything in place to test our your platform. Here are some screenshots of my bot working on some different channels.

Skype

Slack

SMS (via Twillio)

Want to try it for yourself? You can download the full project from my github.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

This field is for validation purposes and should be left unchanged.

Thank you message.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.