问题
I'm developing a bot using the Bot Framework and Azure for Microsoft Teams and I'll to develop and test my code directly in MS Teams using ngrok. In the settings of the bot I've set the message endpoint to the URL I get from ngrok and added /api/messages
.
But every time I want to send a message from Teams, I get a 401 unauthorized response.
How could I authorize ngrok to send messages?
Update: Form an answer of @Hilton Giesenow I've checked the appSettings.json
file and everything is correct there. He said also that there is a small change that the problem is in the HttpAdapter
. So here is my code:
public class AdapterWithErrorHandler : BotFrameworkHttpAdapter
{
public AdapterWithErrorHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger) : base(configuration, logger)
{
OnTurnError = OnTurnErrorHandler;
}
private async Task OnTurnErrorHandler(ITurnContext turnContext, Exception exception)
{
string replyText = "An error occured while sending a message. If this error stays, please contact the service desk.";
await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText));
}
}
Code in startup file in the ConfigureServices
method:
services.AddSingleton<IBotFrameworkHttpAdapter, AdapterWithErrorHandler>();
services.AddTransient<ILogger<BotFrameworkHttpAdapter>, BotFrameworkHttpAdapterLogger>();
回答1:
So you've got the right first steps, if it's getting that far. Just an unrelated tip, you can even see the traffic by viewing http://localhost:4040.
Aside from that, your actual error is most likely caused by the incorrect or missing bot App Id and App Password in your bot's configuration (e.g. your appSettings.json for a C# project, or equivalent for a node project). Here's an example for a C# project.
You get that info inside the Azure portal. The easiest way is to go your bot itself, go to the "Settings" section on the left menu, and then selecting the "Manage" link above the "Microsoft App Id" section (the App Id that appears is the one you need, the "manage" link is where you go to get the apppassword). See a screenshot of the Settings screen here.
If it's still not working, then it's likely the configuration settings themselves. Essentially, under the covers and by default, the HttpAdapter will try to instantiate a ConfigurationCredentialProvider, which is looking very specifically for the configuration items named "MicrosoftAppId" and "MicrosoftAppPassword" (see here for more).
So, you'd need to ensure that your setting names match that correctly in your config file. If you're still having issues, then you can at least verify it's a configuration issue by replacing the line to instantiate the base, in your "AdapterWithErrorHandler" file. Modify it to:
: base(new Bot.Connector.Authentication.SimpleCredentialProvider("[your app id]", "[your add password"), logger: logger)
来源:https://stackoverflow.com/questions/61519187/test-a-bot-functionality-in-teams-using-ngrok