Chapter 9: Building Your Own Bot for Microsoft Teams – Hands-On Microsoft Teams

Chapter 9: Building Your Own Bot for Microsoft Teams

If you were to say in your organization that you were going to build a bot to automate any tasks, I’m sure you would gain the attention of everyone in the room. Bots are powerful automated agents that can help you to do your daily tasks in an efficient way using natural language.

Even though it sounds futuristic and complex, building a bot is quite easy and does not require a single line of code. It is also quite easily accessible to everyone. By the end of this chapter, you will hopefully be the Microsoft Teams superhero of your organization.

The topics you will learn about in this chapter are as follows:

  • Creating and packaging a bot using QnA Maker
  • Creating and packaging a bot using Power Virtual Agents
  • Using a bot in Microsoft Teams

Creating a bot using QnA Maker

Creating a bot for Microsoft Teams is only a few clicks away from any citizen developer using Microsoft QnA Maker, Microsoft Azure, and Microsoft Teams App Studio.

Microsoft QnA Maker is a service that allows you to build and publish bots without writing code. Using an intelligent extraction system, it is able to get the information to be used by the bot from an Excel spreadsheet, a Word document, or even a website.

Before we begin creating our bot, you will need to have access to your organization’s Microsoft Azure and you must be able to deploy custom apps on Microsoft Teams.

Peter – HR manager

Peter manages HR in his organization and spends most of his day replying to questions from other employees. To save him time, Peter has compiled questions and replies in an Excel file, from which he copies them into Microsoft Teams.

To get more time to do his daily tasks, Peter wants to build a Microsoft Teams bot to reply to questions automatically, and to achieve this, he will use Microsoft QnA Maker.

If, like Peter, you see yourself building a bot, follow these steps and you will be able to do it in under 60 minutes. So let’s begin:

  1. Start by opening QnA Maker – https://www.qnamaker.ai/.
  2. Click on Sign in and authenticate using your corporate account:

    Figure 9.1: QnA Maker website

  3. Once authenticated, in the black bar at the top, click on Create a knowledge base. This action will redirect you to a five-step process in which you will create the intelligence for your bot.
  4. In STEP 1 Create a QnA service in Microsoft Azure, click on the Create a QnA service button. This action will open Microsoft Azure and, if you're not yet authenticated, you must sign in with your corporate account:

    Figure 9.2: Create a QnA service

  5. In Azure, you will be presented with a form similar to the one in the following screenshot, which you must fill in to create your QnA service:

    Figure 9.3: QnA creation form

    (a) Name: The name that will identify your QnA service on Azure.

    (b) Subscription: If you have access to more than one Azure subscription, select the one you want to use here.

    (c) Pricing tier: Select the pricing tier. There are several options available. Choose one according to the load you estimate for your bot. The F tier is free and gives you 3 transactions per second and a maximum of 50,000 transactions per month. You can start with the free instance and adjust it later if needed.

    (d) Resource group: The resource group where you want to create your service. Resource groups on Azure are typically used to group resources related to the same application or to separate resources from production and development environments.

    (e) Azure Search pricing tier: Azure Search is a cognitive service that will be used by the bot. This is the service that handles the natural language and retrieves the results even if the question is not an exact match to what was provided to the QnA service. The F tier is free. It gives you 50 MB of storage. If needed, you can adjust it later to a paid tier with more storage space.

    (f) Azure Search location: The data center where your service will be running. It’s recommended to select the one closer to the physical location of your organization.

    (g) App Name: The name of your app service. By default, it will have the same name as the QnA service, but you can change it if needed.

    (h) Website location: The data center where your website will be located. It’s recommended to select the one closest to the physical location of your organization.

    (i) App insights: App insights is a tool that helps you to understand how your app is being used. It will monitor the usage of the application so you will know what your users are doing with your bot.

  6. Click on Create. The service will take a few minutes to get created. Once it’s available, you will receive a notification on Azure.

    Go back to the QnA Maker portal, scroll down to STEP 2 Connect your QnA service to your KB, and click the Refresh button. This will establish a connection with Azure and will get the information about the service you have just created. Select the service as shown in the following screenshot:

    Figure 9.4: QnA connection

  7. In STEP 3 Name your KB, provide a name for your knowledge base. This can be changed at any time if needed:

    Figure 9.5: Knowledge base name

  8. In STEP 4 Populate your KB, provide the data that your bot will use to answer the questions. This supports existing websites that will be parsed by the system to get questions, documents, and Excel spreadsheets.
  9. Click on Add file and upload your Excel file. The following screenshot has an example of how simple your spreadsheet can be, with just two columns. In column A, we have the questions and in column B, we have the answers:

    Figure 9.6: Excel questions and answers

  10. Choose the Chit-chat option. By default, this option is off, but you can enable it to allow the bot to reply to questions that you have not planned to answer. There are five different types that you can choose from. Make sure you pick one that matches the culture of your organization:

    Figure 9.7: Knowledge base settings

  11. In STEP 5 Create your KB, click on Create your KB:

    Figure 9.8: Create knowledge base

  12. After a few seconds, you will be redirected to the knowledge base EDIT panel and you will be able to review and add alternative phrases to your questions and answers.
  13. Click on Save and train, to save any modifications you may have made to the system:

    Figure 9.9: Knowledge base review

  14. In the black bar at the top, click on PUBLISH and then click on the Publish button:

    Figure 9.10: Publish knowledge base

  15. After a few seconds, you will be redirected to a success page. On this page, click on the Create Bot button. This will redirect you to Azure again, where you will need to create the bot itself:

    Figure 9.11: Deployment success

  16. The form on Azure is automatically filled; you just need to validate the data and make the necessary adjustments. Since this is a no-code solution, you can ignore the option selected for SDK language:

    Figure 9.12: Create the bot

  17. Click on Create. This process will take a few minutes. Once the deployment finishes, you will get a notification on Azure.
  18. On the Azure notification, click on the Go to resource button. This will open the setup page of the bot framework:

    Figure 9.13: Bot framework setup

  19. On the vertical menu, click on Test in Web Chat and check whether your bot works as you expect:

    Figure 9.14: Test in Web Chat

  20. On the vertical menu, click on Channels and then click on the Microsoft Teams logo:

    Figure 9.15: Add a Microsoft Teams channel

  21. Click on Save to create a new channel for Microsoft Teams. This action will allow you to test your bot on Microsoft Teams.

