Building a basic bot for Teams

Microsoft Teams is a real game changer in the way that, certainly within my organisation, teams work together. It allows us to communicate and collaborate efficiently as small and large teams regardless of where we’re located geographically. But with all technology now, there’s always the question of “what else can it do?” being asked, and in reality the answer comes down to your imagination. Being able to build a bot and access it from Microsoft Teams means that I can build interaction into the application without having to build a whole new user interface. I can use the chat window to have a conversation and trigger some functionality from that.

The way that we do that is by creating a bot which can be used as an interface into a defined piece of functionality. It doesn’t matter if this functionality is to something within O365 or whether it is something in another ecosystem, the point of interaction is the same: The chat window in Microsoft Teams.

When I first started looking at this I found it difficult to find all of the relevant information that I needed to create a Bot in one place, therefore this blog will get you started with creating a basic bot for Microsoft Teams.

As an overview this will involve:

  • Setting up Azure Resources
  • Adding the Bot to Teams
  • Configuring Continuous Deployment
  • Changing the code

Setting up Azure Resources

I initially did this using free credit from my MSDN subscription, but this could be done in any Azure subscription.

Once you’re into your Azure portal, I created a new Bot Service. This asks for the usual information about the service plan that is going to be used to provision the service, location if required, and name. Previously, while in preview, you would have to generate a new App ID, and App Password. Azure now does all of this for you, and makes it really easy to get your basic bot up and running.

Step-by-Step

  1. Navigate to the Azure Portal (https://portal.azure.com) and log in
  2. Select New
  3. Search for Bot
  4. Select Web App Bot and click Create
  5. Enter the required information:
    1. Bot name
    2. Subscription
    3. Resource Group – choose if you’re going to create a new Resource Group or use an existing one
    4. Location – which data centre you want your bot to be hosted in
    5. Pricing Tier
    6. App Name – This will default to that of your Bot name and will become the host URL for the background web application
    7. Bot Template – This is (for me) the most exciting part of this form because there are some cool templates in there to get you up and running. Also this is where you select your base technology: C# or Node. As my background is .NET I’ll be going for C#. There are a number of templates which you can select from which I’ll explore in another blog. For this example I’m going to select Basic.
      2018-02-01 (3)
    8. App Service Plan/location
    9. Azure Storage – the associated BLOB, Storage and Queue
    10. Application Insights and Insights Location

Once you have hit create, Azure will go off and will provision all of the required assets for you. Microsoft have made some great advances in this area in terms of the options that you have on Blade. You can “Test in Web Chat” which allows you to go and start chatting and seeing what your bot is going to do. It’s cool to start with but you soon get bored of talking to yourself.

Add the Bot to Teams

In order to start using the bot in teams we need to add it as an active channel. This is achieved on the blade for your Bot in the Azure Portal. Under Bot Management, select Channels.

You’ll see at the top that Web Chat is already a channel which is running, which we know because we’ve already tested it. We are now interested in what other channels we can add, and for this demo we’re going to add Microsoft Teams. To add it, simply click it, and agree to the Terms of Service. That’s it, assuming that you don’t change the “Enabled” toggle at the bottom of the screen, we’re ready to start interacting with the Bot in Microsoft Teams.

2018-02-01 (6)

Once you’re back at the Channels screen, you can easily just click the Teams logo and Microsoft Teams will fire up with a chat panel ready for you to start chatting with your bot. This is obviously only a 1-2-1 chat at the moment, whereas we want to add it to a team so that others can start using it as well. This is done through sideloading the app, which is covered in a really good blog by Cerbu Andrei:

https://blogs.technet.microsoft.com/exovoice/2017/10/26/add-a-microsoft-teams-bot-in-channels/

Alternatively you can publish the Bot to the store and you can add it that way.

Adding Functionality

Continuous Deployment

There are a couple of ways in which you can modify the bot, the first is by configuring Continuous Deployment from Visual Studio Team Services. This is my favourite approach, I love being able to check my code in and VSTS does what it needs to do and deploys my changes up for me.

First of all I’m going to create myself a VSTS project where I can store my code, and then I’m going to configure Continuous Deployment from Azure.

To configure Continuous Deployment, navigate back to your Bot in Azure and

  1. Download the Zip file – this will download the basic solution which is deployed as your Bot. Not the most elegant start, but we need to get the code somehow.
  2. Configure your IDE. I quite like using Visual Studio Code, so if you want to set up using this please see the following blog:
    https://www.bytesizedalex.com/visual-studio-code-and-team-services-integration/
  3. Drop your code into you project repo and commit + pull your changes to VSTS so that you now have your code base in the cloud
  4.  Return to your bot in the Azure Portal
  5. Click on “Step 3: Configure continuous deployment”
  6. Click on Set up so that we open the deployment options blade
    1. Choose your source. In this case I’m using VSTS, but as you can see there are more options available
      2018-02-01 (14)
    2. Configure your connection by choosing a project and the branch which you want to take your “live” code from
    3. Click OK
    4. Azure will then go and configure the connection for you.
      2018-02-01 (15)
  7. Assuming that your connection is successful, you will then see that your code is building from your repo and eventually it will mark it as active.
    2018-02-01 (16)

If you want to double check that everything is still working, then you can go back to Test in Web Chat and just ensure that your bot is still responding to chat requests.

Start Building

Now that I’ve got my IDE connected to my source control, and my source control connected to my Bot, I just want to make a couple of small changes to prove the end-to-end process.

Return to your IDE and locate the file ECHODIALOG.cs in the dialogs folder.

2018-02-01 (19)

I’m going to make a very small change such as changing “You said” to “Simon said”.

2018-02-01 (20)

I’ll stage, commit and push that change into VSTS so that the repo is updated. Going back to the continuous build screen for my Bot I can now see that a new version is building with my comments associated.

2018-02-01 (21)

So once again, back to Test in web chat and I can check that my bot is now playing Simon says.

2018-02-01 (22)

Then I can look in Teams again and see the same change reflected there.

2018-02-01 (23)

Conclusion

So now we’ve gone through all of the steps that we need to get our basic bot up and running, it now really comes down to some imagination and time coding to implement a bot that can add some real value to your users.

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s