V4 Bot框架CreateConversationAsync (ConversationReference过时).&l



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

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供参考:

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

任何帮助都将非常感激!

------更新了代码片段------

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

想把我得到的建议告诉大家:

如果你想亲自发送消息给用户,你需要传递会话Id的1:1聊天而不是通道,所以要得到,会话引用应该是这样的(请参阅变量conversationReference在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);

我能够在本地测试这种方法,并且它有效!

最新更新