Edit

Send a personal welcome message

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

  1. 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.

  2. 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}/chat
    

    The id property of the response is the chatId.

    You can also retrieve the chatId with the following request but it requires the broader Chat.Read.All permission:

    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