Introduction

Bot Service provides an integrated environment that is purpose-built for bot development, enabling you to build, connect, test, deploy, and manage intelligent bots, all from one place. Bot Service leverages the Bot Builder SDK with support for .NET and Node.js. You can write a bot, connect, test, deploy, and manage it from your web browser with no separate editor or source control required.

For simple bots, you may not need to write code at all. Bot Service accelerates bot development with five bot templates you can choose from when you create a bot. You can further modify your bot directly in the browser using the Azure editor or in an Integrated Development Environment (IDE), such as Visual Studio and Visual Studio Code

Here are some key features of Bot Service,

In this post, I will show how to create Azure bot that is integrated with Entity Framework to retrieve the employee vacation balance from Azure SQL database.

Prerequisites

  1. Visual Studio 2017.
  2. In Visual Studio, update all extensions to their latest versions.
  3. Bot template for C#.

Create Chat Bot

First, you'll need to download and install the Bot template, to download click on the following URL: https://marketplace.visualstudio.com/items?itemName=BotBuilder.BotBuilderV3

After the download completes, double-click on BotBuilderVSIX.vsix and install it.

Then, you'll need to download and install the emulator. Open the following URL https://emulator.botframework.com/ to download the emulator. After the download completes, launch the executable and complete the installation process.

Now, open Visual Studio, select Bot framework, change the name, and click OK.

Create your own Azure Chat Bot

Now, we need to create Employee Order form to collect the employee select service. To do that create a new class named EmployeeOrder.cs and replace the original class code with the following one.

  1. public enum ServiceType
  2. {
  3. GetVacationBalance,
  4. LeaveRequest
  5. }
  6. [Serializable]
  7. public class EmployeeOrder
  8. {
  9. [Describe(Description = "Service", Image = "")]
  10. [Prompt("Please select the service you want. {||}")]
  11. public ServiceType? Service { get; set; }
  12. public static IForm<EmployeeOrder> BuildForm()
  13. {
  14. //PromptAttribute
  15. return new FormBuilder<EmployeeOrder>()
  16. .Field(nameof(Service))
  17. .Confirm("No verification will be shown", state => false)
  18. .Build();
  19. }
  20. }

Next, we need to create a Vacation Form by creating Vacation.cs class.

  1. [Serializable]
  2. public class Vacation
  3. {
  4. [Describe(Description = "Employee ID")]
  5. [Numeric(1, 100000)]
  6. [Prompt("Please give me your employee identification number")]
  7. public int? EmployeeId { get; set; }
  8. public static IForm<Vacation> BuildForm()
  9. {
  10. //PromptAttribute
  11. return new FormBuilder<Vacation>()
  12. .Message("Your Select Vacation Balance Service")
  13. .Field(nameof(EmployeeId))
  14. .Confirm("No verification will be shown", state => false)
  15. .Build();
  16. }
  17. }

Next, we need to connect the form to the framework. Open MessagesController and then add the following methods.

  1. public static IDialog<EmployeeOrder> MakeEmployeeDialog()
  2. {
  3. return Chain.From(() => FormDialog.FromForm(EmployeeOrder.BuildForm));
  4. }
  5. public static IDialog<Vacation> MakeVacationDialog()
  6. {
  7. return Chain.From(() => FormDialog.FromForm(Vacation.BuildForm));
  8. }

Next, we need to connect to the database by using Entity Framework by creating the data context and entity classes.

First, we need to create EmployeeContext class.

  1. public EmployeeContext():base("name=dbCs")
  2. {
  3. }
  4. public DbSet<Employee> Employees { get; set; }

Next, create the Employee class.

  1. public class Employee
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public DateTime JoinDate { get; set; }
  6. public decimal VacationBalance { get; set; }
  7. public DateTime UpdateDate { get; set; }
  8. public decimal Salary { get; set; }
  9. }

Then, add the connection string in web.config.

  1. <connectionStrings>
  2. <add name="dbCs" connectionString="Data Source=.;Initial Catalog=EmployeeDb;User ID={UserId};Password={password};MultipleActiveResultSets=True"
  3. providerName="System.Data.SqlClient" />
  4. </connectionStrings>

