Skip to content

Commit 0137402

Browse files
committed
Added multilingual bot sample
1 parent ede7974 commit 0137402

113 files changed

Lines changed: 3285 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
//
4+
// Generated with Bot Builder V4 SDK Template for Visual Studio CoreBot v4.16.0
5+
6+
using Microsoft.Bot.Builder;
7+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
8+
using Microsoft.Bot.Builder.TraceExtensions;
9+
using Microsoft.Bot.Connector.Authentication;
10+
using Microsoft.Extensions.Logging;
11+
using System;
12+
using TranslationBot.Translation;
13+
14+
namespace TranslationBot
15+
{
16+
public class AdapterWithErrorHandler : CloudAdapter
17+
{
18+
public AdapterWithErrorHandler(TranslationMiddleware translationMiddleware, BotFrameworkAuthentication auth, ILogger<IBotFrameworkHttpAdapter> logger, ConversationState conversationState = default)
19+
: base(auth, logger)
20+
{
21+
Use(translationMiddleware);
22+
23+
OnTurnError = async (turnContext, exception) =>
24+
{
25+
// Log any leaked exception from the application.
26+
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
27+
28+
// Send a message to the user
29+
await turnContext.SendActivityAsync("The bot encountered an error or bug.");
30+
await turnContext.SendActivityAsync("To continue to run this bot, please fix the bot source code.");
31+
32+
if (conversationState != null)
33+
{
34+
try
35+
{
36+
await conversationState.DeleteAsync(turnContext);
37+
}
38+
catch (Exception ex)
39+
{
40+
logger.LogError(exception, $"Exception caught on attempting to Delete ConversationState: {ex.Message}");
41+
}
42+
}
43+
44+
// Send a trace activity, which will be displayed in the Bot Framework Emulator
45+
await turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError");
46+
};
47+
}
48+
}
49+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
//
4+
// Generated with Bot Builder V4 SDK Template for Visual Studio EmptyBot v4.16.0
5+
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.Bot.Builder;
8+
using Microsoft.Bot.Builder.Integration.AspNet.Core;
9+
using System.Collections.Generic;
10+
using System;
11+
using System.Threading.Tasks;
12+
using Microsoft.Extensions.Logging;
13+
using TranslationBot.Translation.Helpers;
14+
15+
namespace TranslationBot.Controllers
16+
{
17+
// This ASP Controller is created to handle a request. Dependency Injection will provide the Adapter and IBot
18+
// implementation at runtime. Multiple different IBot implementations running at different endpoints can be
19+
// achieved by specifying a more specific type for the bot constructor argument.
20+
21+
[ApiController]
22+
public class BotController : ControllerBase
23+
{
24+
private readonly IBotFrameworkHttpAdapter _adapter;
25+
private readonly IBot _bot;
26+
private readonly ILogger<BotController> _logger;
27+
private readonly UserLanguage _languages;
28+
29+
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot, UserLanguage languages, ILogger<BotController> logger)
30+
{
31+
_adapter = adapter;
32+
_bot = bot;
33+
_languages = languages ?? throw new ArgumentNullException(nameof(languages));
34+
_logger = logger;
35+
}
36+
37+
[HttpPost]
38+
[HttpGet]
39+
[Route("api/{route}/{language?}")]
40+
public async Task PostAsync(string route, string language)
41+
{
42+
if (string.IsNullOrEmpty(route))
43+
{
44+
_logger.LogError($"PostAsync: No route provided.");
45+
throw new ArgumentNullException(nameof(route));
46+
}
47+
if (!string.IsNullOrEmpty(language))
48+
{
49+
_languages.Save(language);
50+
}
51+
52+
if (_adapter != null)
53+
{
54+
if (_logger.IsEnabled(LogLevel.Debug))
55+
{
56+
_logger.LogInformation($"PostAsync: routed '{route}' to {_adapter}");
57+
}
58+
59+
// Delegate the processing of the HTTP POST to the appropriate adapter.
60+
// The adapter will invoke the bot.
61+
await _adapter.ProcessAsync(Request, Response, _bot).ConfigureAwait(false);
62+
}
63+
else
64+
{
65+
_logger.LogError($"PostAsync: No adapter registered and enabled for route {route}.");
66+
throw new KeyNotFoundException($"No adapter registered and enabled for route {route}.");
67+
}
68+
}
69+
}
70+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"azureBotId": {
6+
"value": ""
7+
},
8+
"azureBotSku": {
9+
"value": "F0"
10+
},
11+
"azureBotRegion": {
12+
"value": "global"
13+
},
14+
"botEndpoint": {
15+
"value": ""
16+
},
17+
"appType": {
18+
"value": "MultiTenant"
19+
},
20+
"appId": {
21+
"value": ""
22+
},
23+
"UMSIName": {
24+
"value": ""
25+
},
26+
"UMSIResourceGroupName": {
27+
"value": ""
28+
},
29+
"tenantId": {
30+
"value": ""
31+
}
32+
}
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"appServiceName": {
6+
"value": ""
7+
},
8+
"existingAppServicePlanName": {
9+
"value": ""
10+
},
11+
"existingAppServicePlanLocation": {
12+
"value": ""
13+
},
14+
"newAppServicePlanName": {
15+
"value": ""
16+
},
17+
"newAppServicePlanLocation": {
18+
"value": ""
19+
},
20+
"newAppServicePlanSku": {
21+
"value": {
22+
"name": "F0",
23+
"tier": "Free",
24+
"size": "F0",
25+
"family": "F",
26+
"capacity": 1
27+
}
28+
},
29+
"appType": {
30+
"value": "MultiTenant"
31+
},
32+
"appId": {
33+
"value": ""
34+
},
35+
"appSecret": {
36+
"value": ""
37+
},
38+
"UMSIName": {
39+
"value": ""
40+
},
41+
"UMSIResourceGroupName": {
42+
"value": ""
43+
},
44+
"tenantId": {
45+
"value": ""
46+
}
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Usage
2+
The BotApp must be deployed prior to AzureBot.
3+
4+
Command line:
5+
- az login
6+
- az deployment group create --resource-group <group-name> --template-file <template-file> --parameters @<parameters-file>
7+
8+
# parameters-for-template-BotApp-with-rg:
9+
10+
- **appServiceName**:(required) The Name of the Bot App Service.
11+
12+
- (choose an existingAppServicePlan or create a new AppServicePlan)
13+
- **existingAppServicePlanName**: The name of the App Service Plan.
14+
- **existingAppServicePlanLocation**: The location of the App Service Plan.
15+
- **newAppServicePlanName**: The name of the App Service Plan.
16+
- **newAppServicePlanLocation**: The location of the App Service Plan.
17+
- **newAppServicePlanSku**: The SKU of the App Service Plan. Defaults to Standard values.
18+
19+
- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.**
20+
21+
- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings.
22+
23+
- **appSecret**:(required for MultiTenant and SingleTenant) Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings.
24+
25+
- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication.
26+
27+
- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication.
28+
29+
- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to <Subscription Tenant ID>.
30+
31+
MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource
32+
33+
34+
35+
# parameters-for-template-AzureBot-with-rg:
36+
37+
- **azureBotId**:(required) The globally unique and immutable bot ID.
38+
- **azureBotSku**: The pricing tier of the Bot Service Registration. **Allowed values are: F0, S1(default)**.
39+
- **azureBotRegion**: Specifies the location of the new AzureBot. **Allowed values are: global(default), westeurope**.
40+
- **botEndpoint**: Use to handle client messages, Such as https://<botappServiceName>.azurewebsites.net/api/messages.
41+
42+
- **appType**: Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. **Allowed values are: MultiTenant(default), SingleTenant, UserAssignedMSI.**
43+
- **appId**:(required) Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings.
44+
- **UMSIName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource used for the Bot's Authentication.
45+
- **UMSIResourceGroupName**:(required for UserAssignedMSI) The User-Assigned Managed Identity Resource Group used for the Bot's Authentication.
46+
- **tenantId**: The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to <Subscription Tenant ID>.
47+
48+
MoreInfo: https://docs.microsoft.com/en-us/azure/bot-service/tutorial-provision-a-bot?view=azure-bot-service-4.0&tabs=userassigned%2Cnewgroup#create-an-identity-resource
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"azureBotId": {
6+
"type": "string",
7+
"metadata": {
8+
"description": "The globally unique and immutable bot ID."
9+
}
10+
},
11+
"azureBotSku": {
12+
"defaultValue": "S1",
13+
"type": "string",
14+
"metadata": {
15+
"description": "The pricing tier of the Bot Service Registration. Allowed values are: F0, S1(default)."
16+
}
17+
},
18+
"azureBotRegion": {
19+
"type": "string",
20+
"defaultValue": "global",
21+
"metadata": {
22+
"description": "Specifies the location of the new AzureBot. Allowed values are: global(default), westeurope."
23+
}
24+
},
25+
"botEndpoint": {
26+
"type": "string",
27+
"metadata": {
28+
"description": "Use to handle client messages, Such as https://<botappServiceName>.azurewebsites.net/api/messages."
29+
}
30+
},
31+
"appType": {
32+
"type": "string",
33+
"defaultValue": "MultiTenant",
34+
"allowedValues": [
35+
"MultiTenant",
36+
"SingleTenant",
37+
"UserAssignedMSI"
38+
],
39+
"metadata": {
40+
"description": "Type of Bot Authentication. set as MicrosoftAppType in the Web App's Application Settings. Allowed values are: MultiTenant, SingleTenant, UserAssignedMSI. Defaults to \"MultiTenant\"."
41+
}
42+
},
43+
"appId": {
44+
"type": "string",
45+
"metadata": {
46+
"description": "Active Directory App ID or User-Assigned Managed Identity Client ID, set as MicrosoftAppId in the Web App's Application Settings."
47+
}
48+
},
49+
"UMSIName": {
50+
"type": "string",
51+
"defaultValue": "",
52+
"metadata": {
53+
"description": "The User-Assigned Managed Identity Resource used for the Bot's Authentication."
54+
}
55+
},
56+
"UMSIResourceGroupName": {
57+
"type": "string",
58+
"defaultValue": "",
59+
"metadata": {
60+
"description": "The User-Assigned Managed Identity Resource Group used for the Bot's Authentication."
61+
}
62+
},
63+
"tenantId": {
64+
"type": "string",
65+
"defaultValue": "[subscription().tenantId]",
66+
"metadata": {
67+
"description": "The Azure AD Tenant ID to use as part of the Bot's Authentication. Only used for SingleTenant and UserAssignedMSI app types. Defaults to \"Subscription Tenant ID\"."
68+
}
69+
}
70+
},
71+
"variables": {
72+
"tenantId": "[if(empty(parameters('tenantId')), subscription().tenantId, parameters('tenantId'))]",
73+
"msiResourceId": "[concat(subscription().id, '/resourceGroups/', parameters('UMSIResourceGroupName'), '/providers/', 'Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('UMSIName'))]",
74+
"appTypeDef": {
75+
"MultiTenant": {
76+
"tenantId": "",
77+
"msiResourceId": ""
78+
},
79+
"SingleTenant": {
80+
"tenantId": "[variables('tenantId')]",
81+
"msiResourceId": ""
82+
},
83+
"UserAssignedMSI": {
84+
"tenantId": "[variables('tenantId')]",
85+
"msiResourceId": "[variables('msiResourceId')]"
86+
}
87+
},
88+
"appType": {
89+
"tenantId": "[variables('appTypeDef')[parameters('appType')].tenantId]",
90+
"msiResourceId": "[variables('appTypeDef')[parameters('appType')].msiResourceId]"
91+
}
92+
},
93+
"resources": [
94+
{
95+
"apiVersion": "2021-05-01-preview",
96+
"type": "Microsoft.BotService/botServices",
97+
"name": "[parameters('azureBotId')]",
98+
"location": "[parameters('azureBotRegion')]",
99+
"kind": "azurebot",
100+
"sku": {
101+
"name": "[parameters('azureBotSku')]"
102+
},
103+
"properties": {
104+
"displayName": "[parameters('azureBotId')]",
105+
"iconUrl": "https://docs.botframework.com/static/devportal/client/images/bot-framework-default.png",
106+
"endpoint": "[parameters('botEndpoint')]",
107+
"msaAppId": "[parameters('appId')]",
108+
"msaAppTenantId": "[variables('appType').tenantId]",
109+
"msaAppMSIResourceId": "[variables('appType').msiResourceId]",
110+
"msaAppType": "[parameters('appType')]",
111+
"luisAppIds": [],
112+
"schemaTransformationVersion": "1.3",
113+
"isCmekEnabled": false,
114+
"isIsolated": false
115+
},
116+
"dependsOn": []
117+
}
118+
]
119+
}

0 commit comments

Comments
 (0)