Skip to content

Commit

Permalink
docs: added docs
Browse files Browse the repository at this point in the history
  • Loading branch information
j05u3 committed Aug 21, 2023
1 parent 67d31bf commit ae33112
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
61 changes: 41 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@

> A set of Node.js and Express.js functions for sending/receiving messages using the [Whatsapp Cloud API](https://developers.facebook.com/docs/whatsapp/cloud-api/). Contains typescript declarations.
## Features:

All features in [here](https://github.com/tawn33y/whatsapp-cloud-api/tree/v0.2.6), plus:

✅ Added `to_phone_number` so you can identify which phone number was the one receiving the message.

✅ Added support for type `button` in incoming messages. Which is generated when the user "replies" from a template button.

✅ Added a logging callback for each message sent so you can log each sent message easily.

✅ Added a way to listen for status changes in messages. This allows to listen for 'delivered', 'failed', 'read',... statuses on the sent messages.

✅ Changed the architecture so we can use the webhook and the sender separately.

✅ Made the webhook able to run on serverless environments (like Google Cloud Functions).[^1]

[^1]: This is because on the webhook now we wait for callbacks to finish before the response is sent (`sendStatus`), this was done because on serverless environments code is not guaranteed to be kept alive after the response is sent.

## Install

```bash
Expand All @@ -18,33 +36,36 @@ npm install whatsapp-cloud-api-express

## Usage

```ts
import { myPackage } from 'whatsapp-cloud-api-express';
You can use this library only to send Whatsapp messages or only to receive Whatsapp messages or you can do both.

myPackage('hello');
//=> 'hello from my package'
```

## API

### myPackage(input, options?)

#### input

Type: `string`
### Receiving messages

Lorem ipsum.
The webhook part of the API is implemented as an express router. The webhook is the part that allows you to listen for new messages incoming to your bot. You can use it like this:

#### options
```ts
app.use(
'/webhook/whatsapp',
getWebhookRouter({
// fill your own values here:
webhookVerifyToken: process.env.WHATSAPP_WEBHOOK_VERIFICATION_TOKEN ?? '',
onNewMessage,
})
);
```

Type: `object`
### Sending messages

##### postfix
First, create a sender like this:

Type: `string`
Default: `rainbows`
```ts
const sender = createMessageSender(
// fill your own values here:
process.env.NUMBER_ID ?? '',
process.env.ACCESS_TOKEN ?? ''
);
```

Lorem ipsum.
To send a message you can check [this guide](https://github.com/tawn33y/whatsapp-cloud-api/blob/v0.2.6/API.md#api-reference) (omit `createBot`, `startExpressServer` and `on` as those were removed here). You can find some examples in [there too](https://github.com/tawn33y/whatsapp-cloud-api/tree/v0.2.6).

[build-img]: https://github.com/j05u3/whatsapp-cloud-api-express/actions/workflows/release.yml/badge.svg
[build-url]: https://github.com/j05u3/whatsapp-cloud-api-express/actions/workflows/release.yml
Expand Down
3 changes: 3 additions & 0 deletions src/createBot.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,8 @@ export interface MessageSender {
export type ICreateMessageSender = (
fromPhoneNumberId: string,
accessToken: string,
/**
* Optional function to log/store the request and response from the API.
*/
responseLogger?: (obj: any) => Promise<void>
) => MessageSender;
5 changes: 5 additions & 0 deletions src/expressRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ export interface WebhookRouterOptions {
onNewMessage: (message: Message) => Promise<void>;
webhookPath?: string;
onStatusChange?: (status: Status) => Promise<void>;
/**
* Log all entrant requests to console.
* Remember that the Whatsapp servers make requests to this webhook with new incoming messages and statuses.
* This option is useful for debugging.
*/
logAllEntrantRequests?: boolean;
}

Expand Down

0 comments on commit ae33112

Please sign in to comment.