Next, open RootDialog class and add the following code.

  1. private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
  2. {
  3. var activity = await result as Activity;
  4. var _order = activity.Text.ToLower();
  5. // defined welcome message
  6. var _WelcomeMsg2 = context.MakeMessage();
  7. _WelcomeMsg2.Attachments.Add(new Attachment()
  8. {
  9. ContentUrl = "https://cdn.dribbble.com/users/722835/screenshots/4082720/bot_icon_teaser.gif",
  10. ContentType = "image/gif",
  11. Name = "HR Bot"
  12. });
  13. _WelcomeMsg2.Text = $"Hello. How I can help you?";
  14. // Return our reply to the user
  15. await context.PostAsync(_WelcomeMsg2);
  16. // Go to Employee Dialog
  17. var _employeeServiceDialog = MessagesController.MakeEmployeeDialog();
  18. await context.Forward(_employeeServiceDialog, this.ResumeAfterNewOrderDialog, activity, CancellationToken.None);
  19. }
  20. private async Task ResumeAfterNewOrderDialog(IDialogContext context, IAwaitable<EmployeeOrder> result)
  21. {
  22. // Store the value that NewOrderDialog returned.
  23. // (At this point, new order dialog has finished and returned some value to use within the root dialog.)
  24. var resultFromNewOrder = await result;
  25. switch (resultFromNewOrder.Service.Value)
  26. {
  27. case ServiceType.GetVacationBalance:
  28. // Your select Vacation Balance
  29. // Go to Vacation Dialog
  30. var _VacationDialog = MessagesController.MakeVacationDialog();
  31. await context.Forward(_VacationDialog, ResumeAfterVacationBalanceDialog, null, CancellationToken.None);
  32. break;
  33. case ServiceType.LeaveRequest:
  34. await context.PostAsync($"This Service is Not Yet Available!");
  35. PromptDialog.Confirm(
  36. context,
  37. AfterConfirmAsync,
  38. "Do you need any other service?",
  39. "Didn't get that!",
  40. promptStyle: PromptStyle.Keyboard);
  41. break;
  42. }
  43. }
  44. private async Task ResumeAfterVacationBalanceDialog(IDialogContext context, IAwaitable<Vacation> result)
  45. {
  46. var resultFromVacation = await result;
  47. // wating till get employee vacation balance from database.
  48. var _msgWait = context.MakeMessage();
  49. _msgWait.Type = ActivityTypes.Typing;
  50. _msgWait.InputHint = InputHints.IgnoringInput;
  51. await context.PostAsync(_msgWait);
  52. // create new Data Context object from EmployeeContext Class
  53. EmployeeContext db = new EmployeeContext();
  54. var employee = await db.Employees.FindAsync(resultFromVacation.EmployeeId.Value);
  55. // check if the user exist
  56. if (employee != null)
  57. {
  58. var _msg = context.MakeMessage();
  59. //To create an Adaptive Card using .NET, install the Microsoft.AdaptiveCards NuGet package.
  60. AdaptiveCard card = new AdaptiveCard();
  61. // Add text to the card.
  62. card.Body.Add(new TextBlock()
  63. {
  64. Text = "Your Vacation Balance is:",
  65. Size = TextSize.Large,
  66. Weight = TextWeight.Bolder
  67. });
  68. // Add text to the card.
  69. card.Body.Add(new TextBlock()
  70. {
  71. Text = $"{employee.VacationBalance.ToString("G2")} Days",
  72. Weight = TextWeight.Bolder
  73. });
  74. // Create the attachment.
  75. Attachment attachment = new Attachment()
  76. {
  77. ContentType = AdaptiveCard.ContentType,
  78. Content = card
  79. };
  80. _msg.Attachments.Add(attachment);
  81. await context.PostAsync(_msg);
  82. }
  83. else
  84. {
  85. await context.PostAsync($"There is no employee with this #: [{resultFromVacation.EmployeeId}].");
  86. }
  87. PromptDialog.Confirm(
  88. context,
  89. AfterConfirmAsync,
  90. "Do you need any other service?",
  91. "Didn't get that!",
  92. promptStyle: PromptStyle.Keyboard);
  93. }
  94. private async Task AfterConfirmAsync(IDialogContext context, IAwaitable<bool> result)
  95. {
  96. var confirm = await result;
  97. if (confirm)
  98. {
  99. // Go to Employee Dialog
  100. var _employeeServiceDialog = MessagesController.MakeEmployeeDialog();
  101. await context.Forward(_employeeServiceDialog, this.ResumeAfterNewOrderDialog, null, CancellationToken.None);
  102. }
  103. else
  104. {
  105. await context.PostAsync("Thanks. Goodbye.");
  106. context.EndConversation("");
  107. }
  108. }

Now, we need to test our bot, build the solution, open the bot file [filename.bot], and modify the endpoint and then click Save.

Create your own Azure Chat Bot

From debug, click start debugging or press F5.

Next, open the Bot emulator. On the welcome screen, click on "Open bot".

Create your own Azure Chat Bot

In open dialog, find the bot file [fileName.bot] which will be located in the same project files.

Create your own Azure Chat Bot

Create your own Azure Chat Bot

Now, the bot is ready to test. We need to type any message to start the chatting.

Create your own Azure Chat Bot

Next, we need to deploy the bot to Azure. Right-click on the project and select "Publish".

Create your own Azure Chat Bot

Select "Create new" then click "Publish".

Create your own Azure Chat Bot

In the "Create App Service" dialog, fill the app name, subscription, resource group, and hosting plan and then click on the "Create" button.

Create your own Azure Chat Bot

After deploying process is completed, open https://portal.azure.com, open resource groups, HRBot, and verify you find the app service

Create your own Azure Chat Bot

From the left menu create on create a resource.

Create your own Azure Chat Bot

From AI + Machine Learning select Bot Channels Registration then click create.

Create your own Azure Chat Bot

In Bot Channels Registration dialog fill in all required fields, for messaging endpoint type the app service URL “https://appservise_url/api/messages” then click create.

Create your own Azure Chat Bot

Next, we need to get app id and password, go to Bot Channels Registration and open it, go to settings

Create your own Azure Chat Bot

Then click on Microsoft App ID (Manage) link, click on generate new password then copy it.

Create your own Azure Chat Bot

B=Go back to Bot Channels Registration settings and copy app id. Then Click save.

New open bot app service, go to application settings, under Application settings add the following keys,

Create your own Azure Chat Bot

Under Connection strings add the connections string then click save.

Create your own Azure Chat Bot

Back to Bot Channels Registration, click on Test on Web Chat,

Create your own Azure Chat Bot

Now we need to add a new channel, click on channels, then click on Skype,

Create your own Azure Chat Bot

Fill in the required fields and then click save. Now click on the Skype icon,

Create your own Azure Chat Bot

Create your own Azure Chat Bot

Click add to contacts to open on Skype.

Create your own Azure Chat Bot