How to create a Microsoft Teams bot which is notifications-only in private scope but responds to mentions in a team/chat scope?

Dejan Franković 0 Reputation points
2024-07-31T11:46:46.38+00:00

I have a bot which sends only notifications to the personal scope of a user, however it can also provide some on-demand information when in team and chat scope, therefore needs to respond to @bot commands.
However, setting "isNotificationOnly" affects all scopes which is not desired.

Also, having multiple bots in a manifest where one would be for personal scope and only for notifications and the other for teams and chat scope with mentions is also not supported.

So, what's the best practice way to achieve this?

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
10,326 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,346 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sayali-MSFT 2,746 Reputation points Microsoft Vendor
    2024-08-06T09:54:01.8866667+00:00

    To create a Microsoft Teams bot that behaves differently in personal and team/chat scopes, you can follow these steps. The goal is to have the bot act as a notification-only bot in personal chats while responding to mentions in team or chat scopes. Since isNotificationOnly applies to all scopes, you need to handle this programmatically.

    Here's a practical approach:

    1. Set Up Your Bot

    Create the Bot: Start by creating a bot using the Microsoft Bot Framework. This bot will be registered with Azure Bot Service.

    Define Scopes: In your bot's manifest file, you'll configure the scopes and capabilities. Although you cannot specify different scopes for different behaviors directly in the manifest, you'll handle the differences programmatically.

    1. Implement Bot Logic

    You’ll need to handle the different behaviors based on the scope of the conversation. Here’s how you can do this:

    Determine the Scope: When your bot receives a message, you need to determine if it's coming from a personal chat, a team, or a group chat. You can do this by checking the conversation object in the Activity payload.

    Respond Based on Scope:

    • Personal Scope: If the bot is in a personal chat (1:1 conversation), you should implement logic to handle notifications only. This might involve sending periodic updates or notifications without responding to commands.
      • Team/Group Scope: If the bot is mentioned in a team or group chat, respond to @bot mentions or specific commands.

    Here's a code snippet in Node.js using the Bot Framework SDK to illustrate how you can differentiate between scope.

    const { ActivityTypes } = require('botbuilder');
    
    async function onMessageActivity(context) {
        // Check if the activity is a message
        if (context.activity.type === ActivityTypes.Message) {
            // Determine the conversation type
            const isPersonal = context.activity.conversation.isGroup === false;
    
            if (isPersonal) {
                // Handle personal scope notifications
                await handlePersonalNotification(context);
            } else {
                // Handle team or group chat commands
                await handleTeamCommand(context);
            }
        }
    }
    
    async function handlePersonalNotification(context) {
        // Implement your notification logic here
        await context.sendActivity("This is a notification-only response.");
    }
    
    async function handleTeamCommand(context) {
        // Check for bot mentions or commands in team/group chat
        if (context.activity.text.includes('@your-bot-name')) {
            await context.sendActivity("You mentioned me in a team or group chat!");
        } else {
            await context.sendActivity("I'm here to help with commands in teams!");
        }
    }
    
    
    1. Deploy and Test

    Deploy the Bot: Deploy your bot to Azure or any other hosting service you prefer.

    Test: Add the bot to a personal chat, a team, and a group chat to verify that it behaves as expected:

    • In personal chats, it should send notifications but not respond to commands.
      • In teams and group chats, it should respond to @bot mentions or specific commands.
    1. Handle Additional Scenarios
    • Message Filters: You may want to add additional logic to filter out specific messages or handle edge cases based on your bot's functionality.

    By structuring your bot this way, you ensure that it follows the desired behavior depending on the context of the conversation while adhering to the limitations of the Microsoft Teams platform.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.