<commit_before>const logger = require('../../config/logger');
const messenger = require('../messenger');


/**
 *  Handle "Message Received" callbacks. We receive these callbacks if we
 *  subscribed to "messages" events when we created our Facebook App. For more
 *  information about the format of the `data`, check out the documentation at
 *  https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-received
 **/

function messageReceived(data) {
    logger.info('Processing "Message Received" callback');
    logger.debug(data);

    const sender = data.sender.id;
    const { text, attachments } = data.message;
}


module.exports = messageReceived;<commit_msg>implement message response pipeline and handle thank you messages<commit_after>const logger = require('../../config/logger');
const messenger = require('../messenger');


/**
 *  Handle "Message Received" callbacks. We receive these callbacks if we
 *  subscribed to "messages" events when we created our Facebook App. For more
 *  information about the format of the `data`, check out the documentation at
 *  https://developers.facebook.com/docs/messenger-platform/webhook-reference/message-received
 **/

function messageReceived(data) {
    logger.info('Processing "Message Received" callback');
    logger.debug(data);

    const sender = data.sender.id;
    const { text, attachments } = data.message;

    return messageResponsePipeline(sender, data.message);
}


/**
 *  Run the received message through a series of message handler functions
 *  until one of them responds to the message.
 **/

function messageResponsePipeline(recipient, message) {
    const messageHandlers = [
        handleThankYouMessage
    ];

    let index = 0;
    const next = _ => {
        if (++index < messageHandlers.length) {
            messageHandlers(recipient, message, next);
        }
    };

    messageHandlers[0](recipient, message, next);
}


/** Respond to messages saying "thank you" (and several variations). **/

function handleThankYouMessage(recipient, message, next) {
    const pattern = /(thank you|thanks|tnx|thnx|salamat)/g;
    if (pattern.test(message.text)) {
        return messenger.sendTextMessage(recipient, 'Sure, no problem! :)');
    }
    next();
}


module.exports = messageReceived;
{'fraction_non_alphanumeric': 0.11739130434782609, 'fraction_numerical': 0.0008695652173913044, 'mean_word_length': 3.7639751552795033, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1762723', 'n_tokens_mistral': 646, 'n_tokens_neox': 612, 'n_words': 188}