@@ -21,33 +21,39 @@ A TypeScript server agnostic Whatsapp's Official API framework.
21
21
22
22
## Set up
23
23
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 ) .
27
27
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:
29
36
30
37
``` sh
31
38
npm install whatsapp-api-js
32
39
```
33
40
34
- Now you can write code like this:
41
+ Which will let you write code like this:
35
42
36
43
``` js
37
44
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" ;
40
46
41
47
// Kind reminder to not hardcode your token and secret
42
48
const TOKEN = " YOUR_TOKEN" ;
43
49
const APP_SECRET = " YOUR_SECRET" ;
44
50
51
+ /** @type WhatsAppAPI<number> */
45
52
const Whatsapp = new WhatsAppAPI ({ token: TOKEN , appSecret: APP_SECRET });
46
53
47
54
// Assuming post is called on a POST request to your server
48
55
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
51
57
return await Whatsapp .post (
52
58
JSON .parse (e .data ),
53
59
e .data ,
@@ -62,47 +68,32 @@ Whatsapp.on.message = async ({ phoneID, from, message, name, reply }) => {
62
68
)} `
63
69
);
64
70
65
- let promise ;
71
+ let response ;
66
72
67
73
if (message .type === " text" ) {
68
- promise = reply (
74
+ response = await reply (
69
75
new Text (` *${ name} * said:\n\n ${ message .text .body } ` ),
70
76
true
71
77
);
72
78
}
73
79
74
80
if (message .type === " image" ) {
75
- promise = reply (
81
+ response = await reply (
76
82
new Image (message .image .id , true , ` Nice photo, ${ name} ` )
77
83
);
78
84
}
79
85
80
86
if (message .type === " document" ) {
81
- promise = reply (
87
+ response = await reply (
82
88
new Document (message .document .id , true , undefined , " Our document" )
83
89
);
84
90
}
85
91
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
-
103
92
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."
106
97
);
107
98
108
99
Whatsapp .markAsRead (phoneID, message .id );
@@ -115,12 +106,14 @@ Whatsapp.on.sent = ({ phoneID, to, message }) => {
115
106
};
116
107
` ` `
117
108
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.
119
113
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.
122
115
123
- The package also has a GET wizard for the webhook authentication:
116
+ The package also includes a GET handler for the webhook authentication:
124
117
125
118
` ` ` js
126
119
import { WhatsAppAPI } from " whatsapp-api-js" ;
@@ -136,28 +129,29 @@ const Whatsapp = new WhatsAppAPI({
136
129
});
137
130
138
131
// 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
140
132
function get (e ) {
133
+ // Too long!? Read https://whatsappapijs.web.app/modules/middleware.html
141
134
return Whatsapp .get (e .query );
142
135
}
143
136
` ` `
144
137
145
- Once you are done, click the administrate button, and subscribe to the messages event.
146
-
147
138
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
149
141
[Environments.md file](https://github.com/Secreto31126/whatsapp-api-js/blob/main/ENVIRONMENTS.md).
150
142
151
143
## Examples and Tutorials
152
144
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.
155
147
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/).
157
150
158
151
## Types
159
152
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:
161
155
162
156
` ` ` ts
163
157
import { GetParams , PostData } from " whatsapp-api-js/types" ;
@@ -166,12 +160,15 @@ import { OnMessage, OnSent, OnStatus } from "whatsapp-api-js/emitters";
166
160
167
161
## Changelog
168
162
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).
170
165
171
166
## Documentation
172
167
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/).
175
172
176
173
## Contributors
177
174
@@ -197,15 +194,17 @@ and previous versions are available in [secreto31126.github.io/whatsapp-api-js](
197
194
198
195
## Contributions
199
196
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
201
199
[CONTRIBUTING.md file](https://github.com/Secreto31126/whatsapp-api-js/blob/main/CONTRIBUTING.md).
202
200
203
201
## Breaking changes
204
202
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).
206
205
207
206
## Beta releases
208
207
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.
0 commit comments