updateUserTone processes the Tone Analyzer payload to pull out the emotion,
    writing and social tones, and identify the meaningful tones (i.e.,
    those tones that meet the specified thresholds).
    The conversationPayload json object is updated to include these tones.
    @param conversationPayload json object returned by the Watson Conversation
    Service
    @param toneAnalyzerPayload json object returned by the Watson Tone Analyzer
    Service
    @returns conversationPayload where the user object has been updated with tone
    information from the toneAnalyzerPayload

def updateUserTone(conversationPayload, toneAnalyzerPayload, maintainHistory):
    """
    updateUserTone processes the Tone Analyzer payload to pull out the emotion,
    writing and social tones, and identify the meaningful tones (i.e.,
    those tones that meet the specified thresholds).
    The conversationPayload json object is updated to include these tones.
    @param conversationPayload json object returned by the Watson Conversation
    Service
    @param toneAnalyzerPayload json object returned by the Watson Tone Analyzer
    Service
    @returns conversationPayload where the user object has been updated with tone
    information from the toneAnalyzerPayload
    """
    emotionTone = None
    writingTone = None
    socialTone = None

    # if there is no context in a
    if 'context' not in conversationPayload:
        conversationPayload['context'] = {}

    if 'user' not in conversationPayload['context']:
        conversationPayload['context'] = initUser()

    # For convenience sake, define a variable for the user object
    user = conversationPayload['context']['user']

    # Extract the tones - emotion, writing and social
    if toneAnalyzerPayload and toneAnalyzerPayload['document_tone']:
        for toneCategory in toneAnalyzerPayload['document_tone']['tone_categories']:
            if toneCategory['category_id'] == EMOTION_TONE_LABEL:
                emotionTone = toneCategory
            if toneCategory['category_id'] == LANGUAGE_TONE_LABEL:
                writingTone = toneCategory
            if toneCategory['category_id'] == SOCIAL_TONE_LABEL:
                socialTone = toneCategory

        updateEmotionTone(user, emotionTone, maintainHistory)
        updateWritingTone(user, writingTone, maintainHistory)
        updateSocialTone(user, socialTone, maintainHistory)

    conversationPayload['context']['user'] = user

    return conversationPayload