<commit_before># Serverless Facebook Messenger Bot

[![Build Status](https://travis-ci.org/michalsanger/serverless-facebook-messenger-bot.svg?branch=master)](https://travis-ci.org/michalsanger/serverless-facebook-messenger-bot)

Serverless backend for Facebook Messenger Bot

## Install

You need to have installed the [Serverless Framework](https://github.com/serverless/serverless) (version 0.5.2 or higher) and you're using Node.js v4.0+.


### 1. Init project
```
sls project install serverless-facebook-messenger-bot
```

### 2. Setup FB App
Follow [quickstart](https://developers.facebook.com/docs/messenger-platform/quickstart) but start with [Step 1](https://developers.facebook.com/docs/messenger-platform/quickstart#create_app_page) and [Step 3](https://developers.facebook.com/docs/messenger-platform/quickstart#get_page_access_token) to get Page Access Token before deploying backend.

### 3. Set variables
```
sls variables set -k PAGE_ACCESS_TOKEN
sls variables set -k VERIFY_TOKEN
```
`VERIFY_TOKEN` is used for subsciption verification.

### 4. Deploy backend app
Deploy all functions and endpoints
```
sls function deploy --all
sls endpoint deploy --all
```
Now you have public webhook URL.

### 5. Setup Webhook
Back to the [FB App Quickstart](https://developers.facebook.com/docs/messenger-platform/quickstart#setup_webhook) and verify your webhook.

### 6. Subscribe the App to the Page
```
curl -ik -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=<PAGE_ACCESS_TOKEN>"
```

### 7. Send messages
Go to your Facebook Page and send a message to it. The response will come from your brand new servreless backend! See screenshots in the [Quickstart](https://developers.facebook.com/docs/messenger-platform/quickstart#receive_messages)

## Running Tests
```
npm test
```

## Develop
Want to add some logic into bot's responses? Take a look in `functions/webhook/handlers/`<commit_msg>Info about running localy<commit_after># Serverless Facebook Messenger Bot

[![Build Status](https://travis-ci.org/michalsanger/serverless-facebook-messenger-bot.svg?branch=master)](https://travis-ci.org/michalsanger/serverless-facebook-messenger-bot)

Serverless backend for Facebook Messenger Bot

## Install

You need to have installed the [Serverless Framework](https://github.com/serverless/serverless) (version 0.5.2 or higher) and you're using Node.js v4.0+.


### 1. Init project
```
sls project install serverless-facebook-messenger-bot
```

### 2. Setup FB App
Follow [quickstart](https://developers.facebook.com/docs/messenger-platform/quickstart) but start with [Step 1](https://developers.facebook.com/docs/messenger-platform/quickstart#create_app_page) and [Step 3](https://developers.facebook.com/docs/messenger-platform/quickstart#get_page_access_token) to get Page Access Token before deploying backend.

### 3. Set variables
```
sls variables set -k PAGE_ACCESS_TOKEN
sls variables set -k VERIFY_TOKEN
```
`VERIFY_TOKEN` is used for subsciption verification.

### 4. Deploy backend app
Deploy all functions and endpoints
```
sls function deploy --all
sls endpoint deploy --all
```
Now you have public webhook URL.

### 5. Setup Webhook
Back to the [FB App Quickstart](https://developers.facebook.com/docs/messenger-platform/quickstart#setup_webhook) and verify your webhook.

### 6. Subscribe the App to the Page
```
curl -ik -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=<PAGE_ACCESS_TOKEN>"
```

### 7. Send messages
Go to your Facebook Page and send a message to it. The response will come from your brand new servreless backend! See screenshots in the [Quickstart](https://developers.facebook.com/docs/messenger-platform/quickstart#receive_messages)

## Running Tests
```
npm test
```

## Develop
Want to add some logic into bot's responses? Take a look in `functions/webhook/handlers/`

## Deploy
```
sls function deploy --all
```

## Running localy
```
npm run offline
```
and you can send POST requests to [http://localhost:3000/webhook](http://localhost:3000/webhook)

To get an idea about the POST body, enable logging:
```
sls variables set -k LOG_WEBHOOK_MESSAGES -v true
```

deploy, send few messages via Messenger and see the logs:
```
sls function logs webhook
```
{'fraction_non_alphanumeric': 0.11961384506710619, 'fraction_numerical': 0.009418412997409937, 'mean_word_length': 5.094691535150646, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 18, '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': '2508482', 'n_tokens_mistral': 1371, 'n_tokens_neox': 1268, 'n_words': 399}