Creating a QnA bot is accessible to all users if they have access to their organization’s Microsoft Azure subscription. Also, it can be used to automate processes that are currently being done manually, wasting the productive time of all collaborators.

Packaging the bot using QnA Maker

To make your bot available to all users in the organization, you have to package it as an app. In this section, you will learn what steps are needed to package your first bot for Microsoft Teams.

Note

If you are not yet familiar with Microsoft Teams App Studio, it is recommended to have a look at Chapter 8, Build Your Own App for Microsoft Teams Using App Studio.

Let’s get right to it:

  1. Open App Studio on Microsoft Teams.
  2. Click on the Manifest editor tab.
  3. Click on the Create a new app button:

    Figure 9.16: Create a new app

  4. Fill in the App details form. A detailed description of this form can be found in Chapter 8, Build Your Own App for Microsoft Teams Using App Studio.
  5. On the Capabilities group click on Bots.
  6. To start the connection between your bot and your Microsoft Teams app, click on Set up:

    Figure 9.17: Setup bot

  7. On the Set up a bot popup, click on the Existing bot tab:

    Figure 9.18: Connect to a bot

  8. Select Connect to a different bot id and open Azure to get the ID of your bot.
  9. On Azure, click on All services.
  10. In the Overview menu, click on AI + machine learning.
  11. Click on the name of your newly created bot:

    Figure 9.19: Web App Bot

  12. On the App Service Settings group, click on Configuration.
  13. On the Application settings tab, look for the value MicrosoftAppId.
  14. Click on it and copy its value:

    Figure 9.20: Copy bot ID

  15. Paste the ID from Azure in the Bot ID field in Microsoft Teams App Studio.
  16. On the scope option, select where you want to make it available. You can choose Personal, Team, or Group chat. Choose according to the features and the intent of your bot. The more options you select, the more possibilities you will give to your users to interact with it.
  17. Click on Save.
  18. On the Finish step, click on Test and distribute.
  19. Click on Download to get the application package:

    Figure 9.21: Download the bot app

  20. Open Microsoft Teams Apps Store.
  21. Click on Upload a custom app, and select the upload for the organization to make it available to all users.
  22. Once installed, click on the app and then click Add. To learn more about how to install custom apps on Microsoft Teams, read Chapter 6, Extending Microsoft Teams Using Apps.

After completing these steps, your bot has been built with QnA Maker on Azure, and that was done without writing a single line of code. Similar to how we created a bot using QnA Maker, in the next section, we will see how to do so using another tool.

Creating a bot using Power Virtual Agents

