forked from microsoft/CopilotStudioSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBotController.cs
More file actions
70 lines (63 loc) · 2.58 KB
/
Copy pathBotController.cs
File metadata and controls
70 lines (63 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Generated with Bot Builder V4 SDK Template for Visual Studio EmptyBot v4.16.0
using Microsoft.AspNetCore.Mvc;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Integration.AspNet.Core;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using TranslationBot.Translation.Helpers;
namespace TranslationBot.Controllers
{
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
// achieved by specifying a more specific type for the bot constructor argument.
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly IBot _bot;
private readonly ILogger<BotController> _logger;
private readonly UserLanguage _languages;
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot, UserLanguage languages, ILogger<BotController> logger)
{
_adapter = adapter;
_bot = bot;
_languages = languages ?? throw new ArgumentNullException(nameof(languages));
_logger = logger;
}
[HttpPost]
[HttpGet]
[Route("api/{route}/{language?}")]
public async Task PostAsync(string route, string language)
{
if (string.IsNullOrEmpty(route))
{
_logger.LogError($"PostAsync: No route provided.");
throw new ArgumentNullException(nameof(route));
}
if (!string.IsNullOrEmpty(language))
{
_languages.Save(language);
}
if (_adapter != null)
{
if (_logger.IsEnabled(LogLevel.Debug))
{
_logger.LogInformation($"PostAsync: routed '{route}' to {_adapter}");
}
// Delegate the processing of the HTTP POST to the appropriate adapter.
// The adapter will invoke the bot.
await _adapter.ProcessAsync(Request, Response, _bot).ConfigureAwait(false);
}
else
{
_logger.LogError($"PostAsync: No adapter registered and enabled for route {route}.");
throw new KeyNotFoundException($"No adapter registered and enabled for route {route}.");
}
}
}
}