Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
After proactively installing your bot for a user using Microsoft Graph, you can send them a 1:1 personal welcome message. This involves retrieving the conversation chatId for the user and then sending the message using the Teams SDK.
Retrieve the conversation chatId
When your app is installed for the user, the bot receives an install activity. Use the OnInstall handler (C#) or install.add event (Node.js/Python) in the Teams SDK to capture the conversationId needed to send the proactive message.
Microsoft Graph page reference: Get chat
You must have your app's
{teamsAppInstallationId}. If you don't have it, use the following:HTTP GET request:
GET https://graph.microsoft.com/v1.0/users/{user-id}/teamwork/installedApps?$expand=teamsApp&$filter=teamsApp/id eq '{teamsAppId}'The id property of the response is the
teamsAppInstallationId.Make the following request to fetch the
chatId:HTTP GET request (permission—
TeamsAppInstallation.ReadWriteSelfForUser.All):GET https://graph.microsoft.com/v1.0/users/{user-id}/teamwork/installedApps/{teamsAppInstallationId}/chatThe id property of the response is the
chatId.You can also retrieve the
chatIdwith the following request but it requires the broaderChat.Read.Allpermission:HTTP GET request (permission—
Chat.Read.All):GET https://graph.microsoft.com/v1.0/users/{user-id}/chats?$filter=installedApps/any(a:a/teamsApp/id eq '{teamsAppId}')
Send proactive messages
Your bot can send proactive messages after the bot has been added for a user or a team, and has received all the user information.
Code snippets
The following code provides an example of sending proactive messages:
using Microsoft.Teams.Api;
using Microsoft.Teams.Apps;
// ...
// Store conversation IDs (e.g., during install event)
var conversationStorage = new Dictionary<string, string>();
app.OnInstall(async context =>
{
var userId = context.Activity.From.AadObjectId;
var conversationId = context.Activity.Conversation.Id;
conversationStorage[userId] = conversationId;
await context.Send("Hi! I will send you proactive notifications.");
});
// Send proactive message from anywhere
public static async Task SendProactiveNotification(string userId)
{
var conversationId = conversationStorage.GetValueOrDefault(userId);
if (conversationId is null) return;
await app.Send(conversationId, "Proactive hello.");
}
See also
Platform Docs