Microsoft has different tools that allow you to rapidly create bots without writing code. We looked at QnA Maker in the previous section. Now we will have a look at Power Virtual Agents.

Note: Power Virtual Agents licensing

Power Virtual Agents is not included in Microsoft 365 plans – it requires a separate license for it to work. More information about prices can be found on the Power Virtual Agents website at https://powervirtualagents.microsoft.com/.

To get started with Power Virtual Agents, do the following:

  1. Open the sign-in page: https://powervirtualagents.microsoft.com.
  2. Sign in if you already have a license or start a trial by clicking on Sign up free or Start free:

    Figure 9.22: Sign in to Power Virtual Agents

  3. On the Create a new bot popup, give your bot a name:

    Figure 9.23: Create a new bot

  4. Click on Create and wait a few seconds for the process to finish. Once it’s created, you will enter Power Virtual Agents:

    Figure 9.24: Power Virtual Agents layout

  5. On the vertical menu, click on Topics. You will notice that the topics are already pre-populated with lessons that will help you to understand how Power Virtual Agents work with conditions and variables:

    Figure 9.25: Topics

  6. To add your own topic, click on New topic.
  7. Provide a name and a description for your topic.
  8. Enter a trigger phrase and click on Add. Try to start with 5-10 diverse phrases.
  9. Click on Go to authoring canvas to start building your topic:

    Figure 9.26: Create a new topic

  10. Click on the + icon to add a node. Nodes can be messages, questions, or conditions:

    Figure 9.27: Authoring canvas

  11. Build your topic as illustrated in the following screenshot and save it.
  12. Test it in the test bot. The reply path will be highlighted in the build area:

Figure 9.28: Test bot

Create as many topics as you need for your bot until it is ready to be added to Microsoft Teams.

Packaging the bot using Power Virtual Agents

The process to get your new bot packaged and installed on Teams is similar to the one we have seen already in the previous section, Packaging the Bot using QnA Maker. The main difference is in the steps required to get the bot’s ID.

To get the bot’s ID to package it into a Microsoft Teams app, do the following:

  1. In the vertical menu, click on Publish.
  2. In the Publish window, click on the Publish button. The publication will take 15 minutes:

    Figure 9.29: Publish

  3. Scroll down and click on the Go to channels link.
  4. Click on Microsoft Teams:

    Figure 9.30: Channels

  5. Click on Add to generate a new App ID.
  6. Click on Copy to copy the app ID to the clipboard:

    Figure 9.31: App ID

  7. Follow the steps described in the Packaging the bot created using QnA Maker section (steps 1 to 7 and steps 19 to 22) to get the bot installed on Microsoft Teams.

Once packaged and installed, you will be able to interact with your virtual agent, as illustrated in the following screenshot:

Figure 9.32: Power Virtual Agents bot running on Microsoft Teams

With Power Virtual Agents, you will be able to create complex scenarios for your bot without writing code. The graphical user interface allows you to easily see how things are working and how you can improve the replies of the bot.

Now that our bot is created and packaged, we are now ready to use it.

Using the bot on Microsoft Teams

Now that you have the bot installed on your tenant, you need to know how other members of the organization can interact with it.

Let’s now see how to use the bot as a personal app:

  1. You will need to click on to expand the More apps section and get access to it:

Figure 9.33: Installed bot on Microsoft Teams

Once you open the bot, you will be able to ask it private questions in a 1:1 chat without even mentioning the bot. Just type your question and wait for it to reply.

As you can see in the following screenshot, we have enabled the Chit-chat option so the bot is able to reply to questions other than the ones that were uploaded from the spreadsheet. This will make the user more comfortable when interacting with the bot:

Figure 9.34: Bot interaction

The bot can also be used in the context of chats in channels and group chats. To use it, all you need to do is to mention it. Mentions on Microsoft Teams start with the @ sign followed by the name of the bot or the person.

In the following screenshot, you can see the bot being mentioned in a channel. The use of the bot in a channel allows you to keep a conversation about the same topic in one thread, as also illustrated in the screenshot:

Figure 9.35: Bot mention

Summary

In this chapter, you have learned how to build your own bot with artificial intelligence and without writing a single line of code. You have also learned how to package the bot into a Microsoft Teams app, how to make it available in the tenant, and about the ways you can use it to interact with bots.

The last chapter of this book is for users that have an administrator role in their organization. It will cover Microsoft Teams PowerShell and will explain how you can automate tasks such as team creation in your organization using scripts.