Skip to content

Commit d633a2d

Browse files
authored
Merge pull request #359 from Secreto31126/docs-v4
Docs v4
2 parents 8d11552 + 1569f99 commit d633a2d

File tree

3 files changed

+54
-55
lines changed

3 files changed

+54
-55
lines changed

README.md

+51-52
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,39 @@ A TypeScript server agnostic Whatsapp's Official API framework.
2121

2222
## Set up
2323

24-
First, you need a Facebook app with the Whatsapp API activated.
25-
You can create your first app following [this steps](https://developers.facebook.com/docs/whatsapp/getting-started/signing-up).
26-
Get the API token, either a temporal or a permanent one.
24+
Before all, you will need a Meta Bussiness App with WhatsApp API activated. You
25+
can create your first app following
26+
[this steps](https://developers.facebook.com/docs/whatsapp/cloud-api/get-started).
2727

28-
In your server you can install the module using npm:
28+
- Get the API token, either a temporal or a
29+
[permanent one](https://developers.facebook.com/docs/whatsapp/business-management-api/get-started).
30+
- Get your App secret from App Settings > Basic > App Secret.
31+
- More in-depth information on how to set and retrieve this values is available
32+
at
33+
[the module documentation](https://whatsappapijs.web.app/types/types.TheBasicConstructorArguments.html)
34+
35+
You can now install the module using npm:
2936

3037
```sh
3138
npm install whatsapp-api-js
3239
```
3340

34-
Now you can write code like this:
41+
Which will let you write code like this:
3542

3643
```js
3744
import { WhatsAppAPI } from "whatsapp-api-js";
38-
import { Text, Image, Document } from "whatsapp-api-js/messages";
39-
import * as Contacts from "whatsapp-api-js/messages/contacts";
45+
import { Document, Image, Text } from "whatsapp-api-js/messages";
4046

4147
// Kind reminder to not hardcode your token and secret
4248
const TOKEN = "YOUR_TOKEN";
4349
const APP_SECRET = "YOUR_SECRET";
4450

51+
/** @type WhatsAppAPI<number> */
4552
const Whatsapp = new WhatsAppAPI({ token: TOKEN, appSecret: APP_SECRET });
4653

4754
// Assuming post is called on a POST request to your server
4855
async function post(e) {
49-
// The handlers work with any framework, as long as you pass the correct data
50-
// You can also use one of the middlewares provided in the package, keep reading
56+
// Too long? Read https://whatsappapijs.web.app/modules/middleware.html
5157
return await Whatsapp.post(
5258
JSON.parse(e.data),
5359
e.data,
@@ -62,47 +68,32 @@ Whatsapp.on.message = async ({ phoneID, from, message, name, reply }) => {
6268
)}`
6369
);
6470

65-
let promise;
71+
let response;
6672

6773
if (message.type === "text") {
68-
promise = reply(
74+
response = await reply(
6975
new Text(`*${name}* said:\n\n${message.text.body}`),
7076
true
7177
);
7278
}
7379

7480
if (message.type === "image") {
75-
promise = reply(
81+
response = await reply(
7682
new Image(message.image.id, true, `Nice photo, ${name}`)
7783
);
7884
}
7985

8086
if (message.type === "document") {
81-
promise = reply(
87+
response = await reply(
8288
new Document(message.document.id, true, undefined, "Our document")
8389
);
8490
}
8591

86-
if (message.type === "contacts") {
87-
promise = reply(
88-
new Contacts.Contacts(
89-
[
90-
new Contacts.Name(name, "First name", "Last name"),
91-
new Contacts.Phone(phone),
92-
new Contacts.Birthday("2022", "04", "25")
93-
],
94-
[
95-
new Contacts.Name("John", "First name", "Last name"),
96-
new Contacts.Organization("Company", "Department", "Title"),
97-
new Contacts.Url("https://www.google.com", "WORK")
98-
]
99-
)
100-
);
101-
}
102-
10392
console.log(
104-
(await promise) ??
105-
"There are more types of messages, such as locations, templates, interactive, reactions and all the other media types."
93+
response ??
94+
"There are more types of messages, such as contacts, " +
95+
"locations, templates, interactive, reactions and " +
96+
"all the other media types."
10697
);
10798

10899
Whatsapp.markAsRead(phoneID, message.id);
@@ -115,12 +106,14 @@ Whatsapp.on.sent = ({ phoneID, to, message }) => {
115106
};
116107
```
117108
118-
To receive the post requests on message, you must setup the webhook at your Facebook app.
109+
To receive the messages updates, you must set-up the webhook at your Meta app.
110+
111+
Back in the dashboard, click on WhatsApp > Settings, and write down your webhook
112+
URL. Make sure to subscribe to the messages event.
119113
120-
Back in the dashboard, click on WhatsApp > Settings, and setup the webhook URL.
121-
While setting it up, you will be asked for a Verify Token. This can be any string you want.
114+
You will also be asked for a Verify Token. This can be any string you want.
122115
123-
The package also has a GET wizard for the webhook authentication:
116+
The package also includes a GET handler for the webhook authentication:
124117
125118
```js
126119
import { WhatsAppAPI } from "whatsapp-api-js";
@@ -136,28 +129,29 @@ const Whatsapp = new WhatsAppAPI({
136129
});
137130

138131
// Assuming get is called on a GET request to your server
139-
// You can also use one of the middlewares provided in the package, keep reading
140132
function get(e) {
133+
// Too long!? Read https://whatsappapijs.web.app/modules/middleware.html
141134
return Whatsapp.get(e.query);
142135
}
143136
```
144137
145-
Once you are done, click the administrate button, and subscribe to the messages event.
146-
147138
And that's it! Now you have a functioning Whatsapp Bot connected to your server.
148-
For more information on the setup process for specific runtimes and frameworks, check out the
139+
For more information on the setup process for specific runtimes and frameworks,
140+
check out the
149141
[Environments.md file](https://github.com/Secreto31126/whatsapp-api-js/blob/main/ENVIRONMENTS.md).
150142
151143
## Examples and Tutorials
152144
153-
There are a few examples that cover how to create each type of message,
154-
and how to use the basic methods of the library.
145+
There are a few examples that cover how to create each type of message, and how
146+
to use the basic methods of the library.
155147
156-
Check them out in the [examples folder](https://github.com/Secreto31126/whatsapp-api-js/blob/main/EXAMPLES/).
148+
Check them out in the
149+
[examples folder](https://github.com/Secreto31126/whatsapp-api-js/blob/main/EXAMPLES/).
157150
158151
## Types
159152
160-
The library is fully typed. Most types are available by importing `/types` or `/emitters` :
153+
The library is fully typed. Most types are available by importing `/types` or
154+
`/emitters` files:
161155
162156
```ts
163157
import { GetParams, PostData } from "whatsapp-api-js/types";
@@ -166,12 +160,15 @@ import { OnMessage, OnSent, OnStatus } from "whatsapp-api-js/emitters";
166160
167161
## Changelog
168162
169-
To know what changed between updates, check out the [releases on Github](https://github.com/Secreto31126/whatsapp-api-js/releases).
163+
To know what changed between updates, check out the
164+
[releases on Github](https://github.com/Secreto31126/whatsapp-api-js/releases).
170165
171166
## Documentation
172167
173-
The latest release documentation is available in [whatsappapijs.web.app](https://whatsappapijs.web.app/),
174-
and previous versions are available in [secreto31126.github.io/whatsapp-api-js](https://secreto31126.github.io/whatsapp-api-js/).
168+
The latest release documentation is available at
169+
[whatsappapijs.web.app](https://whatsappapijs.web.app/), and previous versions
170+
are archived at
171+
[secreto31126.github.io/whatsapp-api-js](https://secreto31126.github.io/whatsapp-api-js/).
175172
176173
## Contributors
177174
@@ -197,15 +194,17 @@ and previous versions are available in [secreto31126.github.io/whatsapp-api-js](
197194
198195
## Contributions
199196
200-
If you have some free time and really want to improve the library or fix dumb bugs, feel free to read
197+
If you have some free time and really want to improve the library or fix dumb
198+
bugs, feel free to read
201199
[CONTRIBUTING.md file](https://github.com/Secreto31126/whatsapp-api-js/blob/main/CONTRIBUTING.md).
202200
203201
## Breaking changes
204202
205-
You can get a full list of breaking changes in the [BREAKING.md file](https://github.com/Secreto31126/whatsapp-api-js/blob/main/BREAKING.md).
203+
You can get a full list of breaking changes in the
204+
[BREAKING.md file](https://github.com/Secreto31126/whatsapp-api-js/blob/main/BREAKING.md).
206205
207206
## Beta releases
208207
209-
Install the latest beta release with `npm install whatsapp-api-js@beta`.
210-
As any beta, it is 110% likely to break. I also use this tag to test npm releases.
211-
Use it at your own risk.
208+
Install the latest beta release with `npm install whatsapp-api-js@beta`. As any
209+
beta, it is 110% likely to break. I also use this tag to test npm releases. Use
210+
it at your own risk.

SECURITY.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
| Version | Supported |
66
| ------- | ------------------ |
7-
| 3 | :white_check_mark: |
8-
| < 3 | :x: |
7+
| 4 | :white_check_mark: |
8+
| < 4 | :x: |
99

1010
## Reporting a Vulnerability
1111

src/emitters.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export type OnMessageArgs = {
107107
* @param response - The message to send as a reply
108108
* @param context - Wether to mention the current message, defaults to false
109109
* @param biz_opaque_callback_data - An arbitrary 512B string, useful for tracking
110-
* @returns WhatsAppAPI.sendMessage return value
110+
* @returns The {@link WhatsAppAPI.sendMessage} return value
111111
*/
112112
reply: (
113113
response: ClientMessage,

0 commit comments

Comments
 (0)