Create a bot using Teams Toolkit

Completed

The Teams Toolkit for Visual Studio Code extension provides several scenario-based app templates for building bots. In this unit, you will learn how to create a bot using Teams Toolkit.

Bot components

Your Teams bot consists of the following:

  • A publicly accessible web service hosted by you.
  • A Bot Framework registration for your web service.
  • Your Teams app package, which describes the bot capabilities and where the bot can be installed (chat, channel, meeting, personal app).

Create a bot using Teams Toolkit for Visual Studio Code

To create a bot using Teams Toolkit, select the Bot option when creating a new app using the Toolkit extension.

Screenshot of Teams Toolkit for Visual Studio Code options for creating a new app.

Next, select your desired bot capability.

Screenshot of the options for building bots using Teams Toolkit for Visual Studio Code.

The options map to the capabilities described in the previous unit:

  • AI Chat Bot: Build a bot using Teams AI Library.
  • Chat Notification Message: Build a notification bot.
  • Chat Command: Build a command bot.
  • Sequential Workflow in Chat: Build a workflow bot.

You'll then select your desired programming language and a directory to store your project files.

Understand the scaffolded project

Teams Toolkit scaffolds a bot project for you that you can customize. For example, when you create a Basic Bot, Teams Toolkit sets up a project with the following core files and folders:

Folder Contents
.vscode VSCode files for debugging
appPackage Templates for the Teams application manifest
env Environment files
infra Templates for provisioning Azure resources

The following files can be customized and demonstrate an example implementation to get you started.

File Contents
teamsBot.ts Handles business logics for the Basic Bot.
index.ts index.ts is used to setup and configure the Basic Bot.

The following are Teams Toolkit specific project files. You can access the guide on GitHub to understand how Teams Toolkit works.

File Contents
teamsapp.yml This is the main Teams Toolkit project file. The project file defines two primary things: Properties and configuration Stage definitions.
teamsapp.local.yml This overrides teamsapp.yml with actions that enable local execution and debugging.
teamsapp.testtool.yml This overrides teamsapp.yml with actions that enable local execution and debugging in Teams App Test Tool.

Customize the project

Implement your custom bot logic to customize the scaffolded bot. As described in a previous unit, this could include implementing event handlers, defining the desired scopes for your bot, and defining commands that your bot supports. The units that follow will describe additional ways to add functionality to your bot using adaptive cards and proactive messaging.

Process a message

To process a message that the bot receives, use the text property of the turn context object's Activity object in your bot's activity handler.

For example, the following code processes the message received from context.activity. It uses a static method provided with the Bot Framework, removeMentionText, to parse out the @Mention portion of the message text then echoes the message back to the user:

this.onMessage(async (context, next) => {
      console.log("Running with Message Activity.");
      const removedMentionText = TurnContext.removeRecipientMention(context.activity);
      const txt = removedMentionText.toLowerCase().replace(/\n|\r/g, "").trim();
      await context.sendActivity(`Echo: ${txt}`);
      // By calling next() you ensure that the next BotHandler is run.
      await next();
    });

Send a message

To send a text message, specify the string you want to send as the activity. In the bot's activity handlers, use the turn context object's SendActivity() method to send a single message response.

Example:

    this.onMembersAdded(async (context, next) => {
      const membersAdded = context.activity.membersAdded;
      for (let cnt = 0; cnt < membersAdded.length; cnt++) {
        if (membersAdded[cnt].id) {
          await context.sendActivity(
            `Hi there! I'm a Teams bot that will echo what you said to me.`
          );
          break;
        }
      }
      await next();
    });

Implement capabilities

The updates you need to make also depend on the bot capability you implemented:

  • For a workflow bot, define the Adaptive Card and the actions it can perform. You'll also need to handle the responses from the Adaptive Card in your bot's code.

  • For a notification bot, set up a way for your bot to receive updates from the external system, and then send these updates as messages to users in Teams.

  • For a command bot, define the commands that your bot can respond to, and then implement the logic for each command in your bot's code.