jovotech / jovo-framework

🔈 The React for Voice and Chat: Build Apps for Alexa, Messenger, Instagram, the Web, and more
https://www.jovo.tech
Apache License 2.0
1.68k stars 309 forks source link

Passing incorrect localeId to client Lex runtime #1260

Closed jrglg closed 2 years ago

jrglg commented 2 years ago

I'm submitting a...

Expected Behavior

Using Lex in Jovo Debugger should work well.

Current Behavior

With default configuration from Jovo docs for Lex, some errors are thrown that can make you think that the configuration of the bot or AWS is wrong. The locale attribute is needed to avoid this. If not specified, then the following method has to be adapted, because jovo.$request.getLocale() doesn't stick to the convention: https://docs.aws.amazon.com/lexv2/latest/dg/how-languages.html

  private getLocale(jovo: Jovo): string {
    return this.config.locale || jovo.$request.getLocale() || this.config.fallbackLocale;
  }

So I would change the name of the locale attribute to localeId, and then add it to the docs at: https://www.jovo.tech/marketplace/slu-lex#configuration

What's happening:

Testing my bot from the Lex simulator, I see these requests are sent:

{
 "botAliasId": "someID",
 "botId": "someID2",
 "localeId": "es_ES",
 "text": "some text",
 "sessionId": "22280xxx"
}

But, from LexSlu.ts, when generating the params object for the command: https://github.com/jovotech/jovo-framework/blob/fcadf2892b4ae61ac185fda91a398df371f64e36/integrations/slu-lex/src/LexSlu.ts#L124

an incorrect localeId is passed: "es", in my case. It should be "es_ES", as in the simulator and in: https://docs.aws.amazon.com/lexv2/latest/dg/how-languages.html

Error log

ResourceNotFoundException: UnknownError
    at deserializeAws_restJson1ResourceNotFoundExceptionResponse (omitted\node_modules\.pnpm\@aws-sdk+client-lex-runtime-v2@3.53.0\node_modules\@aws-sdk\client-lex-runtime-v2\dist-cjs\protocols\Aws_restJson1.js:1142:23)
    at deserializeAws_restJson1RecognizeTextCommandError (omitted\node_modules\.pnpm\@aws-sdk+client-lex-runtime-v2@3.53.0\node_modules\@aws-sdk\client-lex-runtime-v2\dist-cjs\protocols\Aws_restJson1.js:653:25)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at omitted\node_modules\.pnpm\@aws-sdk+middleware-serde@3.53.0\node_modules\@aws-sdk\middleware-serde\dist-cjs\deserializerMiddleware.js:7:24
    at omitted\node_modules\.pnpm\@aws-sdk+middleware-signing@3.53.0\node_modules\@aws-sdk\middleware-signing\dist-cjs\middleware.js:11:20
    at StandardRetryStrategy.retry (omitted\node_modules\.pnpm\@aws-sdk+middleware-retry@3.53.0\node_modules\@aws-sdk\middleware-retry\dist-cjs\StandardRetryStrategy.js:51:46)
    at omitted\node_modules\.pnpm\@aws-sdk+middleware-logger@3.53.0\node_modules\@aws-sdk\middleware-logger\dist-cjs\loggerMiddleware.js:6:22
    at LexSlu.processText (omitted\node_modules\.pnpm\@jovotech+slu-lex@4.2.3_@jovotech+framework@4.2.3\node_modules\@jovotech\slu-lex\src\LexSlu.ts:132:22)
    at LexSlu.nlu (omitted\node_modules\.pnpm\@jovotech+framework@4.2.3\node_modules\@jovotech\framework\src\plugins\InterpretationPlugin.ts:72:30)
    at Middleware.run (omitted\node_modules\.pnpm\@jovotech+framework@4.2.3\node_modules\@jovotech\framework\src\Middleware.ts:23:7) {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 404,
    requestId: '61b54aea-6571-487f-b2e8-12bc3d296321',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  '$response': HttpResponse {
    statusCode: 404,
    headers: {
      ':status': 404,
      'x-amzn-requestid': '61b54aea-6571-487f-b2e8-12bc3d296321',
      'x-amzn-errortype': 'ResourceNotFoundException:http://internal.amazon.com/coral/com.amazonaws.deepsense.runtimeservice.v2/',
      date: 'Fri, 11 Mar 2022 08:52:06 GMT',
      'content-type': 'application/json',
      'content-length': '2'
    },

Your Environment

aswetlow commented 2 years ago

Hey @jrglg

Renaming locale to localeId in the config does make sense to me. We'll discuss it with the team.

Unfortunately, Amazon uses different formats in its products. Lex uses es_ES, Alexa es-ES. For now, you can hard code the locale property in your Lex config to es_ES.

Feature suggestion:

Add a locale map to the Lex integration:

Example:

localeMap: {
  'es': 'es_ES'
}

This would map the request locale (e.g. es) to 'es_ES` which is compatible with Lex

rmtuckerphx commented 2 years ago

@aswetlow

I suggest something like this:

localeMap: { 'en': 'en_US', 'default': 'en_US' }

The logic for selecting the localeId would be:

private getLocale(request: JovoRequest): string {
    const DEFAULT_KEY = 'default';
    const key = request.getLocale() || DEFAULT_KEY;
    const locale = this.config.localeMap[key] || this.config.localeMap[DEFAULT_KEY] || 'en_US';

    return locale;
}
rmtuckerphx commented 2 years ago

@aswetlow @jrglg Created a PR for this.

jankoenig commented 2 years ago

Thank you @rmtuckerphx 🎉

This was merged and released yesterday.