Send Proactive 1:1 message in Teams using Bot Framework V4

594 views Asked by At

I'm trying to send proactive 1:1 message from bot to teams using Teams SDK with the code below

MicrosoftAppCredentials.TrustServiceUrl(turnContext.Activity.ServiceUrl);
            var connectorClient = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), Configuration["MicrosoftAppId"], Configuration["MicrosoftAppPassword"]);

            var userId = _operation.MemberTeamsId;
            var tenantId = Configuration["TenantId"];
            var parameters = new ConversationParameters
            {
                Members = new[] { new ChannelAccount(userId) },
                ChannelData = new TeamsChannelData
                {
                    Tenant = new TenantInfo(tenantId),
                },
            };

            var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);
            var message = Microsoft.Bot.Schema.Activity.CreateMessageActivity();
            message.Text = _operation.Message;
            await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Microsoft.Bot.Schema.Activity)message);

I have the following issues with this,

  1. The bot cannot send a proactive message unless the user has a prior conversation after deployment

    Bot deployed

    Bill to Bot - Works

    Bot to Bill - Works

    Bot redeployed

    Bot to Bill - Not works because there are no members in the conversation now after redeploying

    Bill to Bot - Works

    Bot to Bill - Works now as Bill had a conversation after redeploying

  2. Bot sends the same message multiple times to users

    Bill to Bot - Works

    Bot to Bill - Works Proactively - Sends 1 defined message as it should

    Sim to Bot - Works

    Bot to Sim - Sends 2 same messages as there are two members in the conversation now

    Will to Bot - works

    Bot to Will - Sends 3 same messages as there are three members in the conversation now

Note: I am storing the Teams userid in DB and use them to send direct messages to users

Any help on how to correct this would be appreciated. Thanks.

1

There are 1 answers

0
Hilton Giesenow On

Based on the answers in the comments, I think I understand the scenario better, and it looks to me like the problem is that you're not setting a Conversation Id anyway - although you tell the Bot which user you want to interact with, it needs to know if you want to interact with them directly (i.e. 1-1), or as part of a group chat somewhere, or a Teams channel.

You need to set this by configuring the Conversation property on the Activity instance (in your case the "message" variable). See my post here for more detailed information: Programmatically sending a message to a bot in Microsoft Teams

Hope that helps - if not, let me know