首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >V4 Bot框架CreateConversationAsync (ConversationReference过时)

V4 Bot框架CreateConversationAsync (ConversationReference过时)
EN

Stack Overflow用户
提问于 2021-08-06 07:50:59
回答 1查看 634关注 0票数 1

下面的代码起作用了--当一个新用户被添加到Teams时,bot会亲自向用户发送欢迎信息,而不是向整个通道发送欢迎消息。由于某些原因,它不再起作用了--我相信它与CreateConversationAsync()方法有关。V4文档声明:“这个方法现在已经过时了,因为ConversationReference参数现在是多余的。请使用不带此参数的重载。”但是,我还没有弄清楚如何正确地更新下面的代码才能工作。

CreateConversationAsync: (此方法将会话引用(现已过时)传递给ContinueConversationAsync())

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
 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供参考:

代码语言:javascript
运行
复制
var conversationParameters = new ConversationParameters
{
  IsGroup = false,
  Bot = this.TeamsHelper.GetRecipient(turnContext),
  Members = new List<ChannelAccount>() { member },
  TenantId = this.TeamsHelper.GetChannelTennantId(channelData),
  TopicName = "Testing",
};

任何帮助都将不胜感激!

代码语言:javascript
运行
复制
            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);
EN

回答 1

Stack Overflow用户

发布于 2021-09-28 06:37:16

我想向大家提供一个建议:

如果要向用户亲自发送消息,则需要传递1:1 chat的会话Id,而不是通道,因此,会话引用应该如下所示(请参阅CreateConversationAsync中的变量conversationReference ):

代码语言:javascript
运行
复制
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);

我能够在本地测试这种方法,而且成功了!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68678026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档