# wa-chat-server-facebook

> wa-chat-server adapter for Facebook

Latest version **0.2.6** (published 2023-05-15) · ISC license · 0 weekly downloads

## Install

```sh
npm install wa-chat-server-facebook
pnpm add wa-chat-server-facebook
yarn add wa-chat-server-facebook
bun add wa-chat-server-facebook
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.6 |
| Published | 2023-05-15 |
| First published | 2022-03-14 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 41.8 KB |
| Known vulnerabilities | 0 (+24 in 2 direct dependencies) |
| Install scripts | no |
| Author | IBM AI CoE Prague |
| Maintainers | ibmjanneme, boris.podskalicky, matej.palenik |
| Keywords | Watson, Chat, Server, Facebook |

## Links

- npm: https://www.npmjs.com/package/wa-chat-server-facebook
- Repository: git@github.ibm.com:AI-CoE-Prague/wa-chat-server-facebook
- npm.io page: https://npm.io/package/wa-chat-server-facebook

## Dependencies (8)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [axios](https://npm.io/package/axios.md) ^0.26.1
- [turndown](https://npm.io/package/turndown.md) ^7.1.1
- [botbuilder](https://npm.io/package/botbuilder.md) ^4.15.0
- [typescript](https://npm.io/package/typescript.md) ^4.6.2
- [body-parser](https://npm.io/package/body-parser.md) ^1.19.2
- [html-to-text](https://npm.io/package/html-to-text.md) ^8.1.0
- [wa-chat-server](https://npm.io/package/wa-chat-server.md) ^0.15.8

## Recent versions

- 0.2.6 (latest) — 2023-05-15
- 0.2.5 — 2023-05-15
- 0.2.4 — 2022-11-09
- 0.2.3 — 2022-08-11
- 0.2.2 — 2022-05-23
- 0.2.1 — 2022-05-06
- 0.2.0 — 2022-05-04
- 0.1.13 — 2022-04-20
- 0.1.12 — 2022-04-13
- 0.1.11 — 2022-04-13
- 0.1.9 — 2022-03-23
- 0.1.8 — 2022-03-22
- 0.1.7 — 2022-03-22
- 0.1.6 — 2022-03-22
- 0.1.5 — 2022-03-21
- … 4 more at https://npm.io/package/wa-chat-server-facebook/versions

## README

# wa-chat-server-facebook

**wa-chat-server-facebook** is a **wa-chat-server** adapter for the **Facebook** messenger.

## Create Facebook Messenger App

1. Go to https://developers.facebook.com/ and log in using your credentials
1. Create new Messenger app and open it
1. From the left panel go to **Messenger / Settings**
1. In **Access Tokens** section add your Facebook page and generate **token (1)**
1. In **Webhooks** section add your callback URL <APP_URL>/adapters/facebook and add a **Verify token (2)** which can be random value (ideally UUID)
1. Add your Facebook page to this Webhook and add select **messages** and **messaging_postbacks** in the **Edit page subscriptions** dialog

## Adapter Usage

### Register The Adapter

An initialization of the **wa-chat-server** (in a chat application using
**wa-chat-server** to communicate with Watson Assistant) with a
**wa-chat-server-facebook** adapter looks like this:

```
require('dotenv').config();
import { WAChatServer, Types } from 'wa-chat-server';
import { WAChatServerFacebookAdapter } from 'wa-chat-server-facebook';
const server: WAChatServer = new WAChatServer((process.env as any) as Types.IWAChatServerConfig);
server.addAdapter('facebook', WAChatServerFacebookAdapter);
server.serve();
```

### Configure The Adapter

We must set the following environment variables (e. g. in `.env`):

| Variable                                   | Obligatory | Meaning                                                                                                                                                                                                                                                                                                           |
| ------------------------------------------ | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `adapter__facebook__facebookApiUrl`        | Y          | `https://graph.facebook.com/v13.0/me/messages`                                                                                                                                                                                                                                                                    |
| `adapter__facebook__facebookToken`         | Y          | Token (1)                                                                                                                                                                                                                                                                                                         |
| `adapter__facebook__chatServerVerifyToken` | Y          | Verify token (2)                                                                                                                                                                                                                                                                                                  |
| `adapter__facebook__integrationSource`     | N          | Sets value of context variable `integration-source`. Default value is `facebook`                                                                                                                                                                                                                                  |
| `adapter__facebook__replaceHtmlAnchor`     | N          | If set to `true` the adapter will replace html tags to plain text value                                                                                                                                                                                                                                           |
| `adapter__facebook__useSenderActions`      | N          | If set to `true` the adapter will use `mark_seen` and `typing_on` sender actions when message is received (might generate additional API calls); If set to any other string (including `false`), it will be used as a context variable name and checked in incoming requests (refer to table below for examples); |

#### Use Sender Actions Example Table

| Configuration          | Context                                | Will use sender actions? |
| ---------------------- | -------------------------------------- | ------------------------ |
| `undefined \| null`    | any                                    | N                        |
| `true`                 | any                                    | Y                        |
| `disableSenderActions` | `{ }`                                  | Y                        |
| `disableSenderActions` | `{ disableSenderActions: null }`       | Y                        |
| `disableSenderActions` | `{ disableSenderActions: false }`      | Y                        |
| `disableSenderActions` | `{ disableSenderActions: true }`       | N                        |
| `disableSenderActions` | `{ disableSenderActions: 'disabled' }` | N                        |

### Watson Assistant Response Support

1. `text` - Text reply (interpreted as Text message)
1. `suggestion` - Disambiguation (interpreted as Postback buttons)
1. `option` - Options (interpreted as Postback buttons)

## Local Development

### HTTP Tunnel Setup

For local development we need to set up a secure tunnel localhost and Facebook.
We can use [ngrok](https://ngrok.com/) for this purpose:

```
ngrok http 3023
```

opens a tunel and gives us a public HTTPS URL to be used by Facebook (without
an ngrok account the lifetime of the tunnel will be 2 hours). We must then
use the [Meta for Developers](https://developers.facebook.com/) to set the Callback URL:

We may inspect the communication tunneled by **ngrok** to localhost on
[http://localhost:4040](http://localhost:4040).

### Transpilation

Run

```
npm run dev
```

### Linking The Library to The Chat Project

Run

```
npm link
```

from the **wa-chat-server-facebook** root folder.

and then

```
npm link wa-chat-server-facebook
```

from the root of your chat application.

---
_Source: https://npm.io/package/wa-chat-server-facebook · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
