Understand core bot concepts for Microsoft Teams

Completed

Microsoft Teams provides a platform for developers to build and deploy bots that can interact with users in a natural, conversational way. In this unit you'll learn about core concepts related to building bots for Microsoft Teams.

Conversations

Conversations in the Bot Framework are the interactions between a user and a bot. Each conversation is identified by a unique ID, and can include multiple activities. Conversations can be one-on-one between a user and a bot, or they can involve multiple users. The messages your bot sends in conversations can be simple text messages, or they can include rich content like cards, images, or interactive elements.

Activities

Each message in a bot conversation is an Activity object of type messageType: message. When a user sends a message, Teams posts the message to your bot and the bot handles the message. Teams sends notifications to your bot for conversation events that happen in scopes where your bot is active.

You can capture these events in your code and take action on them. For example, you might:

  • Trigger a welcome message when your bot is added to a team.
  • Trigger a welcome message when a new team member is added or removed.
  • Trigger a notification when a channel is created, renamed, or deleted.
  • Trigger a notification when a bot message is liked by a user.
  • Identify the default channel for your bot from user input (selection) during installation.

Activity handlers

Activity handlers are the methods in your bot's code that handle these activities.

When a bot for Microsoft Teams receives an activity, it passes it onto its activity handlers. Under the covers, there's one base handler called the turn handler, that all activities are routed through. The turn handler calls the required activity handler to handle whatever type of activity was received.

Where a bot designed for Microsoft Teams differs is that its derived from TeamsActivityHandler class that is derived from the Bot Framework's ActivityHandler class. Your code needs to override methods in this class to implement your bot logic.

For example, in JavaScript, there are two primary Teams activity handlers in the TeamsActivityHandler class: dispatchConversationUpdateActivity and onInvokeActivity. dispatchConversationUpdateActivity routes all conversation update activities and onInvokeActivity routes all Teams invoke activities.

Review the list of general and Teams-specific activity handlers here. Here are a few examples of core Bot Framework handlers:

  • onMessage
  • onMessageUpdate
  • onMessageDelete

Some of the events unique to Microsoft Teams include:

  • Team Member Events
    • teamMemberAdded
    • teamMemberRemoved
  • Channel Events
    • channelCreated
    • channelRenamed
    • channelDeleted
  • Team Events
    • teamRenamed
  • Message Reactions
    • reactionsAdded
    • reactionsRemoved

Sample code overriding an event handler:

onTeamsChannelRenamed(async (channelInfo, teamInfo, context, next) => {
       // code for handling
       await next()
    });

Bot logic

The bot logic is the code that determines how your bot responds to activities. This could be as simple as sending a fixed response to a certain type of message, or it could involve complex logic that uses AI and machine learning to understand and respond to user input.

Bot scope

Based on the types of conversations you want to support, you need to define the scopes for your bot in your app manifest. There are three options for scope:

Scope Description
channel This conversation type is visible to all members of the channel.
personal This conversation type includes conversations between bots and a single user.
groupChat This conversation type includes chat between a bot and two or more users. It also enables your bot in meeting chats.

For example, the following sample code demonstrates a bot with 3 defined scopes:

"bots": [
        {
            "botId": "${{BOT_ID}}",
            "scopes": [
                "personal",
                "team",
                "groupchat"
            ],
            "supportsFiles": false,
            "isNotificationOnly": false
        }
    ],