From 6c28f1becb4dda10c1adb36f04cc425a8f9dca8c Mon Sep 17 00:00:00 2001 From: Tracy Boehrer Date: Fri, 8 Nov 2019 14:09:12 -0600 Subject: [PATCH 1/2] Added 17.multilingual-bot --- samples/17.multilingual-bot/README.md | 58 +++++ samples/17.multilingual-bot/app.py | 101 ++++++++ samples/17.multilingual-bot/bots/__init__.py | 6 + .../bots/multilingual_bot.py | 106 ++++++++ .../cards/welcomeCard.json | 46 ++++ samples/17.multilingual-bot/config.py | 17 ++ .../template-with-preexisting-rg.json | 242 ++++++++++++++++++ samples/17.multilingual-bot/requirements.txt | 3 + .../translation/__init__.py | 7 + .../translation/microsoft_translator.py | 39 +++ .../translation/translation_middleware.py | 78 ++++++ .../translation/translation_settings.py | 12 + 12 files changed, 715 insertions(+) create mode 100644 samples/17.multilingual-bot/README.md create mode 100644 samples/17.multilingual-bot/app.py create mode 100644 samples/17.multilingual-bot/bots/__init__.py create mode 100644 samples/17.multilingual-bot/bots/multilingual_bot.py create mode 100644 samples/17.multilingual-bot/cards/welcomeCard.json create mode 100644 samples/17.multilingual-bot/config.py create mode 100644 samples/17.multilingual-bot/deploymentTemplates/template-with-preexisting-rg.json create mode 100644 samples/17.multilingual-bot/requirements.txt create mode 100644 samples/17.multilingual-bot/translation/__init__.py create mode 100644 samples/17.multilingual-bot/translation/microsoft_translator.py create mode 100644 samples/17.multilingual-bot/translation/translation_middleware.py create mode 100644 samples/17.multilingual-bot/translation/translation_settings.py diff --git a/samples/17.multilingual-bot/README.md b/samples/17.multilingual-bot/README.md new file mode 100644 index 000000000..41666b6f3 --- /dev/null +++ b/samples/17.multilingual-bot/README.md @@ -0,0 +1,58 @@ +# Multilingual Bot + +Bot Framework v4 multilingual bot sample + +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/). + +## Concepts introduced in this sample + +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. + +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. + +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. + +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. +The API uses the most modern neural machine translation technology, as well as offering statistical machine translation technology. + +## Running the sample +- Clone the repository +```bash +git clone https://github.com/Microsoft/botbuilder-python.git +``` +- Activate your desired virtual environment +- Bring up a terminal, navigate to `botbuilder-python\samples\17.multilingual-bot` folder +- In the terminal, type `pip install -r requirements.txt` + +- 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). Paste the key in the `SUBSCRIPTION_KEY` and `SUBSCRIPTION_REGION` settings in the `config.py` file. + +- In the terminal, type `python app.py` + +## Testing the bot using Bot Framework Emulator +[Microsoft 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. + +- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) + +### Connect to bot using Bot Framework Emulator +- Launch Bot Framework Emulator +- File -> Open Bot +- Paste this URL in the emulator window - http://localhost:3978/api/messages + + +### Creating a custom middleware + +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. +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. + +### Microsoft Translator Text API + +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. +The API uses the most modern neural machine translation technology, as well as offering statistical machine translation technology. + +# Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Bot State](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-storage-concept?view=azure-bot-service-4.0) +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) +- [Channels and Bot Connector Service](https://docs.microsoft.com/en-us/azure/bot-service/bot-concepts?view=azure-bot-service-4.0) \ No newline at end of file diff --git a/samples/17.multilingual-bot/app.py b/samples/17.multilingual-bot/app.py new file mode 100644 index 000000000..c968cd633 --- /dev/null +++ b/samples/17.multilingual-bot/app.py @@ -0,0 +1,101 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import asyncio +import sys +from datetime import datetime +from types import MethodType + +from flask import Flask, request, Response +from botbuilder.core import ( + BotFrameworkAdapter, + BotFrameworkAdapterSettings, + MemoryStorage, + TurnContext, + UserState, +) +from botbuilder.schema import Activity, ActivityTypes + +from bots import MultiLingualBot + +# Create the loop and Flask app +from translation import TranslationMiddleware, MicrosoftTranslator + +LOOP = asyncio.get_event_loop() +app = Flask(__name__, instance_relative_config=True) +app.config.from_object("config.DefaultConfig") + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +SETTINGS = BotFrameworkAdapterSettings(app.config["APP_ID"], app.config["APP_PASSWORD"]) +ADAPTER = BotFrameworkAdapter(SETTINGS) + + +# Catch-all for errors. +async def on_error(self, context: TurnContext, error: Exception): + # This check writes out errors to console log .vs. app insights. + # NOTE: In production environment, you should consider logging this to Azure + # application insights. + print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr) + + # Send a message to the user + await context.send_activity("The bot encountered an error or bug.") + await context.send_activity("To continue to run this bot, please fix the bot source code.") + # Send a trace activity if we're talking to the Bot Framework Emulator + if context.activity.channel_id == 'emulator': + # Create a trace activity that contains the error object + trace_activity = Activity( + label="TurnError", + name="on_turn_error Trace", + timestamp=datetime.utcnow(), + type=ActivityTypes.trace, + value=f"{error}", + value_type="https://www.botframework.com/schemas/error" + ) + # Send a trace activity, which will be displayed in Bot Framework Emulator + await context.send_activity(trace_activity) + +ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) + +# Create MemoryStorage and state +MEMORY = MemoryStorage() +USER_STATE = UserState(MEMORY) + +# Create translation middleware and add to adapter +TRANSLATOR = MicrosoftTranslator(app.config["SUBSCRIPTION_KEY"], app.config["SUBSCRIPTION_REGION"]) +TRANSLATION_MIDDLEWARE = TranslationMiddleware(TRANSLATOR, USER_STATE) +ADAPTER.use(TRANSLATION_MIDDLEWARE) + +# Create Bot +BOT = MultiLingualBot(USER_STATE) + + +# Listen for incoming requests on /api/messages. +@app.route("/api/messages", methods=["POST"]) +def messages(): + # Main bot message handler. + if "application/json" in request.headers["Content-Type"]: + body = request.json + else: + return Response(status=415) + + activity = Activity().deserialize(body) + auth_header = ( + request.headers["Authorization"] if "Authorization" in request.headers else "" + ) + + try: + task = LOOP.create_task( + ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + ) + LOOP.run_until_complete(task) + return Response(status=201) + except Exception as exception: + raise exception + + +if __name__ == "__main__": + try: + app.run(debug=False, port=app.config["PORT"]) # nosec debug + except Exception as exception: + raise exception diff --git a/samples/17.multilingual-bot/bots/__init__.py b/samples/17.multilingual-bot/bots/__init__.py new file mode 100644 index 000000000..377f4a8ec --- /dev/null +++ b/samples/17.multilingual-bot/bots/__init__.py @@ -0,0 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .multilingual_bot import MultiLingualBot + +__all__ = ["MultiLingualBot"] diff --git a/samples/17.multilingual-bot/bots/multilingual_bot.py b/samples/17.multilingual-bot/bots/multilingual_bot.py new file mode 100644 index 000000000..4ca973db4 --- /dev/null +++ b/samples/17.multilingual-bot/bots/multilingual_bot.py @@ -0,0 +1,106 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import json +import os + +from botbuilder.core import ActivityHandler, TurnContext, UserState, CardFactory, MessageFactory +from botbuilder.schema import ChannelAccount, Attachment, SuggestedActions, CardAction, ActionTypes + +from translation.translation_settings import TranslationSettings + +""" +This bot demonstrates how to use Microsoft Translator. +More information can be found at: +https://docs.microsoft.com/en-us/azure/cognitive-services/translator/translator-info-overview" +""" + + +class MultiLingualBot(ActivityHandler): + def __init__(self, user_state: UserState): + if user_state is None: + raise TypeError( + "[MultiLingualBot]: Missing parameter. user_state is required but None was given" + ) + + self.user_state = user_state + + self.language_preference_accessor = self.user_state.create_property("LanguagePreference") + + async def on_members_added_activity( + self, members_added: [ChannelAccount], turn_context: TurnContext + ): + # Greet anyone that was not the target (recipient) of this message. + # To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details. + for member in members_added: + if member.id != turn_context.activity.recipient.id: + await turn_context.send_activity( + MessageFactory.attachment(self._create_adaptive_card_attachment()) + ) + await turn_context.send_activity( + "This bot will introduce you to translation middleware. Say \'hi\' to get started." + ) + + async def on_message_activity(self, turn_context: TurnContext): + if self._is_language_change_requested(turn_context.activity.text): + # If the user requested a language change through the suggested actions with values "es" or "en", + # simply change the user's language preference in the user state. + # The translation middleware will catch this setting and translate both ways to the user's + # selected language. + # If Spanish was selected by the user, the reply below will actually be shown in Spanish to the user. + current_language = turn_context.activity.text.lower() + if current_language == TranslationSettings.english_english.value \ + or current_language == TranslationSettings.spanish_english.value: + lang = TranslationSettings.english_english.value + else: + lang = TranslationSettings.english_spanish.value + + await self.language_preference_accessor.set(turn_context, lang) + + await turn_context.send_activity(f"Your current language code is: {lang}") + + # Save the user profile updates into the user state. + await self.user_state.save_changes(turn_context) + else: + # Show the user the possible options for language. If the user chooses a different language + # than the default, then the translation middleware will pick it up from the user state and + # translate messages both ways, i.e. user to bot and bot to user. + reply = MessageFactory.text("Choose your language:") + reply.suggested_actions = SuggestedActions( + actions=[ + CardAction( + title="EspaƱol", + type=ActionTypes.post_back, + value=TranslationSettings.english_spanish.value + ), + CardAction( + title="English", + type=ActionTypes.post_back, + value=TranslationSettings.english_english.value + ) + ] + ) + + await turn_context.send_activity(reply) + + def _create_adaptive_card_attachment(self) -> Attachment: + """ + Load attachment from file. + :return: + """ + card_path = os.path.join(os.getcwd(), "cards/welcomeCard.json") + with open(card_path, "rt") as in_file: + card_data = json.load(in_file) + + return CardFactory.adaptive_card(card_data) + + def _is_language_change_requested(self, utterance: str) -> bool: + if not utterance: + return False + + utterance = utterance.lower() + return utterance == TranslationSettings.english_spanish.value \ + or utterance == TranslationSettings.english_english.value \ + or utterance == TranslationSettings.spanish_spanish.value \ + or utterance == TranslationSettings.spanish_english.value + diff --git a/samples/17.multilingual-bot/cards/welcomeCard.json b/samples/17.multilingual-bot/cards/welcomeCard.json new file mode 100644 index 000000000..100aa5287 --- /dev/null +++ b/samples/17.multilingual-bot/cards/welcomeCard.json @@ -0,0 +1,46 @@ +{ + "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", + "type": "AdaptiveCard", + "version": "1.0", + "body": [ + { + "type": "Image", + "url": "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU", + "size": "stretch" + }, + { + "type": "TextBlock", + "spacing": "medium", + "size": "default", + "weight": "bolder", + "text": "Welcome to Bot Framework!", + "wrap": true, + "maxLines": 0 + }, + { + "type": "TextBlock", + "size": "default", + "isSubtle": "true", + "text": "Now that you have successfully run your bot, follow the links in this Adaptive Card to expand your knowledge of Bot Framework.", + "wrap": true, + "maxLines": 0 + } + ], + "actions": [ + { + "type": "Action.OpenUrl", + "title": "Get an overview", + "url": "https://docs.microsoft.com/en-us/azure/bot-service/?view=azure-bot-service-4.0" + }, + { + "type": "Action.OpenUrl", + "title": "Ask a question", + "url": "https://stackoverflow.com/questions/tagged/botframework" + }, + { + "type": "Action.OpenUrl", + "title": "Learn how to deploy", + "url": "https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-deploy-azure?view=azure-bot-service-4.0" + } + ] +} \ No newline at end of file diff --git a/samples/17.multilingual-bot/config.py b/samples/17.multilingual-bot/config.py new file mode 100644 index 000000000..7d323dda5 --- /dev/null +++ b/samples/17.multilingual-bot/config.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import os + +""" Bot Configuration """ + + +class DefaultConfig: + """ Bot Configuration """ + + PORT = 3978 + APP_ID = os.environ.get("MicrosoftAppId", "") + APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "") + SUBSCRIPTION_KEY = os.environ.get("SubscriptionKey", "") + SUBSCRIPTION_REGION = os.environ.get("SubscriptionRegion", "") diff --git a/samples/17.multilingual-bot/deploymentTemplates/template-with-preexisting-rg.json b/samples/17.multilingual-bot/deploymentTemplates/template-with-preexisting-rg.json new file mode 100644 index 000000000..bff8c096d --- /dev/null +++ b/samples/17.multilingual-bot/deploymentTemplates/template-with-preexisting-rg.json @@ -0,0 +1,242 @@ +{ + "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", + "contentVersion": "1.0.0.0", + "parameters": { + "appId": { + "type": "string", + "metadata": { + "description": "Active Directory App ID, set as MicrosoftAppId in the Web App's Application Settings." + } + }, + "appSecret": { + "type": "string", + "metadata": { + "description": "Active Directory App Password, set as MicrosoftAppPassword in the Web App's Application Settings." + } + }, + "botId": { + "type": "string", + "metadata": { + "description": "The globally unique and immutable bot ID. Also used to configure the displayName of the bot, which is mutable." + } + }, + "botSku": { + "defaultValue": "F0", + "type": "string", + "metadata": { + "description": "The pricing tier of the Bot Service Registration. Acceptable values are F0 and S1." + } + }, + "newAppServicePlanName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The name of the new App Service Plan." + } + }, + "newAppServicePlanSku": { + "type": "object", + "defaultValue": { + "name": "S1", + "tier": "Standard", + "size": "S1", + "family": "S", + "capacity": 1 + }, + "metadata": { + "description": "The SKU of the App Service Plan. Defaults to Standard values." + } + }, + "appServicePlanLocation": { + "type": "string", + "metadata": { + "description": "The location of the App Service Plan." + } + }, + "existingAppServicePlan": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "Name of the existing App Service Plan used to create the Web App for the bot." + } + }, + "newWebAppName": { + "type": "string", + "defaultValue": "", + "metadata": { + "description": "The globally unique name of the Web App. Defaults to the value passed in for \"botId\"." + } + } + }, + "variables": { + "defaultAppServicePlanName": "[if(empty(parameters('existingAppServicePlan')), 'createNewAppServicePlan', parameters('existingAppServicePlan'))]", + "useExistingAppServicePlan": "[not(equals(variables('defaultAppServicePlanName'), 'createNewAppServicePlan'))]", + "servicePlanName": "[if(variables('useExistingAppServicePlan'), parameters('existingAppServicePlan'), parameters('newAppServicePlanName'))]", + "publishingUsername": "[concat('$', parameters('newWebAppName'))]", + "resourcesLocation": "[parameters('appServicePlanLocation')]", + "webAppName": "[if(empty(parameters('newWebAppName')), parameters('botId'), parameters('newWebAppName'))]", + "siteHost": "[concat(variables('webAppName'), '.azurewebsites.net')]", + "botEndpoint": "[concat('https://', variables('siteHost'), '/api/messages')]" + }, + "resources": [ + { + "comments": "Create a new Linux App Service Plan if no existing App Service Plan name was passed in.", + "type": "Microsoft.Web/serverfarms", + "apiVersion": "2016-09-01", + "name": "[variables('servicePlanName')]", + "location": "[variables('resourcesLocation')]", + "sku": "[parameters('newAppServicePlanSku')]", + "kind": "linux", + "properties": { + "name": "[variables('servicePlanName')]", + "perSiteScaling": false, + "reserved": true, + "targetWorkerCount": 0, + "targetWorkerSizeId": 0 + } + }, + { + "comments": "Create a Web App using a Linux App Service Plan", + "type": "Microsoft.Web/sites", + "apiVersion": "2016-08-01", + "name": "[variables('webAppName')]", + "location": "[variables('resourcesLocation')]", + "dependsOn": [ + "[resourceId('Microsoft.Web/serverfarms/', variables('servicePlanName'))]" + ], + "kind": "app,linux", + "properties": { + "enabled": true, + "hostNameSslStates": [ + { + "name": "[concat(parameters('newWebAppName'), '.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Standard" + }, + { + "name": "[concat(parameters('newWebAppName'), '.scm.azurewebsites.net')]", + "sslState": "Disabled", + "hostType": "Repository" + } + ], + "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('servicePlanName'))]", + "reserved": true, + "scmSiteAlsoStopped": false, + "clientAffinityEnabled": false, + "clientCertEnabled": false, + "hostNamesDisabled": false, + "containerSize": 0, + "dailyMemoryTimeQuota": 0, + "httpsOnly": false, + "siteConfig": { + "appSettings": [ + { + "name": "MicrosoftAppId", + "value": "[parameters('appId')]" + }, + { + "name": "MicrosoftAppPassword", + "value": "[parameters('appSecret')]" + }, + { + "name": "SCM_DO_BUILD_DURING_DEPLOYMENT", + "value": "true" + } + ], + "cors": { + "allowedOrigins": [ + "https://botservice.hosting.portal.azure.net", + "https://hosting.onecloud.azure-test.net/" + ] + } + } + } + }, + { + "type": "Microsoft.Web/sites/config", + "apiVersion": "2016-08-01", + "name": "[concat(variables('webAppName'), '/web')]", + "location": "[variables('resourcesLocation')]", + "dependsOn": [ + "[resourceId('Microsoft.Web/sites', variables('webAppName'))]" + ], + "properties": { + "numberOfWorkers": 1, + "defaultDocuments": [ + "Default.htm", + "Default.html", + "Default.asp", + "index.htm", + "index.html", + "iisstart.htm", + "default.aspx", + "index.php", + "hostingstart.html" + ], + "netFrameworkVersion": "v4.0", + "phpVersion": "", + "pythonVersion": "", + "nodeVersion": "", + "linuxFxVersion": "PYTHON|3.7", + "requestTracingEnabled": false, + "remoteDebuggingEnabled": false, + "remoteDebuggingVersion": "VS2017", + "httpLoggingEnabled": true, + "logsDirectorySizeLimit": 35, + "detailedErrorLoggingEnabled": false, + "publishingUsername": "[variables('publishingUsername')]", + "scmType": "None", + "use32BitWorkerProcess": true, + "webSocketsEnabled": false, + "alwaysOn": false, + "appCommandLine": "", + "managedPipelineMode": "Integrated", + "virtualApplications": [ + { + "virtualPath": "/", + "physicalPath": "site\\wwwroot", + "preloadEnabled": false, + "virtualDirectories": null + } + ], + "winAuthAdminState": 0, + "winAuthTenantState": 0, + "customAppPoolIdentityAdminState": false, + "customAppPoolIdentityTenantState": false, + "loadBalancing": "LeastRequests", + "routingRules": [], + "experiments": { + "rampUpRules": [] + }, + "autoHealEnabled": false, + "vnetName": "", + "minTlsVersion": "1.2", + "ftpsState": "AllAllowed", + "reservedInstanceCount": 0 + } + }, + { + "apiVersion": "2017-12-01", + "type": "Microsoft.BotService/botServices", + "name": "[parameters('botId')]", + "location": "global", + "kind": "bot", + "sku": { + "name": "[parameters('botSku')]" + }, + "properties": { + "name": "[parameters('botId')]", + "displayName": "[parameters('botId')]", + "endpoint": "[variables('botEndpoint')]", + "msaAppId": "[parameters('appId')]", + "developerAppInsightsApplicationId": null, + "developerAppInsightKey": null, + "publishingCredentials": null, + "storageResourceId": null + }, + "dependsOn": [ + "[resourceId('Microsoft.Web/sites/', variables('webAppName'))]" + ] + } + ] +} \ No newline at end of file diff --git a/samples/17.multilingual-bot/requirements.txt b/samples/17.multilingual-bot/requirements.txt new file mode 100644 index 000000000..32e489163 --- /dev/null +++ b/samples/17.multilingual-bot/requirements.txt @@ -0,0 +1,3 @@ +requests +botbuilder-core>=4.4.0b1 +flask>=1.0.3 diff --git a/samples/17.multilingual-bot/translation/__init__.py b/samples/17.multilingual-bot/translation/__init__.py new file mode 100644 index 000000000..7112f41c0 --- /dev/null +++ b/samples/17.multilingual-bot/translation/__init__.py @@ -0,0 +1,7 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .microsoft_translator import MicrosoftTranslator +from .translation_middleware import TranslationMiddleware + +__all__ = ["MicrosoftTranslator", "TranslationMiddleware"] diff --git a/samples/17.multilingual-bot/translation/microsoft_translator.py b/samples/17.multilingual-bot/translation/microsoft_translator.py new file mode 100644 index 000000000..e6a0ef16f --- /dev/null +++ b/samples/17.multilingual-bot/translation/microsoft_translator.py @@ -0,0 +1,39 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import requests +import uuid + + +class MicrosoftTranslator: + def __init__(self, subscription_key: str, subscription_region: str): + self.subscription_key = subscription_key + self.subscription_region = subscription_region + + # Don't forget to replace with your Cog Services location! + # Our Flask route will supply two arguments: text_input and language_output. + # When the translate text button is pressed in our Flask app, the Ajax request + # will grab these values from our web app, and use them in the request. + # See main.js for Ajax calls. + async def translate(self, text_input, language_output): + base_url = 'https://api.cognitive.microsofttranslator.com' + path = '/translate?api-version=3.0' + params = '&to=' + language_output + constructed_url = base_url + path + params + + headers = { + 'Ocp-Apim-Subscription-Key': self.subscription_key, + 'Ocp-Apim-Subscription-Region': self.subscription_region, + 'Content-type': 'application/json', + 'X-ClientTraceId': str(uuid.uuid4()) + } + + # You can pass more than one object in body. + body = [{ + 'text': text_input + }] + response = requests.post(constructed_url, headers=headers, json=body) + j = response.json() + + # for this sample, return the first translation + return j[0]["translations"][0]["text"] diff --git a/samples/17.multilingual-bot/translation/translation_middleware.py b/samples/17.multilingual-bot/translation/translation_middleware.py new file mode 100644 index 000000000..3b2ee0930 --- /dev/null +++ b/samples/17.multilingual-bot/translation/translation_middleware.py @@ -0,0 +1,78 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from typing import Callable, Awaitable, List + +from botbuilder.core import Middleware, UserState, TurnContext +from botbuilder.schema import Activity, ActivityTypes + +from translation import MicrosoftTranslator +from translation.translation_settings import TranslationSettings + +""" +Middleware for translating text between the user and bot. +Uses the Microsoft Translator Text API. +""" + + +class TranslationMiddleware(Middleware): + def __init__(self, translator: MicrosoftTranslator, user_state: UserState): + self.translator = translator + self.language_preference_accessor = user_state.create_property("LanguagePreference") + + async def on_turn( + self, turn_context: TurnContext, logic: Callable[[TurnContext], Awaitable] + ): + """ + Processes an incoming activity. + :param turn_context: + :param logic: + :return: + """ + translate = await self._should_translate(turn_context) + if translate and turn_context.activity.type == ActivityTypes.message: + turn_context.activity.text = await self.translator.translate( + turn_context.activity.text, TranslationSettings.default_language.value + ) + + async def aux_on_send( + context: TurnContext, activities: List[Activity], next_send: Callable + ): + user_language = await self.language_preference_accessor.get( + context, TranslationSettings.default_language.value + ) + should_translate = user_language != TranslationSettings.default_language.value + + # Translate messages sent to the user to user language + if should_translate: + for activity in activities: + await self._translate_message_activity(activity, user_language) + + return await next_send() + + async def aux_on_update( + context: TurnContext, activity: Activity, next_update: Callable + ): + user_language = await self.language_preference_accessor.get( + context, TranslationSettings.default_language.value + ) + should_translate = user_language != TranslationSettings.default_language.value + + # Translate messages sent to the user to user language + if should_translate and activity.type == ActivityTypes.message: + await self._translate_message_activity(activity, user_language) + + return await next_update() + + turn_context.on_send_activities(aux_on_send) + turn_context.on_update_activity(aux_on_update) + + await logic() + + async def _should_translate(self, turn_context: TurnContext) -> bool: + user_language = await self.language_preference_accessor.get(turn_context, TranslationSettings.default_language.value) + return user_language != TranslationSettings.default_language.value + + async def _translate_message_activity(self, activity: Activity, target_locale: str): + if activity.type == ActivityTypes.message: + activity.text = await self.translator.translate(activity.text, target_locale) diff --git a/samples/17.multilingual-bot/translation/translation_settings.py b/samples/17.multilingual-bot/translation/translation_settings.py new file mode 100644 index 000000000..aee41542d --- /dev/null +++ b/samples/17.multilingual-bot/translation/translation_settings.py @@ -0,0 +1,12 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from enum import Enum + + +class TranslationSettings(str, Enum): + default_language = "en" + english_english = "en" + english_spanish = "es" + spanish_english = "in" + spanish_spanish = "it" From 52a1d84ab38c2072943f9df55db2c89d6b219fcc Mon Sep 17 00:00:00 2001 From: Tracy Boehrer Date: Mon, 11 Nov 2019 12:22:52 -0600 Subject: [PATCH 2/2] 17.multilingual-bot suggested corrections --- samples/17.multilingual-bot/app.py | 4 ++-- samples/17.multilingual-bot/bots/multilingual_bot.py | 11 +++++------ .../translation/microsoft_translator.py | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/samples/17.multilingual-bot/app.py b/samples/17.multilingual-bot/app.py index c968cd633..20b1490b4 100644 --- a/samples/17.multilingual-bot/app.py +++ b/samples/17.multilingual-bot/app.py @@ -32,7 +32,7 @@ # Catch-all for errors. -async def on_error(self, context: TurnContext, error: Exception): +async def on_error(context: TurnContext, error: Exception): # This check writes out errors to console log .vs. app insights. # NOTE: In production environment, you should consider logging this to Azure # application insights. @@ -55,7 +55,7 @@ async def on_error(self, context: TurnContext, error: Exception): # Send a trace activity, which will be displayed in Bot Framework Emulator await context.send_activity(trace_activity) -ADAPTER.on_turn_error = MethodType(on_error, ADAPTER) +ADAPTER.on_turn_error = on_error # Create MemoryStorage and state MEMORY = MemoryStorage() diff --git a/samples/17.multilingual-bot/bots/multilingual_bot.py b/samples/17.multilingual-bot/bots/multilingual_bot.py index 4ca973db4..8ff3de599 100644 --- a/samples/17.multilingual-bot/bots/multilingual_bot.py +++ b/samples/17.multilingual-bot/bots/multilingual_bot.py @@ -28,7 +28,7 @@ def __init__(self, user_state: UserState): self.language_preference_accessor = self.user_state.create_property("LanguagePreference") async def on_members_added_activity( - self, members_added: [ChannelAccount], turn_context: TurnContext + self, members_added: [ChannelAccount], turn_context: TurnContext ): # Greet anyone that was not the target (recipient) of this message. # To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details. @@ -99,8 +99,7 @@ def _is_language_change_requested(self, utterance: str) -> bool: return False utterance = utterance.lower() - return utterance == TranslationSettings.english_spanish.value \ - or utterance == TranslationSettings.english_english.value \ - or utterance == TranslationSettings.spanish_spanish.value \ - or utterance == TranslationSettings.spanish_english.value - + return (utterance == TranslationSettings.english_spanish.value + or utterance == TranslationSettings.english_english.value + or utterance == TranslationSettings.spanish_spanish.value + or utterance == TranslationSettings.spanish_english.value) diff --git a/samples/17.multilingual-bot/translation/microsoft_translator.py b/samples/17.multilingual-bot/translation/microsoft_translator.py index e6a0ef16f..4b9a796c8 100644 --- a/samples/17.multilingual-bot/translation/microsoft_translator.py +++ b/samples/17.multilingual-bot/translation/microsoft_translator.py @@ -33,7 +33,7 @@ async def translate(self, text_input, language_output): 'text': text_input }] response = requests.post(constructed_url, headers=headers, json=body) - j = response.json() + json_response = response.json() # for this sample, return the first translation - return j[0]["translations"][0]["text"] + return json_response[0]["translations"][0]["text"]