Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit 3dec9a9

Browse files
Batta32fran893VictorGrycuk
authored
[Samples] Add 17.multilingual-bot sample (#898)
* Add deploymentTemplates folder * Add webapp folder * Add resources folder * Add translation folder * Add Startup and Bot files * Add welcomeCard * Add dependencies and documentation * Add test structure * Apply minor fixes due to parity * Apply internal feedback * Fix test errors during validation * Fix test errors * Fix error in Middleware * Apply feedback * Add missing this * Update sendWelcomeMessage method to send the first non-target member * Fix issues during testing of the sample * Fix translation error * Add component notation in MultiLingualBot * Add notation to ignore unknown properties * Fix error when trasnlationKey is empty Co-authored-by: Franco Alvarez <51216149+fran893@users.noreply.github.com> Co-authored-by: Victor Grycuk <victor.grycuk@southworks.com>
1 parent 1eca556 commit 3dec9a9

File tree

21 files changed

+1869
-0
lines changed

21 files changed

+1869
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Microsoft Corporation. All rights reserved.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Multilingual Bot
2+
3+
Bot Framework v4 multilingual bot sample
4+
5+
This sample will present the user with a set of cards to pick their choice of language. The user can either change language by invoking the option cards, or by entering the language code (_en_/_es_). The bot will then acknowledge the selection.
6+
7+
This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to translate incoming and outgoing text using a custom middleware and the [Microsoft Translator Text API](https://docs.microsoft.com/en-us/azure/cognitive-services/translator/).
8+
9+
## Concepts introduced in this sample
10+
11+
Translation Middleware: We create a translation middleware that can translate text from bot to user and from user to bot, allowing the creation of multi-lingual bots.
12+
13+
The middleware is driven by user state. This means that users can specify their language preference, and the middleware automatically will intercept messages back and forth and present them to the user in their preferred language.
14+
15+
Users can change their language preference anytime, and since this gets written to the user state, the middleware will read this state and instantly modify its behavior to honor the newly selected preferred language.
16+
17+
The [Microsoft Translator Text API](https://docs.microsoft.com/en-us/azure/cognitive-services/translator/), Microsoft Translator Text API is a cloud-based machine translation service. With this API you can translate text in near real-time from any app or service through a simple REST API call.
18+
The API uses the most modern neural machine translation technology, as well as offering statistical machine translation technology.
19+
20+
## Prerequisites
21+
22+
- Java 1.8+
23+
- Install [Maven](https://maven.apache.org/)
24+
- An account on [Azure](https://azure.microsoft.com) if you want to deploy to Azure.
25+
- [Microsoft Translator Text API key](https://docs.microsoft.com/en-us/azure/cognitive-services/translator/translator-text-how-to-signup)
26+
27+
To consume the Microsoft Translator Text API, first obtain a key following the instructions in the [Microsoft Translator Text API documentation](https://docs.microsoft.com/en-us/azure/cognitive-services/translator/translator-text-how-to-signup).
28+
29+
Paste the key in the `TranslatorKey` setting in the `application.properties` file.
30+
31+
## To try this sample
32+
33+
- From the root of this project folder:
34+
- Build the sample using `mvn package`
35+
- Run it by using `java -jar .\target\bot-multilingual-sample.jar`
36+
37+
## Testing the bot using Bot Framework Emulator
38+
39+
[Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.
40+
41+
- Install the latest Bot Framework Emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)
42+
43+
### Connect to the bot using Bot Framework Emulator
44+
45+
- Launch Bot Framework Emulator
46+
- File -> Open Bot
47+
- Enter a Bot URL of `http://localhost:3978/api/messages`
48+
49+
### Creating a custom middleware
50+
51+
Translation Middleware: We create a translation middleware than can translate text from bot to user and from user to bot, allowing the creation of multilingual bots.
52+
Users can specify their language preference, which is stored in the user state. The translation middleware translates to and from the user's preferred language.
53+
54+
### Microsoft Translator Text API
55+
56+
The [Microsoft Translator Text API](https://docs.microsoft.com/en-us/azure/cognitive-services/translator/), Microsoft Translator Text API is a cloud-based machine translation service. With this API you can translate text in near real-time from any app or service through a simple REST API call.
57+
The API uses the most modern neural machine translation technology, as well as offering statistical machine translation technology.
58+
59+
## Deploy this bot to Azure
60+
61+
To learn more about deploying a bot to Azure, see [Deploy your bot to Azure](https://aka.ms/azuredeployment) for a complete list of deployment instructions.
62+
63+
### Add `TranslatorKey` to Application Settings
64+
65+
If you used the `application.properties` file to store your `TranslatorKey` then you'll need to add this key and its value to the Application Settings for your deployed bot.
66+
67+
- Log into the [Azure portal](https://portal.azure.com)
68+
- In the left nav, click on `Bot Services`
69+
- Click the `<your_bot_name>` Name to display the bots Web App Settings
70+
- Click the `Application Settings`
71+
- Scroll to the `Application settings` section
72+
- Click `+ Add new setting`
73+
- Add the key `TranslatorKey` with a value of the Translator Text API `Authentication key` created from the steps above
74+
75+
## Further reading
76+
77+
- [Bot Framework Documentation](https://docs.botframework.com)
78+
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
79+
- [Bot State](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-storage-concept?view=azure-bot-service-4.0)
80+
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
81+
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
82+
- [Azure Bot Service Documentation](https://docs.microsoft.com/azure/bot-service/?view=azure-bot-service-4.0)
83+
- [Azure CLI](https://docs.microsoft.com/cli/azure/?view=azure-cli-latest)
84+
- [Azure Portal](https://portal.azure.com)
85+
- [Spring Boot](https://spring.io/projects/spring-boot)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"groupLocation": {
6+
"value": ""
7+
},
8+
"groupName": {
9+
"value": ""
10+
},
11+
"appId": {
12+
"value": ""
13+
},
14+
"appSecret": {
15+
"value": ""
16+
},
17+
"botId": {
18+
"value": ""
19+
},
20+
"botSku": {
21+
"value": ""
22+
},
23+
"newAppServicePlanName": {
24+
"value": ""
25+
},
26+
"newAppServicePlanSku": {
27+
"value": {
28+
"name": "S1",
29+
"tier": "Standard",
30+
"size": "S1",
31+
"family": "S",
32+
"capacity": 1
33+
}
34+
},
35+
"newAppServicePlanLocation": {
36+
"value": ""
37+
},
38+
"newWebAppName": {
39+
"value": ""
40+
}
41+
}
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"appId": {
6+
"value": ""
7+
},
8+
"appSecret": {
9+
"value": ""
10+
},
11+
"botId": {
12+
"value": ""
13+
},
14+
"botSku": {
15+
"value": ""
16+
},
17+
"newAppServicePlanName": {
18+
"value": ""
19+
},
20+
"newAppServicePlanSku": {
21+
"value": {
22+
"name": "S1",
23+
"tier": "Standard",
24+
"size": "S1",
25+
"family": "S",
26+
"capacity": 1
27+
}
28+
},
29+
"appServicePlanLocation": {
30+
"value": ""
31+
},
32+
"existingAppServicePlan": {
33+
"value": ""
34+
},
35+
"newWebAppName": {
36+
"value": ""
37+
}
38+
}
39+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
{
2+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
3+
"contentVersion": "1.0.0.0",
4+
"parameters": {
5+
"groupLocation": {
6+
"type": "string",
7+
"metadata": {
8+
"description": "Specifies the location of the Resource Group."
9+
}
10+
},
11+
"groupName": {
12+
"type": "string",
13+
"metadata": {
14+
"description": "Specifies the name of the Resource Group."
15+
}
16+
},
17+
"appId": {
18+
"type": "string",
19+
"metadata": {
20+
"description": "Active Directory App ID, set as MicrosoftAppId in the Web App's Application Settings."
21+
}
22+
},
23+
"appSecret": {
24+
"type": "string",
25+
"metadata": {
26+
"description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings."
27+
}
28+
},
29+
"botId": {
30+
"type": "string",
31+
"metadata": {
32+
"description": "The globally unique and immutable bot ID. Also used to configure the displayName of the bot, which is mutable."
33+
}
34+
},
35+
"botSku": {
36+
"type": "string",
37+
"metadata": {
38+
"description": "The pricing tier of the Bot Service Registration. Acceptable values are F0 and S1."
39+
}
40+
},
41+
"newAppServicePlanName": {
42+
"type": "string",
43+
"metadata": {
44+
"description": "The name of the App Service Plan."
45+
}
46+
},
47+
"newAppServicePlanSku": {
48+
"type": "object",
49+
"defaultValue": {
50+
"name": "S1",
51+
"tier": "Standard",
52+
"size": "S1",
53+
"family": "S",
54+
"capacity": 1
55+
},
56+
"metadata": {
57+
"description": "The SKU of the App Service Plan. Defaults to Standard values."
58+
}
59+
},
60+
"newAppServicePlanLocation": {
61+
"type": "string",
62+
"metadata": {
63+
"description": "The location of the App Service Plan. Defaults to \"westus\"."
64+
}
65+
},
66+
"newWebAppName": {
67+
"type": "string",
68+
"defaultValue": "",
69+
"metadata": {
70+
"description": "The globally unique name of the Web App. Defaults to the value passed in for \"botId\"."
71+
}
72+
}
73+
},
74+
"variables": {
75+
"appServicePlanName": "[parameters('newAppServicePlanName')]",
76+
"resourcesLocation": "[parameters('newAppServicePlanLocation')]",
77+
"webAppName": "[if(empty(parameters('newWebAppName')), parameters('botId'), parameters('newWebAppName'))]",
78+
"siteHost": "[concat(variables('webAppName'), '.azurewebsites.net')]",
79+
"botEndpoint": "[concat('https://', variables('siteHost'), '/api/messages')]",
80+
"resourceGroupId": "[concat(subscription().id, '/resourceGroups/', parameters('groupName'))]"
81+
},
82+
"resources": [
83+
{
84+
"name": "[parameters('groupName')]",
85+
"type": "Microsoft.Resources/resourceGroups",
86+
"apiVersion": "2018-05-01",
87+
"location": "[parameters('groupLocation')]",
88+
"properties": {}
89+
},
90+
{
91+
"type": "Microsoft.Resources/deployments",
92+
"apiVersion": "2018-05-01",
93+
"name": "storageDeployment",
94+
"resourceGroup": "[parameters('groupName')]",
95+
"dependsOn": [
96+
"[resourceId('Microsoft.Resources/resourceGroups/', parameters('groupName'))]"
97+
],
98+
"properties": {
99+
"mode": "Incremental",
100+
"template": {
101+
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
102+
"contentVersion": "1.0.0.0",
103+
"parameters": {},
104+
"variables": {},
105+
"resources": [
106+
{
107+
"comments": "Create a new App Service Plan",
108+
"type": "Microsoft.Web/serverfarms",
109+
"name": "[variables('appServicePlanName')]",
110+
"apiVersion": "2018-02-01",
111+
"location": "[variables('resourcesLocation')]",
112+
"sku": "[parameters('newAppServicePlanSku')]",
113+
"properties": {
114+
"name": "[variables('appServicePlanName')]"
115+
}
116+
},
117+
{
118+
"comments": "Create a Web App using the new App Service Plan",
119+
"type": "Microsoft.Web/sites",
120+
"apiVersion": "2015-08-01",
121+
"location": "[variables('resourcesLocation')]",
122+
"kind": "app",
123+
"dependsOn": [
124+
"[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/serverfarms/', variables('appServicePlanName'))]"
125+
],
126+
"name": "[variables('webAppName')]",
127+
"properties": {
128+
"name": "[variables('webAppName')]",
129+
"serverFarmId": "[variables('appServicePlanName')]",
130+
"siteConfig": {
131+
"appSettings": [
132+
{
133+
"name": "WEBSITE_NODE_DEFAULT_VERSION",
134+
"value": "10.14.1"
135+
},
136+
{
137+
"name": "MicrosoftAppId",
138+
"value": "[parameters('appId')]"
139+
},
140+
{
141+
"name": "MicrosoftAppPassword",
142+
"value": "[parameters('appSecret')]"
143+
}
144+
],
145+
"cors": {
146+
"allowedOrigins": [
147+
"https://botservice.hosting.portal.azure.net",
148+
"https://hosting.onecloud.azure-test.net/"
149+
]
150+
}
151+
}
152+
}
153+
},
154+
{
155+
"apiVersion": "2017-12-01",
156+
"type": "Microsoft.BotService/botServices",
157+
"name": "[parameters('botId')]",
158+
"location": "global",
159+
"kind": "bot",
160+
"sku": {
161+
"name": "[parameters('botSku')]"
162+
},
163+
"properties": {
164+
"name": "[parameters('botId')]",
165+
"displayName": "[parameters('botId')]",
166+
"endpoint": "[variables('botEndpoint')]",
167+
"msaAppId": "[parameters('appId')]",
168+
"developerAppInsightsApplicationId": null,
169+
"developerAppInsightKey": null,
170+
"publishingCredentials": null,
171+
"storageResourceId": null
172+
},
173+
"dependsOn": [
174+
"[concat(variables('resourceGroupId'), '/providers/Microsoft.Web/sites/', variables('webAppName'))]"
175+
]
176+
}
177+
],
178+
"outputs": {}
179+
}
180+
}
181+
}
182+
]
183+
}

0 commit comments

Comments
 (0)