async function configure()

in packages/amplify-category-interactions/provider-utils/awscloudformation/service-walkthroughs/lex-walkthrough.js [50:347]


async function configure(context, defaultValuesFilename, serviceMetadata, resourceName) {
  const { amplify, print } = context;
  context.exeInfo = amplify.getProjectDetails();

  const { inputs, samples } = serviceMetadata;

  const defaultValuesSrc = `${__dirname}/../default-values/${defaultValuesFilename}`;
  const { getAllDefaults } = require(defaultValuesSrc);

  const defaultValues = getAllDefaults(amplify.getProjectDetails());

  const projectBackendDirPath = context.amplify.pathManager.getBackendDirPath();

  print.info('');
  print.info('Welcome to the Amazon Lex chatbot wizard');
  print.info('You will be asked a series of questions to help determine how to best construct your chatbot.');
  print.info('');

  // Ask resource name question

  const resourceQuestion = {
    type: inputs[0].type,
    name: inputs[0].key,
    message: inputs[0].question,
    validate: amplify.inputValidation(inputs[0]),
    default: answer => {
      const defaultValue = defaultValues[inputs[0].key];
      return answer.resourceName || defaultValue;
    },
  };

  let answer = {};
  let startChoice;

  if (!resourceName) {
    answer = await inquirer.prompt(resourceQuestion);
    resourceName = answer.resourceName;

    // If it is a new chatbot, ask if they want to start with a sample or from scratch
    const startQuestion = {
      type: inputs[1].type,
      name: inputs[1].key,
      message: inputs[1].question,
      choices: inputs[1].options,
    };

    startChoice = await inquirer.prompt(startQuestion);
  } else {
    startChoice = { startChoice: 'Update an existing chatbot' };
  }

  const botNameQuestion = {
    type: inputs[3].type,
    name: inputs[3].key,
    message: inputs[3].question,
    validate: amplify.inputValidation(inputs[3]),
    // default: defaultValues[inputs[3].key]
  };

  const coppaQuestion = {
    type: inputs[4].type,
    name: inputs[4].key,
    message: inputs[4].question,
    default: inputs[4].default,
  };

  let botName;
  let intentName;
  let answers;
  let parameters;
  let deleteIntentConfirmed = false;

  if (startChoice[inputs[1].key] === 'Start with a sample') {
    // TODO: get list of samples from Lex, if possible
    // Currently samples are hardcoded in supported-services.json
    const sampleChatbotQuestion = {
      type: inputs[2].type,
      name: inputs[2].key,
      message: inputs[2].question,
      choices: inputs[2].options,
    };
    botName = await inquirer.prompt(sampleChatbotQuestion);
    botName = botName[inputs[2].key];

    let coppa = await inquirer.prompt(coppaQuestion);
    coppa = coppa[inputs[4].key];
    if (coppa) {
      print.info('');
      print.info('You must obtain any required verifiable parental consent under COPPA.');
      print.info('');
    }

    const intents = samples[botName];

    answers = {
      resourceName,
      intents,
      outputVoice: 'Matthew',
      botName,
      coppa,
    };
  } else if (startChoice[inputs[1].key] === 'Update an existing chatbot') {
    if (resourceName) {
      const resourceDirPath = path.join(projectBackendDirPath, category, resourceName);
      const parametersFilePath = path.join(resourceDirPath, parametersFileName);
      try {
        parameters = context.amplify.readJsonFile(parametersFilePath);
      } catch (e) {
        parameters = {};
      }
    } else {
      context.print.error('No chatbots to update');
    }

    const addUpdateIntentQuestion = {
      type: inputs[6].type,
      name: inputs[6].key,
      message: inputs[6].question,
      choices: inputs[6].options,
    };
    let utterances = [];
    const intents = [];
    let slots = [];
    let newSlotTypes = [];
    const intentChoice = await inquirer.prompt(addUpdateIntentQuestion);
    if (intentChoice[inputs[6].key] === 'Update an existing intent') {
      const intentList = parameters.intents.map(x => x.intentName);
      const chooseIntent = {
        type: inputs[7].type,
        name: inputs[7].key,
        message: inputs[7].question,
        choices: intentList,
      };
      intentName = await inquirer.prompt(chooseIntent);
      intentName = intentName[inputs[7].key];

      const addUtteranceQuestion = {
        type: inputs[8].type,
        name: inputs[8].key,
        message: inputs[8].question,
        default: inputs[8].default,
      };
      const addUtteranceAnswer = await inquirer.prompt(addUtteranceQuestion);
      if (addUtteranceAnswer[inputs[8].key]) {
        utterances = await addUtterance(context, intentName, botName, resourceName, serviceMetadata);
      }

      const addSlotQuestion = {
        type: inputs[9].type,
        name: inputs[9].key,
        message: inputs[9].question,
        default: inputs[9].default,
      };
      const addSlotAnswer = await inquirer.prompt(addSlotQuestion);

      let slotReturn = [];
      if (addSlotAnswer[inputs[9].key]) {
        slotReturn = await addSlot(context, intentName, botName, resourceName, serviceMetadata, parameters);
      }
      if (slotReturn.length > 1) {
        newSlotTypes = slotReturn[1];
      }
      slots = slotReturn[0];
    } else if (intentChoice[inputs[6].key] === 'Add an intent') {
      let continueAddingIntents = true;
      const addAnotherIntentQuestion = {
        type: inputs[23].type,
        name: inputs[23].key,
        message: inputs[23].question,
        default: inputs[23].default,
      };
      while (continueAddingIntents) {
        intents.push(await addIntent(context, botName, resourceName, serviceMetadata, intents, parameters));
        continueAddingIntents = await inquirer.prompt(addAnotherIntentQuestion);
        continueAddingIntents = continueAddingIntents[inputs[23].key];
      }
    } else if (intentChoice[inputs[6].key] === 'Delete an intent') {
      const intentList = parameters.intents.map(x => x.intentName);
      const chooseIntent = {
        type: inputs[7].type,
        name: inputs[7].key,
        message: inputs[7].question,
        choices: intentList,
      };
      intentName = await inquirer.prompt(chooseIntent);
      intentName = intentName[inputs[7].key];

      const deleteIntentConfirmation = {
        type: inputs[31].type,
        name: inputs[31].key,
        message: inputs[31].question,
      };
      deleteIntentConfirmed = await inquirer.prompt(deleteIntentConfirmation);
      deleteIntentConfirmed = deleteIntentConfirmed[inputs[31].key];
    } else {
      context.print.error('Valid option not chosen');
    }
    answers = {
      resourceName,
      botName: parameters.botName,
      intentName,
      utterances,
      intents,
      slots,
      newSlotTypes,
    };
  } else if (startChoice[inputs[1].key] === 'Start from scratch') {
    botName = await inquirer.prompt(botNameQuestion);
    botName = botName[inputs[3].key];

    const outputVoiceQuestion = {
      type: inputs[10].type,
      name: inputs[10].key,
      message: inputs[10].question,
      choices: inputs[10].options,
    };
    let outputVoice = await inquirer.prompt(outputVoiceQuestion);
    outputVoice = outputVoice[inputs[10].key];

    const sessionTimeoutQuestion = {
      type: inputs[11].type,
      name: inputs[11].key,
      message: inputs[11].question,
      default: defaultValues[inputs[11].key],
    };
    let sessionTimeout = await inquirer.prompt(sessionTimeoutQuestion);
    sessionTimeout = sessionTimeout[inputs[11].key];

    let coppa = await inquirer.prompt(coppaQuestion);
    coppa = coppa[inputs[4].key];
    if (coppa) {
      print.info('');
      print.info('You must obtain any required verifiable parental consent under COPPA.');
      print.info('');
    }

    print.info('');
    print.info('First create an intent for your new chatbot. An intent represents an action that the user wants to perform.');
    print.info('');

    let continueAddingIntents = true;
    const addAnotherIntentQuestion = {
      type: inputs[23].type,
      name: inputs[23].key,
      message: inputs[23].question,
      default: inputs[23].default,
    };
    const intents = [];
    while (continueAddingIntents) {
      intents.push(await addIntent(context, botName, resourceName, serviceMetadata, intents, parameters));
      continueAddingIntents = await inquirer.prompt(addAnotherIntentQuestion);
      continueAddingIntents = continueAddingIntents[inputs[23].key];
    }

    answers = {
      resourceName,
      botName,
      intents,
      outputVoice,
      sessionTimeout,
      coppa,
    };
  } else {
    context.print.error('Valid option not chosen');
  }

  if (parameters) {
    if (answers.intentName) {
      if (deleteIntentConfirmed) {
        parameters.intents = parameters.intents.filter(intent => intent.intentName !== answers.intentName);
      } else {
        parameters.intents.forEach(intent => {
          if (intent.intentName === answers.intentName) {
            if (answers.utterances) {
              intent.utterances = intent.utterances.concat(answers.utterances);
            }
            if (answers.slots) {
              intent.slots = intent.slots.concat(answers.slots);
            }
            if (answers.newSlotTypes) {
              if (intent.newSlotTypes) {
                intent.newSlotTypes = intent.newSlotTypes.concat(answers.newSlotTypes);
              } else {
                intent.newSlotTypes = answers.newSlotTypes;
              }
            }
          }
        });
      }
    } else if (!answers.intents) {
      context.print.error('Valid option not chosen');
    } else {
      parameters.intents = parameters.intents.concat(answers.intents);
    }
    answers = parameters;
  }
  return answers;
}