Channel Configuration - Azure Bot Service To Slack Application

Introduction
 
This article explains how to configure Azure Bot Service to Slack Applications. So, before reading this article, please read our previous article related to Create And Connect A Chat Bot With Azure Bot Service. Then, we will get a clear idea of how to create a Bot service in Azure.
 
Create a Web App Bot in Azure
 
Click on "New" on the left side menu and it will open an Azure Marketplace. There, we can see the list of services. Click "AI + Cognitive Services" then click on the "Web App Bot" for your bot service app.
 
 
Bot Service
 
Fill the following details and add the location based on your client location or your Geolocation.
 
 
 
Once the build is successful, click on the "Dashboard" and we can see that the "menothbotdemo" bot is created in the All resources list. Bot is ready for use!
 
 
 
Create a Slack Application for our Bot
 
First, we need to create a workspace in Slack Account. Check the following link to create a Slack Account: New slack account.
 
Create an app and assign a Development Slack team or Slack Workspace

Click on the URL https://api.slack.com/apps. Then, click on the "Create New App" !!.
 
Once the Slack workspace is created, then only we can create a slack application under the Workspace. Now, we are going to create and assign our slack app name into the Workspace. We have given our App a name as "menothbotdemo".
 
 
 
Click on the "Create App" button. Then, Slack will create our app and generate a Client ID and Client Secret. We can use these IDs for channel configuration in Azure Web App bot.
 
Add a new Redirect URL
 
Click on the "OAuth & Permission" tab in the left panel. Then, add the redirect URLs as "https://slack.botframework.com" and save it properly.
 
 
Create Bot Users
 
Click on the "Bot Users" tab in the left panel. Then, click on "Add a Bot User". In this section, we can give our bot "Display name". For example, we created our bot user's name as "menothbotdemo". If we want our bot to always show as Online, then click on the "On" button. After that, click "Add Bot User" button.
 
 
Event Subscriptions
  1. Select "Event Subscriptions" tab in the left panel.
  2. Click Enable Events to On.
  3. In the "Request URL" we need to add the following URL to our "Bot Handle Name".

    https://slack.botframework.com/api/Events/{bot handle name}

    The "Bot Handle" name we will get inside the "Web App Bot ( we created our web app as "menothbotdemo")" Settings.



    Finally, we can add the Request URL inside the Event Subscriptions.


  4. In Subscribe to Bot Events, click "Add Bot User Event".

  5. In the list of events, click "Add Bot User Event" and select the following event name.



  6. Click "Save Changes". 
Configure Interactive Messages ( Optional )
  1. Select the "Interactive Components" tab and click "Enable Interactive Components".
  2. Enter https://slack.botframework.com/api/Actions as the request URL.
  3. Click the "Enable Interactive Messages" button, and then click the "Save Changes" button.

App Credentials
 
Select the "Basic Information" tab and then we will get the ClientID & Client Secret & Verification Token for our channel configuration in Azure Bot Service.
 
 
 
Channel Configuration
 
There is a very simple way to connect our bot service app to Slack in Azure. Just follow the following steps.
 
Click on the "Channels" menu on the left side option. Then, it will open a window with channel details where you can see "More channels" options. Then, select "Slack" in the channels list.
 
 
Add the following Slack App ( Already Created Slack App ) credentials into the Azure Slack configuration section.
  • ClientID
  • Client Seceret
  • Verification Token 
 
Once the configuration is done, we can see our Slack configured into the channel.
 
 
C# Code
 
We have done some changes in the default code in bot service.
  1. using System;  
  2. using System.Threading.Tasks;  
  3.    
  4. using Microsoft.Bot.Connector;  
  5. using Microsoft.Bot.Builder.Dialogs;  
  6. using System.Net.Http;  
  7.    
  8.    
  9. namespace Microsoft.Bot.Sample.SimpleEchoBot  
  10. {  
  11.     [Serializable]  
  12.     public class EchoDialog : IDialog<object>  
  13.     {  
  14.         protected int count = 1;  
  15.    
  16.         public async Task StartAsync(IDialogContext context)  
  17.         {  
  18.             context.Wait(MessageReceivedAsync);  
  19.         }  
  20.    
  21.         public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)  
  22.         {  
  23.             var message = await argument;  
  24.    
  25.             if (message.Text == "reset")  
  26.             {  
  27.                 PromptDialog.Confirm(  
  28.                     context,  
  29.                     AfterResetAsync,  
  30.                     "Are you sure you want to reset the count?",  
  31.                     "Didn't get that!",  
  32.                     promptStyle: PromptStyle.Auto);  
  33.             }  
  34.             else if (message.Text == "Hi")  
  35.             {  
  36.                 await context.PostAsync($"{this.count++}: Slack Configured in Bot App !!");  
  37.                 context.Wait(MessageReceivedAsync);  
  38.             }  
  39.             else  
  40.             {  
  41.                 await context.PostAsync($"{this.count++}: You said {message.Text}");  
  42.                 context.Wait(MessageReceivedAsync);  
  43.             }  
  44.         }  
  45.    
  46.         public async Task AfterResetAsync(IDialogContext context, IAwaitable<bool> argument)  
  47.         {  
  48.             var confirm = await argument;  
  49.             if (confirm)  
  50.             {  
  51.                 this.count = 1;  
  52.                 await context.PostAsync("Reset count.");  
  53.             }  
  54.             else  
  55.             {  
  56.                 await context.PostAsync("Did not reset count.");  
  57.             }  
  58.             context.Wait(MessageReceivedAsync);  
  59.         }  
  60.    
  61.     }  
  62. }  
Output
 
 
 
Reference
See Also
 
You can download other ASP.NET Core source codes from MSDN Code, using the link, mentioned below.
Summary
 
We learned how to configure Azure Bot Service to Slack application. I hope this article is useful for all Azure beginners. 


Similar Articles