src/main.js
/**
   * sendMessage - build the message that will be passed to the 
   * Assistant service.
   */
build the message that will be passed to the
Assistant service.
build the message that will be passed to the Assistant service .
sendMessage(text) {
    var { context, conversation } = this.state;
    this.setState({
      context: context
    });
    fetch('/api/message', {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        context: context,
        message: text
      })
    }).then(response => {
      if (response.ok) {
        return response.json();
      } else {
        throw response;
      }
    })
      .then(json => {
        const result = json.result.output.generic[0];
        const context = json.result.context;
        if ('webhook_result_1' in context) {
          console.log('Data from Discovery ---- ');
          console.log(JSON.stringify(context, null, 2));
          var passages = context.webhook_result_1.passages;
          passages = utils.formatData(passages);
          messageCounter += 1;
          conversation.push(
            { id: messageCounter,
              text: 'Here are some excerpts from the Users Guide:',
              owner: 'watson'});
          passages.results.forEach(function(result) {
            messageCounter += 1;
            conversation.push(
              { id: messageCounter,
                text: result.text,
                owner: 'watson-cont'});
          });
        } else if (result.response_type === 'text') {
          messageCounter += 1;
          conversation.push({
            id: messageCounter,
            text: result.text,
            owner: 'watson'
          });
        }
        this.setState({
          conversation: conversation,
          context: json.context,
          error: null,
          userInput: ''
        });
        scrollToMain();
      })
      .catch(response => {
        this.setState({
          error: 'Error in assistant'
        });
        console.error(response);
      });
  }
sendMessage ( text ) { var { context , conversation } = this . state ; this . setState ( { context : context } ) ; fetch ( '/api/message' , { method : 'POST' , headers : { 'Accept' : 'application/json' , 'Content-Type' : 'application/json' } , body : JSON . stringify ( { context : context , message : text } ) } ) . then ( response => { if ( response . ok ) { return response . json ( ) ; } else { throw response ; } } ) . then ( json => { const result = json . result . output . generic [ 0 ] ; const context = json . result . context ; if ( 'webhook_result_1' in context ) { console . log ( 'Data from Discovery ---- ' ) ; console . log ( JSON . stringify ( context , null , 2 ) ) ; var passages = context . webhook_result_1 . passages ; passages = utils . formatData ( passages ) ; messageCounter += 1 ; conversation . push ( { id : messageCounter , text : 'Here are some excerpts from the Users Guide:' , owner : 'watson' } ) ; passages . results . forEach ( function ( result ) { messageCounter += 1 ; conversation . push ( { id : messageCounter , text : result . text , owner : 'watson-cont' } ) ; } ) ; } else if ( result . response_type === 'text' ) { messageCounter += 1 ; conversation . push ( { id : messageCounter , text : result . text , owner : 'watson' } ) ; } this . setState ( { conversation : conversation , context : json . context , error : null , userInput : '' } ) ; scrollToMain ( ) ; } ) . catch ( response => { this . setState ( { error : 'Error in assistant' } ) ; console . error ( response ) ; } ) ; }
sendMessage - build the message that will be passed to the
 Assistant service.
sendMessage - build the message that will be passed to the Assistant service .
// send request // un-comment to show results in browser console // console.log('+++ ASSISTANT RESULTS +++'); // console.log(JSON.stringify(json, null, 2)); // console.log('CONTEXT: ' + JSON.stringify(context, null, 2)); // returned text from assistant will either be a pre-canned  // dialog response, or Discovery data // add a header to our message // add to message list // normal dialog response from Assistant // add to message list // eslint-disable-next-line no-console