<commit_before>// FacebookMessengerBot
// ====================
//
// A class for setting-up and launching a webserver to comunicate with
// Facebook Messenger apps.
//

import express from 'express';
import bodyParser from 'body-parser';

// Facebook helpers: [facebookWebhookSetup.js](./facebookWebhookSetup.html)
// and [facebookGraphHelpers.js](./facebookGraphHelpers.html)
import { setupGetWebhook, setupPostWebhook } from './facebookWebhookSetup';
import { pageSubscribe, sendTextMessage, userInfo } from './facebookGraphHelpers';

// default values can be setup using environment vars
const PORT = process.env.PORT;
const FB_CALLBACK_PATH = process.env.FB_CALLBACK_PATH;
const FB_VERIFY_TOKEN = process.env.FB_VERIFY_TOKEN;
const FB_PAGE_ACCESS_TOKEN = process.env.FB_PAGE_ACCESS_TOKEN;

// The main class, this represents a facebook messenger chat bot/server
class FacebookMessengerBot {
    // ## Constructor
    // ### Parameters
    // - **options** - _Object_ - Configuration object.
    //   - **port** - _number_ - The port that the server will listen to.
    //   - **callbackPath** - _string_ - The endpoint path of the setup [Callback URL][fbwebhook]
    //   - **verifyToken** - _string_ - The [Verify Token][fbwebhook]
    //   - **pageTokens** - _Array_ - A list of page tokens to subscribe to
    //   - **listeners** - _Object_ - An object containing handler functions for
    // specific "[messaging][fbmessageformat]" updates or just a generic
    // ```onUpdate``` handler. (see the
    // [implementation](./facebookWebhookSetup.html#setuppostwebhook-options-)
    // for details):
    //     - onUpdate
    //     - onMessage
    //     - onAuthentication
    //     - onDelivery
    //     - onPostback
    //
    // [expressjs]: http://expressjs.com/
    // [fbwebhook]: https://developers.facebook.com/docs/messenger-platform/implementation#setup_webhook
    // [fbmessageformat]: https://developers.facebook.com/docs/messenger-platform/implementation#receive_message
    constructor(options = {}) {
        const {
            port = PORT,
            callbackPath = FB_CALLBACK_PATH,
            verifyToken = FB_VERIFY_TOKEN,
            pageTokens = [FB_PAGE_ACCESS_TOKEN],
            listeners = {}
        } = options;
        this.pageTokens = pageTokens;
        const app = express();
        this.launchPromise = new Promise(resolve => {
            app.use(bodyParser.json());
            app.get(callbackPath, setupGetWebhook(verifyToken));
            app.post(callbackPath, setupPostWebhook(listeners));
            app.listen(port, () => {
                Promise.all(pageTokens.map(token => pageSubscribe(token)))
                .then(() => resolve(true));
            });
        });
    }

    sendMessage(message) {
        // TODO test if message is atext message or something else
        return sendTextMessage(message, FB_PAGE_ACCESS_TOKEN);
    }

    getUserInfo(userId, pageToken = this.pageTokens[0]) {
        return userInfo(userId, pageToken);<commit_msg>removed TODO to create a issue on github<commit_after>// FacebookMessengerBot
// ====================
//
// A class for setting-up and launching a webserver to comunicate with
// Facebook Messenger apps.
//

import express from 'express';
import bodyParser from 'body-parser';

// Facebook helpers: [facebookWebhookSetup.js](./facebookWebhookSetup.html)
// and [facebookGraphHelpers.js](./facebookGraphHelpers.html)
import { setupGetWebhook, setupPostWebhook } from './facebookWebhookSetup';
import { pageSubscribe, sendTextMessage, userInfo } from './facebookGraphHelpers';

// default values can be setup using environment vars
const PORT = process.env.PORT;
const FB_CALLBACK_PATH = process.env.FB_CALLBACK_PATH;
const FB_VERIFY_TOKEN = process.env.FB_VERIFY_TOKEN;
const FB_PAGE_ACCESS_TOKEN = process.env.FB_PAGE_ACCESS_TOKEN;

// The main class, this represents a facebook messenger chat bot/server
class FacebookMessengerBot {
    // ## Constructor
    // ### Parameters
    // - **options** - _Object_ - Configuration object.
    //   - **port** - _number_ - The port that the server will listen to.
    //   - **callbackPath** - _string_ - The endpoint path of the setup [Callback URL][fbwebhook]
    //   - **verifyToken** - _string_ - The [Verify Token][fbwebhook]
    //   - **pageTokens** - _Array_ - A list of page tokens to subscribe to
    //   - **listeners** - _Object_ - An object containing handler functions for
    // specific "[messaging][fbmessageformat]" updates or just a generic
    // ```onUpdate``` handler. (see the
    // [implementation](./facebookWebhookSetup.html#setuppostwebhook-options-)
    // for details):
    //     - onUpdate
    //     - onMessage
    //     - onAuthentication
    //     - onDelivery
    //     - onPostback
    //
    // [expressjs]: http://expressjs.com/
    // [fbwebhook]: https://developers.facebook.com/docs/messenger-platform/implementation#setup_webhook
    // [fbmessageformat]: https://developers.facebook.com/docs/messenger-platform/implementation#receive_message
    constructor(options = {}) {
        const {
            port = PORT,
            callbackPath = FB_CALLBACK_PATH,
            verifyToken = FB_VERIFY_TOKEN,
            pageTokens = [FB_PAGE_ACCESS_TOKEN],
            listeners = {}
        } = options;
        this.pageTokens = pageTokens;
        const app = express();
        this.launchPromise = new Promise(resolve => {
            app.use(bodyParser.json());
            app.get(callbackPath, setupGetWebhook(verifyToken));
            app.post(callbackPath, setupPostWebhook(listeners));
            app.listen(port, () => {
                Promise.all(pageTokens.map(token => pageSubscribe(token)))
                .then(() => resolve(true));
            });
        });
    }

    sendMessage(message) {
        return sendTextMessage(message, FB_PAGE_ACCESS_TOKEN);
    }

    getUserInfo(userId, pageToken = this.pageTokens[0]) {
        return userInfo(userId, pageToken);
{'fraction_non_alphanumeric': 0.12750333778371162, 'fraction_numerical': 0.0003337783711615487, 'mean_word_length': 2.757366771159875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '876435', 'n_tokens_mistral': 1695, 'n_tokens_neox': 1684, 'n_words': 388}