Bot Framework bot 无法在没有提示的情况下在 Microsoft Teams 频道中 post
Bot Framework bot can't post in Microsoft Teams channel without being prompted
我有一个使用 Microsoft Bot Framework 构建的机器人,发布在 Azure App Service 中,我想 post 在 Microsoft Teams 频道中响应 POST 调用。我在继承自 ApiController 的 WebhookAPIController class 中有以下内容:
[HttpPost]
[Route("api/WebhookAPI")]
public async Task<HttpResponseMessage> RespondToWebhook([FromBody] PostTestModel value)
{
try
{
await CreateChannelConversation(value.text);
var resp = new HttpResponseMessage(HttpStatusCode.OK);
return resp;
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
private async Task CreateChannelConversation(string value)
{
var connector = new ConnectorClient(new Uri(System.Configuration.ConfigurationManager.AppSettings["serviceUrl"]));
var channelData = new Dictionary<string, string>();
channelData["teamsChannelId"] = System.Configuration.ConfigurationManager.AppSettings["ChannelId"];
IMessageActivity newMessage = Activity.CreateMessageActivity();
newMessage.Type = ActivityTypes.Message;
newMessage.Text = "Hello channel.";
ConversationParameters conversationParams = new ConversationParameters(
isGroup: true,
bot: null,
members: null,
topicName: "Test Conversation",
activity: (Activity)newMessage,
channelData: channelData);
var result = await connector.Conversations.CreateConversationAsync(conversationParams);
}
此代码从 AppSettings 获取服务 URL 和频道 ID;据我所知,给定 Teams 频道的频道 ID 永远不会改变。但是,当我对 (bot URL)/api/WebhookAPI 进行 POST 调用时,没有消息被 posted 并且我收到此错误消息作为响应:
{
"message": "An error has occurred.",
"exceptionMessage": "Authorization for Microsoft App ID [ID omitted] failed with status code Unauthorized and reason phrase 'Unauthorized'",
"exceptionType": "System.UnauthorizedAccessException",
"stackTrace": " at Microsoft.Bot.Connector.JwtTokenRefresher.<SendAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)\r\n at Microsoft.Bot.Connector.Conversations.<CreateConversationWithHttpMessagesAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Connector.ConversationsExtensions.<CreateConversationAsync>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at WebhookTestBot.Controllers.WebhookAPIController.<CreateChannelConversation>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at WebhookTestBot.Controllers.WebhookAPIController.<RespondToWebhook>d__1.MoveNext()",
"innerException": {
"message": "An error has occurred.",
"exceptionMessage": "Response status code does not indicate success: 401 (Unauthorized).",
"exceptionType": "System.Net.Http.HttpRequestException",
"stackTrace": " at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()\r\n at Microsoft.Bot.Connector.JwtTokenRefresher.<SendAsync>d__2.MoveNext()"
}
但是,如果我先在频道中 @-mention 机器人,POST 会调用 returns 200 并且机器人会在频道中 posts。有没有一种方法可以将我的机器人配置为能够 post 在频道中而不被提及?
尝试向 ConnectorClient 构造函数添加凭据:
string appId = ConfigurationManager.AppSettings["MicrosoftAppId"];
string appPassword = ConfigurationManager.AppSettings["MicrosoftAppPassword"];
var appCredentials = new MicrosoftAppCredentials(appId, appPassword);
var connector = new ConnectorClient(new Uri(ConfigurationManager.AppSettings["serviceUrl"]), appCredentials );
感谢 ,让它正常工作。在调用 CreateConversationAsync() 之前将以下代码添加到 CreateChannelConversation():
MicrosoftAppCredentials.TrustServiceUrl(ConfigurationManager.AppSettings["serviceUrl"], DateTime.MaxValue);
我有一个使用 Microsoft Bot Framework 构建的机器人,发布在 Azure App Service 中,我想 post 在 Microsoft Teams 频道中响应 POST 调用。我在继承自 ApiController 的 WebhookAPIController class 中有以下内容:
[HttpPost]
[Route("api/WebhookAPI")]
public async Task<HttpResponseMessage> RespondToWebhook([FromBody] PostTestModel value)
{
try
{
await CreateChannelConversation(value.text);
var resp = new HttpResponseMessage(HttpStatusCode.OK);
return resp;
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
private async Task CreateChannelConversation(string value)
{
var connector = new ConnectorClient(new Uri(System.Configuration.ConfigurationManager.AppSettings["serviceUrl"]));
var channelData = new Dictionary<string, string>();
channelData["teamsChannelId"] = System.Configuration.ConfigurationManager.AppSettings["ChannelId"];
IMessageActivity newMessage = Activity.CreateMessageActivity();
newMessage.Type = ActivityTypes.Message;
newMessage.Text = "Hello channel.";
ConversationParameters conversationParams = new ConversationParameters(
isGroup: true,
bot: null,
members: null,
topicName: "Test Conversation",
activity: (Activity)newMessage,
channelData: channelData);
var result = await connector.Conversations.CreateConversationAsync(conversationParams);
}
此代码从 AppSettings 获取服务 URL 和频道 ID;据我所知,给定 Teams 频道的频道 ID 永远不会改变。但是,当我对 (bot URL)/api/WebhookAPI 进行 POST 调用时,没有消息被 posted 并且我收到此错误消息作为响应:
{
"message": "An error has occurred.",
"exceptionMessage": "Authorization for Microsoft App ID [ID omitted] failed with status code Unauthorized and reason phrase 'Unauthorized'",
"exceptionType": "System.UnauthorizedAccessException",
"stackTrace": " at Microsoft.Bot.Connector.JwtTokenRefresher.<SendAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)\r\n at Microsoft.Bot.Connector.Conversations.<CreateConversationWithHttpMessagesAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Bot.Connector.ConversationsExtensions.<CreateConversationAsync>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at WebhookTestBot.Controllers.WebhookAPIController.<CreateChannelConversation>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at WebhookTestBot.Controllers.WebhookAPIController.<RespondToWebhook>d__1.MoveNext()",
"innerException": {
"message": "An error has occurred.",
"exceptionMessage": "Response status code does not indicate success: 401 (Unauthorized).",
"exceptionType": "System.Net.Http.HttpRequestException",
"stackTrace": " at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()\r\n at Microsoft.Bot.Connector.JwtTokenRefresher.<SendAsync>d__2.MoveNext()"
}
但是,如果我先在频道中 @-mention 机器人,POST 会调用 returns 200 并且机器人会在频道中 posts。有没有一种方法可以将我的机器人配置为能够 post 在频道中而不被提及?
尝试向 ConnectorClient 构造函数添加凭据:
string appId = ConfigurationManager.AppSettings["MicrosoftAppId"];
string appPassword = ConfigurationManager.AppSettings["MicrosoftAppPassword"];
var appCredentials = new MicrosoftAppCredentials(appId, appPassword);
var connector = new ConnectorClient(new Uri(ConfigurationManager.AppSettings["serviceUrl"]), appCredentials );
感谢
MicrosoftAppCredentials.TrustServiceUrl(ConfigurationManager.AppSettings["serviceUrl"], DateTime.MaxValue);