如何将BotFrameworkAdapter转换为CloudAdapter?代码适配问题求助
切换CloudAdapter的CreateConversationAsync调用失败问题解决
原可运行代码(BotFrameworkAdapter版本)
await ((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync( teamsChannelId, serviceUrl, credentials, conversationParameters, (t, ct) => { conversationReference = t.Activity.GetConversationReference(); _BotProfile.QueueCardList.TryAdd(guid.ToString(), t.Activity.Id); _BotProfile.RequestList.TryAdd(guid.ToString(), conversationReference); return Task.CompletedTask; }, cancellationToken);
尝试的CloudAdapter版本代码(调用失败)
string audience = ""; await ((CloudAdapter)turnContext.Adapter).CreateConversationAsync( _appId, teamsChannelId, serviceUrl, audience, conversationParameters, (t, ct) => { conversationReference = t.Activity.GetConversationReference(); _BotProfile.QueueCardList.TryAdd(guid.ToString(), t.Activity.Id); _BotProfile.RequestList.TryAdd(guid.ToString(), conversationReference); return Task.CompletedTask; }, cancellationToken);
已尝试将audience设为https://api.botframework.com,仍无法成功调用。
问题分析与解决方案
1. 确认Audience的正确取值
Audience需根据部署的云环境设置:
- Azure公有云:
https://api.botframework.com - Azure政府云:
https://api.botframework.us - Azure中国区:
https://api.botframework.cn
2. 验证App ID与凭据配置
- 确保
_appId是机器人注册时的有效应用ID - 确认CloudAdapter实例已正确配置凭据(如通过
ConfigurationCredentialProvider加载应用密码/证书):var adapter = new CloudAdapter(new ConfigurationCredentialProvider(Configuration));
3. 检查ConversationParameters配置
需确保conversationParameters中的Bot属性正确设置为符合Teams要求的机器人身份信息:
conversationParameters.Bot = new ChannelAccount { Id = $"28:{_appId}", // Teams环境下机器人ID固定格式为"28:{appId}" Name = "你的机器人名称" };
4. 修正后的完整代码示例
// 根据云环境设置正确的audience string audience = "https://api.botframework.com"; // 确保conversationParameters的Bot属性配置正确 conversationParameters.Bot = new ChannelAccount { Id = $"28:{_appId}", Name = "Bot Name" }; await ((CloudAdapter)turnContext.Adapter).CreateConversationAsync( _appId, teamsChannelId, serviceUrl, audience, conversationParameters, (t, ct) => { conversationReference = t.Activity.GetConversationReference(); _BotProfile.QueueCardList.TryAdd(guid.ToString(), t.Activity.Id); _BotProfile.RequestList.TryAdd(guid.ToString(), conversationReference); return Task.CompletedTask; }, cancellationToken);
额外排查点
- 确认
teamsChannelId格式有效(示例格式:19:xxxxxx@thread.tacv2) - 查看服务端日志,定位是否存在身份验证失败、参数格式错误等具体报错信息
内容的提问来源于stack exchange,提问作者Rich H.
相关产品推荐
相关产品推荐

