initLiveChatSession()

in lex-web-ui/src/store/actions.js [788:879]


  initLiveChatSession(context) {
    console.info('initLiveChat');
    console.info('config connect', context.state.config.connect);
    if (!context.state.config.ui.enableLiveChat) {
      console.error('error in initLiveChatSession() enableLiveChat is not true in config');
      return Promise.reject(new Error('error in initLiveChatSession() enableLiveChat is not true in config'));
    }
    if (!context.state.config.connect.apiGatewayEndpoint) {
      console.error('error in initLiveChatSession() apiGatewayEndpoint is not set in config');
      return Promise.reject(new Error('error in initLiveChatSession() apiGatewayEndpoint is not set in config'));
    }
    if (!context.state.config.connect.contactFlowId) {
      console.error('error in initLiveChatSession() contactFlowId is not set in config');
      return Promise.reject(new Error('error in initLiveChatSession() contactFlowId is not set in config'));
    }
    if (!context.state.config.connect.instanceId) {
      console.error('error in initLiveChatSession() instanceId is not set in config');
      return Promise.reject(new Error('error in initLiveChatSession() instanceId is not set in config'));
    }

    context.commit('setLiveChatStatus', liveChatStatus.INITIALIZING);

    const initiateChatRequest = {
      ParticipantDetails: {
        DisplayName: context.getters.liveChatUserName(),
      },
      ContactFlowId: context.state.config.connect.contactFlowId,
      InstanceId: context.state.config.connect.instanceId,
    };

    const uri = new URL(context.state.config.connect.apiGatewayEndpoint);
    const endpoint = new AWS.Endpoint(uri.hostname);
    const req = new AWS.HttpRequest(endpoint, context.state.config.region);
    req.method = 'POST';
    req.path = uri.pathname;
    req.headers['Content-Type'] = 'application/json';
    req.body = JSON.stringify(initiateChatRequest);
    req.headers.Host = endpoint.host;
    req.headers['Content-Length'] = Buffer.byteLength(req.body);

    const signer = new AWS.Signers.V4(req, 'execute-api');
    signer.addAuthorization(awsCredentials, new Date());

    const reqInit = {
      method: 'POST',
      mode: 'cors',
      headers: req.headers,
      body: req.body,
    };

    return fetch(
      context.state.config.connect.apiGatewayEndpoint,
      reqInit)
    .then(response => response.json())
    .then(json => json.data)
    .then((result) => {
      console.info('Live Chat Config Success:', result);
      context.commit('setLiveChatStatus', liveChatStatus.CONNECTING);
      function waitMessage(context, type, message) {
        context.commit('pushLiveChatMessage', {
          type,
          text: message,
        });
      };
      if (context.state.config.connect.waitingForAgentMessageIntervalSeconds > 0) {
        const intervalID = setInterval(waitMessage,
          1000 * context.state.config.connect.waitingForAgentMessageIntervalSeconds,
          context,
          'bot',
          context.state.config.connect.waitingForAgentMessage);
        console.info(`interval now set: ${intervalID}`);
        context.commit('setLiveChatIntervalId', intervalID);
      }
      liveChatSession = createLiveChatSession(result);
      console.info('Live Chat Session Created:', liveChatSession);
      initLiveChatHandlers(context, liveChatSession);
      console.info('Live Chat Handlers initialised:');
      return connectLiveChatSession(liveChatSession);
    })
    .then((response) => {
      console.info('live Chat session connection response', response);
      console.info('Live Chat Session CONNECTED:', liveChatSession);
      context.commit('setLiveChatStatus', liveChatStatus.ESTABLISHED);
      // context.commit('setLiveChatbotSession', liveChatSession);
      return Promise.resolve();
    })
    .catch((error) => {
      console.error("Error esablishing live chat");
      context.commit('setLiveChatStatus', liveChatStatus.ENDED);
      return Promise.resolve();
    });
  },