Menu Home

Microsoft Teams Custom Bots with Azure Functions

Build is over, now it is time to play with all the new features that was announced.

Microsoft teams was one of the product that got a new handful of useful features. The list of customizations options for teams are now even longer, check them out here: https://msdn.microsoft.com/en-us/microsoft-teams/

The one new feature that caught my attention was custom bots, bots for teams is not new, but until now you have only been able to talk to the bot in a 1:1 dialog. But with custom bots you can have a bot that is part of a teams channel, much like it is known from slack where that type of bots are very popular.

Installing a team bot is a simple task, you just edit the team and add the bot to it. Two types of channel bots exist, published bots, from other companies and custom bots that you build yourself, and only lives in your team.

When you chose to add a custom bot, you provide a botname (which will also be the handle you use to communicate with the bot). A callback url (https) which is where teams should send the message when you talk to the bot, and you must reply in less than 5 sec. A description and an image.

Once you have registered your bot you get a client secret, save this if you want to secure your callback URL so only your teams can call the API.

Once the bot is installed, you talk to it is to use @botname . You can use the Microsoft Bot Framework to build custom bots, and teams allows your bot to use many of the messaging capabilities that exists in the framework, like hero cards, carrousels, buttons and more.

Microsoft released a WebAPI project that shows how you could build a teams bot (https://github.com/OfficeDev/microsoft-teams-sample-custombot). But I think it is much nicer to build your team bots in Azure functions than having to start a heavy WebAPI project, when most of the bots I can think of are simple helpers that e.g. kicks off a build or pulls in some information from another source. All tasks that Azure Functions are well suited to handle.

To make my life and yours easy, I have created simple ARM template that creates an Azure Function that is ready to be used as a custom bot for Microsoft Teams. The template contains no logic for your bot, but it secures the endpoint and gives you a framework you can build on.

If you want to use the template head over to the github, and click the deploy to azure button.

You get the following by using the template:

  • One HttpTrigger function in run.csx, MsTeamWebhook, this is the entry point called when your bot is invoked from teams.
  • The authtentication logic is found in auth.csx you have to edit this to add your own client secret. Not that for the bot endpoint to be used from different team channels, you should register your callback url with https://<your-azure-function>.azurewebsites.net/api/MsTeamWebhook?id=<channelId> were you use the channelId as the key and the client secret as the value in the dictionary in the code
public class AuthProvider
{
    /// <summary>
    /// A dictionary for storing signing keys. Here, the look up key is based on the value of the query parameter 'id'.
    /// The signing keys must be valid 256 bit base64 encoded strings that are provided during custom bot registration in MS Teams client.
    /// </summary>
    private static readonly Dictionary<string, string> SigningKeyDictionary = new Dictionary<string, string>()
            {
                {"your-channelId", "72CaN0Oewto5U6/BfjCHIpmAt0BjigreL1/EnO3mB6U=" },
                {"fabrikam", "QgyNSToQjf4p6+YzDpjKks1/tXeJQ7FhVHqRwTnugVI=" }
            };

Hope this helps you kick start your Microsoft Teams custom bot development.

Categories: Software Windows Azure

Tagged as:

Simon J.K. Pedersen

2 replies

  1. The channelid is just something you decide, it is just so you can have the backend installed in different teams, since they will each generate their own secret. So the channelid is your identifier that links the backend to a specific team.

Leave a Reply

Your email address will not be published. Required fields are marked *