简体   繁体   English

V4 Bot Framework CreateConversationAsync(ConversationReference 已过时)

[英]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.下面的代码有效 - 当新用户添加到 Teams 频道时,机器人会向用户个人而不是整个频道发送欢迎消息。 For some reason it is no longer working - I believe it has to do with CreateConversationAsync() method.由于某种原因,它不再起作用——我相信它与CreateConversationAsync()方法有关。 The V4 docs state: "This method is now obsolete because the ConversationReference argument is now redundant. Use the overload without this argument." V4 文档 state: “此方法现已过时,因为 ConversationReference 参数现在是多余的。使用没有此参数的重载。” 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()) CreateConversationAsync:(此方法将对话引用(现已过时)传递给 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: ConversationParameters供参考:

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);

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 ):如果您想亲自向用户发送消息,您需要传递该 1:1 聊天的对话 ID 而不是频道,因此,对话引用应该是这样的(请参阅CreateConversationAsync 中的变量 conversationReference ):

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!我能够在本地测试这种方法,它奏效了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM