<commit_before>'use strict'

const facebook = require('./facebook.js'),
      senderActions = require('./webhook/sender_actions.js')

exports.sendMessage = function(recipient, message, callback) {
  senderActions.sendTypingOn(recipient, message, () => {
    if (message.text) {
      sendTextMessage(recipient, message, callback)
    } else if (message.attachment) {
      sendAttachmentMessage(recipient, message, callback)
    }
  })
}

function sendTextMessage(recipient, message, callback) {
  getMessageText(recipient, message, (text) => {
    let body = {
      recipient: recipient,
      message: {
        text: text
      }
    }
    facebook.sendMessage(body, callback)
  })
}

function getMessageText(recipient, message, callback) {
  if (message.required_user_fields) {
    facebook.getUserInfo(recipient.id, message.required_user_fields, (user) => {
      for (let user_field of message.required_user_fields) {
        let placeholder = '##' + user_field + '##'
        let replace = new RegExp(placeholder, 'g');
        message.text = message.text.replace(replace, user[user_field])
        callback(message.text)
      }
    })
  } else {
    callback(message.text)
  }
}

function sendAttachmentMessage(recipient, message, callback) {
  let body = {
    recipient: recipient,
    message: {
      attachment: message.attachment
    }
  }
  facebook.sendMessage(body, callback)
}<commit_msg>do not show undefined<commit_after>'use strict'

const facebook = require('./facebook.js'),
      senderActions = require('./webhook/sender_actions.js')

exports.sendMessage = function(recipient, message, callback) {
  senderActions.sendTypingOn(recipient, message, () => {
    if (message.text) {
      sendTextMessage(recipient, message, callback)
    } else if (message.attachment) {
      sendAttachmentMessage(recipient, message, callback)
    }
  })
}

function sendTextMessage(recipient, message, callback) {
  getMessageText(recipient, message, (text) => {
    let body = {
      recipient: recipient,
      message: {
        text: text
      }
    }
    facebook.sendMessage(body, callback)
  })
}

function getMessageText(recipient, message, callback) {
  if (message.required_user_fields) {
    facebook.getUserInfo(recipient.id, message.required_user_fields, (user) => {
      for (let user_field of message.required_user_fields) {
        console.log(`replacing ${user_field} with ${user[user_field]}`)
        let placeholder = ' ##' + user_field + '##'
        let replace = new RegExp(placeholder, 'g');
        let replaceText = user[user_field] !== undefined ? ` ${user[user_field]}` : ''
        message.text = message.text.replace(replace, replaceText)
        callback(message.text)
      }
    })
  } else {
    callback(message.text)
  }
}

function sendAttachmentMessage(recipient, message, callback) {
  let body = {
    recipient: recipient,
    message: {
      attachment: message.attachment
    }
  }
  facebook.sendMessage(body, callback)
}
{'fraction_non_alphanumeric': 0.12554548506210136, 'fraction_numerical': 0.0, 'mean_word_length': 3.414814814814815, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7004136', 'n_tokens_mistral': 904, 'n_tokens_neox': 883, 'n_words': 174}