examples/watson-fb-messenger/botmaster/middleware/wrapped/humanise.js
async function getHandoffFromUpdate (update) {
  if (!has(update, 'message.text')) {
    return
  }

  // having to do own request as watson-developer-cloud package is broken for
  // Tone analyzer
  const toneAnalyzerUrl = 'https://gateway.watsonplatform.net/tone-analyzer/api'

  const params = {
    // Get the text from the JSON file.
    text: update.message.text,
    tones: 'emotion',
    version: '2016-05-19'
  }

  const requestOptions = {
    url: `${toneAnalyzerUrl}/v3/tone`,
    method: 'GET',
    qs: params,
    auth: {
      username: TONE_ANALYSIS_USERNAME,
      password: TONE_ANALYSIS_PASSWORD
    },
    json: true
  }

  const tone = await request(requestOptions)

  if (has(tone, 'document_tone.tone_categories[0].tones')) {
    const sentiments = tone.document_tone.tone_categories[0].tones

    // return sentiment before
    const badSentimentThreshold = 0.7

    for (const sentiment of sentiments) {
      // not too sure about the ones other than anger for now tbh
      if (['Anger', 'Disgust', 'Fear', 'Sadness'].indexOf(sentiment.tone_name) > -1 &&
      sentiment.score > badSentimentThreshold) {
        return 'lowSentiment'
      }
    }
  }
}
async function getHandoffFromUpdate (update) {
  if (!has(update, 'message.text')) {
    return
  }

  // having to do own request as watson-developer-cloud package is broken for
  // Tone analyzer
  const toneAnalyzerUrl = 'https://gateway.watsonplatform.net/tone-analyzer/api'

  const params = {
    // Get the text from the JSON file.
    text: update.message.text,
    tones: 'emotion',
    version: '2016-05-19'
  }

  const requestOptions = {
    url: `${toneAnalyzerUrl}/v3/tone`,
    method: 'GET',
    qs: params,
    auth: {
      username: TONE_ANALYSIS_USERNAME,
      password: TONE_ANALYSIS_PASSWORD
    },
    json: true
  }

  const tone = await request(requestOptions)

  if (has(tone, 'document_tone.tone_categories[0].tones')) {
    const sentiments = tone.document_tone.tone_categories[0].tones

    // return sentiment before
    const badSentimentThreshold = 0.7

    for (const sentiment of sentiments) {
      // not too sure about the ones other than anger for now tbh
      if (['Anger', 'Disgust', 'Fear', 'Sadness'].indexOf(sentiment.tone_name) > -1 &&
      sentiment.score > badSentimentThreshold) {
        return 'lowSentiment'
      }
    }
  }
}
async function getHandoffFromUpdate ( update ) { if ( ! has ( update , 'message.text' ) ) { return } // having to do own request as watson-developer-cloud package is broken for // Tone analyzer const toneAnalyzerUrl = 'https://gateway.watsonplatform.net/tone-analyzer/api' const params = { // Get the text from the JSON file. text : update . message . text , tones : 'emotion' , version : '2016-05-19' } const requestOptions = { url : ` ${ toneAnalyzerUrl } ` , method : 'GET' , qs : params , auth : { username : TONE_ANALYSIS_USERNAME , password : TONE_ANALYSIS_PASSWORD } , json : true } const tone = await request ( requestOptions ) if ( has ( tone , 'document_tone.tone_categories[0].tones' ) ) { const sentiments = tone . document_tone . tone_categories [ 0 ] . tones // return sentiment before const badSentimentThreshold = 0.7 for ( const sentiment of sentiments ) { // not too sure about the ones other than anger for now tbh if ( [ 'Anger' , 'Disgust' , 'Fear' , 'Sadness' ] . indexOf ( sentiment . tone_name ) > - 1 && sentiment . score > badSentimentThreshold ) { return 'lowSentiment' } } } }
This is an example implementation of the getHandoffFromUpdate optional
parameter. In this example, we determine wether an update should be handed off
to a human based on the tone of a certain message
This is an example implementation of the getHandoffFromUpdate optional parameter . In this example we determine wether an update should be handed off to a human based on the tone of a certain message