'Send the message to personal chat instead of group chat using ms teams chatbot

I would like to post a message to personal chat instead of group chat while message post from group chat how to achieve this ?. Is there is any methods available related to this issue ?.



Solution 1:[1]

As the others have noted in this thread, you need to use something called "Proactive Messaging". I see you're using node.js though, so here is a better sample than the C# or Java people have posted already: https://github.com/pnp/teams-dev-samples/tree/main/samples/bot-proactive-messaging . I put both a dotnet as well as a node.js version in the sample, and there are some links at the bottom of the page to read more about the topic. Here is also a link to a video session where I talk more about the concept: https://www.youtube.com/watch?v=mM7-fYdcJhw&t=1398s

It is important to know that proactive messaging will only work if you have a "context" with the user already, which basically means they have to have installed your app already. It is possible to pre-install it on their behalf though. You need to use Graph to do this, and you can read more about it here: https://docs.microsoft.com/en-us/graph/api/userteamwork-post-installedapps?view=graph-rest-1.0&tabs=http .

Solution 2:[2]

You can post a proactive personal message to a user: Please find below sample code:

Also, before running this code, make sure that the user has installed the bot app in the personal scope or is a member of a Team which has the bot installed.

Reference docs: https://docs.microsoft.com/en-us/java/api/com.microsoft.cognitiveservices.speech.transcription.conversation.createconversationasync?view=azure-java-stable

using Microsoft.Bot.Builder;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Teams.Bot.Conversations
{
    class Program
    {
        public static async Task Main(string[] args)
        {
            //Teams internal id
            string teamInternalId = "19:[email protected]";

            //The Bot Service Url needs to be dynamically stored and fetched from the Team. Recommendation is to store the serviceUrl from the bot Payload and later re-use it to send proactive messages.
            string serviceUrl = "https://smba.trafficmanager.net/emea/";

            //the upn of the user who should recieve the personal message
            string mentionUserPrincipalName = "[email protected]";

            //Office 365/Azure AD tenant id for the 
            string tenantId = "<tenant-GUID>";

            //From the Bot Channel Registration
            string botClientID = "<client-id>";
            string botClientSecret = "<client-secret>";

            var connectorClient = new ConnectorClient(new Uri(serviceUrl), new MicrosoftAppCredentials(botClientID, botClientSecret));

            var user = await ((Microsoft.Bot.Connector.Conversations)connectorClient.Conversations).GetConversationMemberAsync(mentionUserPrincipalName, teamInternalId, default);


            var personalMessageActivity = MessageFactory.Text($"Personal message from the Bot!");

            var conversationParameters = new ConversationParameters()
            {
                ChannelData = new TeamsChannelData
                {
                    Tenant = new TenantInfo
                    {
                        Id = tenantId,
                    }
                },
                Members = new List<ChannelAccount>() { user }
            };

            var response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);

            await connectorClient.Conversations.SendToConversationAsync(response.Id, personalMessageActivity);
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hilton Giesenow
Solution 2 Dharman