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:
- 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.
- 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.
- Team/Group Scope: If the bot is mentioned in a team or group chat, respond to
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!");
}
}
- 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.
- In teams and group chats, it should respond to
- 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.