Noel
Noel

Reputation: 23

V4 Bot Framework CreateConversationAsync (ConversationReference obsolete)

The code below was working - when a new user is added to the Teams channel the bot sends a welcome message to the user personally and not to the whole channel. For some reason it is no longer working - I believe it has to do with CreateConversationAsync() method. The V4 docs state: "This method is now obsolete because the ConversationReference argument is now redundant. Use the overload without this argument." but I haven't been able to figure out how to properly update the code below to work.

CreateConversationAsync: (This method passes the conversation reference (now obsolete) to ContinueConversationAsync())

ConversationReference conversationReference = null;
return await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
  teamsChannelId,
  serviceUrl,
  credentials,
  conversationParameters,
  async (t1, c1) =>
  {
    conversationReference = t1.Activity.GetConversationReference();
    await Task.FromResult(false).ConfigureAwait(false);
  }, cancellationToken).ContinueWith(_ => { return conversationReference; }).ConfigureAwait(false);

ContinueConversationAsync:

 if (conversationReference != null)
 {
    await turnContext.Adapter.ContinueConversationAsync(
      BotAppId,
      conversationReference,
      async (t2, c2) =>
      {
        await t2.SendActivityAsync(MessageFactory.Text(messages[0]), cancellationToken: c2).ConfigureAwait(false);
      },cancellationToken).ConfigureAwait(false);
}

ConversationParameters for reference:

var conversationParameters = new ConversationParameters
{
  IsGroup = false,
  Bot = this.TeamsHelper.GetRecipient(turnContext),
  Members = new List<ChannelAccount>() { member },
  TenantId = this.TeamsHelper.GetChannelTennantId(channelData),
  TopicName = "Testing",
};

Any help would be greatly appreciated!

------ UPDATED WITH SNIPPET ------

            var teamsChannelId = turnContext.Activity.TeamsGetChannelId();
            var serviceUrl = turnContext.Activity.ServiceUrl;
            var credentials = new MicrosoftAppCredentials(BotAppId, BotAppPassword);
            var channelData = turnContext.Activity.GetChannelData<TeamsChannelData>();

            var conversationParameters = new ConversationParameters
            {
                IsGroup = false,
                Bot = turnContext.Activity.Recipient,
                Members = new List<ChannelAccount>() { member },
                //TenantId = turnContext.Activity.Conversation.TenantId,
                TenantId = channelData.Tenant.Id,
                TopicName = "Testing Topic ",
            };

            var conversationReference = new ConversationReference()
            {
                ChannelId = teamsChannelId,
                Bot = turnContext.Activity.Recipient,
                ServiceUrl = serviceUrl,
                Conversation = new ConversationAccount() { ConversationType = "channel", IsGroup = false, Id = teamsChannelId, TenantId = channelData.Tenant.Id },
            };

            await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
                           teamsChannelId,
                           serviceUrl,
                           credentials,
                           conversationParameters,
                           async (t1, c1) =>
                           {
                               await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
                                   BotAppId,
                                   conversationReference,
                                   async (t2, c2) =>
                                   {
                                       await t2.SendActivityAsync(MessageFactory.Text("This will be the first response to the new thread"), c2).ConfigureAwait(false);
                                   },
                                   cancellationToken).ConfigureAwait(false);
                           },
                           cancellationToken).ConfigureAwait(false);

Upvotes: 1

Views: 1683

Answers (1)

Noel
Noel

Reputation: 23

Wanted to update everyone with a suggestion I was given:

If you want to send a message to user personally, you will need to pass conversation Id of that 1:1 chat not the channel, so to get that, conversation reference should be like this (please see variable conversationReference inside CreateConversationAsync) :

await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
    "msteams",
    serviceUrl,
    credentials,
    conversationParameters,
    async (t1, c1) =>
    {
        var userConversation = t1.Activity.Conversation.Id;
        var conversationReference = new ConversationReference
        {
            ServiceUrl = serviceUrl,
            Conversation = new ConversationAccount
            {
                Id = userConversation,
            },
        };
        await ((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
            BotAppId,
            conversationReference,
            async (t2, c2) =>
            {
                await t2.SendActivityAsync(MessageFactory.Text("This will be the first response to the new thread"), c2).ConfigureAwait(false);
            },
            cancellationToken).ConfigureAwait(false);
    },
    cancellationToken).ConfigureAwait(false);

I was able to test this approach locally, and it worked!

Upvotes: 1

Related Questions