Sending a Welcome
Message in a Microsoft Teams Notification Bot Using Azure Function (HTTP
Trigger) with JavaScript
In this blog, we'll
discuss how to send a welcome message when a Microsoft Teams notification
bot, built with Azure Functions (HTTP trigger) using JavaScript, is
added to a chat or channel. By leveraging the conversationUpdate event and
integrating it into the Azure Functions workflow, you can automate the delivery
of a welcome message every time your bot is introduced to a new conversation.
Here’s how to
integrate this functionality into your Azure Function-based bot.
Overview
In this setup, you’re
likely using an Azure HTTP-triggered function to manage notifications,
and you want the bot to automatically send a welcome message when added to a
Microsoft Teams chat or channel.
How Does It Work?
- Bot Initialization: The bot is deployed using Azure
Functions, with a specific URL endpoint for triggering actions.
- Handling conversationUpdate Event: The bot receives an event when it is
added to a conversation. We can use this event to trigger a welcome
message.
- Sending Welcome Message: The bot replies with a message that
includes details such as the conversation type, ID, and the bot's HTTP
endpoint.
Steps to Send a Welcome Message Using an Azure HTTP Trigger
Set Up the Azure
Function
You need to have an
Teams Notification Bot project set up with an HTTP trigger to handle incoming requests.
Learn how to create your own Teams Notification Bot:
1. Locate the teamsBot.js file:
2. Update the teamsBot.js file with the following code:
module.exports = async function (context, req) {
context.log('HTTP trigger function processed a request.');
// Handle conversation update event
const activity = req.body; // Incoming activity from Teams (includes conversation update event)
if (activity.type === 'conversationUpdate') {
const membersAdded = activity.membersAdded;
for (let i = 0; i < membersAdded.length; i++) {
const member = membersAdded[i];
if (member.id === activity.recipient.id) {
// The bot was added to the conversation
const conversationType = activity.conversation.conversationType;
const conversationId = activity.conversation.id;
// Static bot URL (replace with your actual bot's URL)
const botHttpUrl = "https://notification***.azurewebsites.net/api/notification";
// Create the welcome message
const welcomeMessage = {
type: "message",
text: `Hello! I have been added to this ${conversationType}.\n\n` +
`Conversation ID: ${conversationId}\n\n` +
`Bot HTTP URL: ${botHttpUrl}\n\n` +
`Feel free to interact with me!`
};
// Send welcome message response
context.res = {
status: 200,
body: welcomeMessage
};
return;
}
}
}
// Default response if no conversation update event
context.res = {
status: 200,
body: "No members added."
};
};
Explanation of the
Code
- Azure Function HTTP Trigger: This function is triggered by an HTTP
request, likely coming from Microsoft Teams. The function listens to
incoming activities and processes them based on the type of event (e.g., conversationUpdate).
- Handling the conversationUpdate Event:
- The bot checks whether the incoming event
is a conversationUpdate.
- It then loops through the membersAdded
array to check if the bot was added to the conversation.
- If the bot is added, the function
constructs a welcome message containing the conversation type,
conversation ID, and a static HTTP URL.
- Welcome Message Construction:
- The welcome message includes the
following:
- Conversation Type: Whether it's a chat or a channel.
- Conversation ID: Useful for logging or reference.
- Bot HTTP URL: The static URL for the bot that users
can use to interact with it directly.
- Sending the Response: The Azure function sends a response
containing the welcome message as part of the HTTP response to Teams.
Deploying the
Function to Azure
Once the Azure
Function is written and tested locally, you can deploy it to Azure through Visual Studio Code.
Ensure that:
- The bot is registered with the Microsoft
Bot Framework.
- The bot is connected to the Microsoft Teams channel/Chat.
Benefits of This
Approach
- Serverless Architecture: Using Azure Functions, you can scale
your bot without worrying about infrastructure.
- Customizable: You can easily modify the welcome
message, add more logic, or integrate additional services.
- Real-time Notifications: The bot can send notifications in real
time using an HTTP trigger, making it responsive and interactive.
Final Thoughts
Sending a welcome
message when your bot is added to a Microsoft Teams chat or channel is a great
way to create a more personalized and interactive experience for your users. By
combining Azure Functions with the power of the Bot Framework and Microsoft Teams,
you can easily implement this functionality without managing a full server
infrastructure.
Comments
Post a Comment