/wa-translator

Cloud Function designed to be used with Watson Assistant and Language Translator in order to translate a conversation with a user from English to their preferred language without the need for an orchestration layer.

Primary LanguageJavaScriptApache License 2.0Apache-2.0

Watson Assistant Translations from Cloud Functions

This is a Cloud Function designed to be used with Watson Assistant and Language Translator in order to translate a conversation with a user from English to their preferred language without the need for an orchestration layer.

Demo

translator-bot

Prerequisites

  • You should have an instance of Watson Assistant, Language Translator, and Cloud Functions created in your IBM Cloud account
  • Install the IBM Cloud CLI
  • Install Node.js

Part I: Import the skill to your Watson Assistant Instance

  1. If you haven't already, create an instance each of Watson Assistant, Language Translator, and Cloud Functions

  2. Download the files in this repository: git clone https://github.com/modlanglais/wa-translator

  3. Import skill-WA-Translator.json to your Watson Assistant instance

  4. Create and attach an Assistant to your skill and make note of the API key, assistant ID, and URL

Part II: Create the Cloud Function

  1. Run npm install in the project directory to install the dependencies

  2. Open index.js and update the following with your service credentials:

     let wa_apikey = "{your-watson-assisatant-api-key}";
     let wa_url = "{watson-assistant-url}";
     let wa_version = "{watson-assistant-version}";
     let assistantId = "{assistant-id-to-translate}";
    
     let lt_apikey = "{your-language-translator-api-key}";
     let lt_url = "{language-translator-url}";
     let lt_version = "{language-translator-version}";
    

    This will add your credentials for your specific Watson Assistant (wa_) and Language Translator (lt_) services.

  3. Open your Terminal and navigate to the root directory of your app. Then zip all of your application files together using the following command:

    zip -r my-action.zip *

    You can replace my-action.zip with the name of your choosing, i.e. wa-translator.zip or morgans-function.zip

  4. In the terminal, login to your IBM Cloud account using the CLI:

    ibmcloud login

    If you have trouble, please see the documentation. You will need to use the IBM Cloud CLI to point to the namespace in which you created your Cloud Function

  5. Issue the following command to push your new zipped Cloud Function to your IBM Cloud Functions service: ibmcloud fn action create my-action my-action.zip --kind nodejs:10

    You can replace my-action with whatever you would like to name your cloud function. my-action.zip should reflect the name of the .zip file you created in step 5

  6. (Optional) If you make changes to index.js and need to push the new changes, use the following command to update your function

    ibmcloud fn action update my-action my-action.zip --kind nodejs:10

Part III: Retrieve your IBM Cloud Function URL

  1. Open your Cloud Functions dashboard here: https://cloud.ibm.com/functions/

  2. Select the namespace you wish to deploy the function in from the drop-down, then click "Actions" on the left nav bar

    cf-screenshot

  3. Click my-action or the name that you previously gave your action in step 7

  4. Select Endpoints then check "Enable as web action" and click "Save"

  5. Copy the URL in the "Web Action" section and save for later

    cf-webaction

Part IV: Add the Webhook to Your Chatbot

  1. Open the skill you imported in Watson Assistant earlier

  2. Navigate to Options -> Webhooks wa-webhooks

  3. Replace this URL with your webhook URL that you copied in step 15

  4. Important: Make sure you append ".json" to the end of this URL in order for Watson Assistant to process the data correctly

  5. That's it! Your user may now select their preferred language and Language Translator will automatically translate your English-language chatbot.

Debugging

To debug and run locally... un-comment the last line in index.js which invokes the main() method. Then run as you would any Node app: node index.js