Bot Framework bot无法在未提示的情况下在Microsoft Teams频道中发布

时间:2017-04-28 15:49:12

标签: c# azure botframework microsoft-teams

我使用在Microsoft App Service中发布的Microsoft Bot Framework构建了一个机器人,我想在Microsoft Teams频道中发布以响应POST调用。我在WebhookAPIController类中有以下内容,继承自ApiController:

[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;据我所知,对于给定的团队频道,频道ID永远不会改变。但是,当我对(bot URL)/ api / WebhookAPI进行POST调用时,没有消息被发布,我收到此错误消息:

{
    "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()"
}

但是,如果我首先在频道中输入机器人,那么POST调用将返回200并且机器人会在频道中发布。有没有办法可以配置我的机器人能够在没有被提及的情况下在频道中发帖?

3 个答案:

答案 0 :(得分:3)

感谢this answer。在调用CreateConversationAsync()之前,将以下代码添加到CreateChannelConversation():

MicrosoftAppCredentials.TrustServiceUrl(ConfigurationManager.AppSettings["serviceUrl"], DateTime.MaxValue);

答案 1 :(得分:0)

所以你要对serviceUrl和ChannelID进行硬编码?当你提到它时,频道ID是否与机器人接收的内容相匹配?它看起来像这样:“19:693ecdb923ac4458a5c23661b505fc84@thread.skype”另请注意:the teamsChannelId is being deprecated in the future,所以您应该改为channelData.channel.id。我将在MSDN上更新示例代码。

无论如何,假设硬编码的值与您所在的频道匹配,您的代码应该正常工作,并在我的测试用例中工作。

你说你首先@mention the bot它可行。你能解释一下你的测试用例吗?

答案 2 :(得分:0)

尝试将凭据添加到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 );