Send direct message to a user via MS Teams programmatically

nettech 146 Reputation points
2023-10-22T18:07:53.19+00:00

Hi,

We are currently using teams web hooks extensively to send notifications to teams channels.

Sometimes we have situations where we need to send a teams message to a user directly, but have not found a way to accomplish this task using powershell.

Is it possible to make an Invoke-RestMethod PowerShell call to send a message to a user directly?

Thank you

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,824 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.
2,916 questions
0 comments No comments
{count} votes

Accepted answer
  1. Prasad-MSFT 5,706 Reputation points Microsoft Vendor
    2023-10-23T05:58:16.5733333+00:00

    It is not possible to send a direct message to a user using Teams Incoming Webhook. The Incoming Webhook is designed to post messages to a specific channel, not to individual users. However, you can achieve this by creating a Teams bot application. Teams bot applications can send messages to individual users, group chats, and public channels.

    To send a message to a user directly, you would need to use the Microsoft Bot Framework and the Teams SDK. You would need to capture the user's Teams-specific identifiers, which you can do by fetching the roster or user profile data, subscribing to conversation events, or using Microsoft Graph.

    var turnContext = new TurnContext(new AdapterWithErrorHandler(new ConfigurationCredentialProvider(Configuration)), activity);
    var conversationParameters = new ConversationParameters
    {
        IsGroup = false,
        Bot = turnContext.Activity.Recipient,
        Members = new ChannelAccount[] { new ChannelAccount(userId) },
        TenantId = tenantId
    };
    
    await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
        channelId,
        serviceUrl,
        credentials,
        conversationParameters,
        async (t1, c1) =>
        {
            var reply = MessageFactory.Text("Hello, user!");
            await t1.SendActivityAsync(reply, c1);
        },
        cancellationToken);
    
    

    Please refer to the Microsoft Bot Framework documentation and Teams SDK documentation for more detailed information on how to create a bot and send messages to users.

    Thanks, 

    Prasad Das

    ************************************************************************* 

    If the response is helpful, please click "Accept Answer" and upvote it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Prashant Bali (Upwork Global Inc) 75 Reputation points Microsoft Vendor
    2023-12-02T09:33:03.14+00:00

    It is now possible to send messages to personal chat(1:1) or to group chat in Microsoft Teams using Microsoft Graph API. And yes, the messages will be displayed in the Teams application using Microsoft Graph API.

    Please refer the "1:1 and group chat messages" section from the below microsoft documentation link:

    chatMessage resource type

    Also, below is the graph API to send a message to any conversation you want using Post HTTP method :

    https://graph.microsoft.com/beta/users/{user-id}/chats/{chat-id}/messages

    To fetch {user-id} and {chat-id}, please follow the below steps using Get HTTP method:

    Fetch the user id of a logged-in user or user id of other user using below graph API's:

    https://graph.microsoft.com/v1.0/me
    https://graph.microsoft.com/v1.0/users

    Fetch the conversation/chat id of a user:

    https://graph.microsoft.com/beta/me/chats
    https://graph.microsoft.com/beta/users/{id}/chats

    As of now, there is no graph API to reply to a personal chat but we can reply to any team's channel message using Microsoft Graph API.

    If the response is helpful, please click "Accept Answer" and upvote it.