[英]V4 Bot Framework CreateConversationAsync (ConversationReference obsolete)
下面的代碼有效 - 當新用戶添加到 Teams 頻道時,機器人會向用戶個人而不是整個頻道發送歡迎消息。 由於某種原因,它不再起作用——我相信它與CreateConversationAsync()方法有關。 V4 文檔 state: “此方法現已過時,因為 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);
繼續對話異步:
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);
想用我得到的建議更新每個人:
如果您想親自向用戶發送消息,您需要傳遞該 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);
我能夠在本地測試這種方法,它奏效了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.