postText()

in lex-web-ui/src/lib/lex/client.js [130:193]


  postText(inputText, localeId, sessionAttributes = {}) {
    let postTextReq;
    if (this.isV2Bot) {
      postTextReq = this.lexRuntimeClient.recognizeText({
        botAliasId: this.botV2AliasId,
        botId: this.botV2Id,
        localeId: localeId ? localeId : 'en_US',
        sessionId: this.userId,
        text: inputText,
        sessionState: {
          sessionAttributes,
        },
      });
    } else {
      postTextReq = this.lexRuntimeClient.postText({
        botAlias: this.botAlias,
        botName: this.botName,
        userId: this.userId,
        inputText,
        sessionAttributes,
      });
    }
    return this.credentials.getPromise()
      .then(creds => creds && this.initCredentials(creds))
      .then(async () => {
        const res = await postTextReq.promise();
        if (res.sessionState) { // this is v2 response
          res.intentName = res.sessionState.intent.name;
          res.slots = res.sessionState.intent.slots;
          res.sessionAttributes = res.sessionState.sessionAttributes;
          res.dialogState = res.sessionState.intent.state;
          res.slotToElicit = res.sessionState.dialogAction.slotToElicit;
          const finalMessages = [];
          if (res.messages && res.messages.length > 0) {
            res.messages.forEach((mes) => {
              if (mes.contentType === 'ImageResponseCard') {
                res.responseCard = {};
                res.responseCard.version = '1';
                res.responseCard.contentType = 'application/vnd.amazonaws.card.generic';
                res.responseCard.genericAttachments = [];
                res.responseCard.genericAttachments.push(mes.imageResponseCard);
              } else {
                /* eslint-disable no-lonely-if */
                if (mes.contentType) { // push v1 style messages for use in the UI
                  const v1Format = { type: mes.contentType, value: mes.content };
                  finalMessages.push(v1Format);
                }
              }
            });
          }
          if (finalMessages.length > 0) {
            const msg = `{"messages": ${JSON.stringify(finalMessages)} }`;
            res.message = msg;
          } else {
            // handle the case where no message was returned in the V2 response. Most likely only a
            // ImageResponseCard was returned. Append a placeholder with an empty string.
            finalMessages.push({ type: "PlainText", value: "" });
            const msg = `{"messages": ${JSON.stringify(finalMessages)} }`;
            res.message = msg;
          }
        }
        return res;
      });
  }