-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.ts
53 lines (50 loc) · 1.68 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { SetUpWhatsAppAPI, WhatsApp } from "./src";
import { WSResponseErrorCode } from "./src/error";
import {
BodyComponent,
UrlButtonComponent,
} from "./src/templateMessages/components";
import { TextParam } from "./src/templateMessages/params";
SetUpWhatsAppAPI({
businessPhoneNumberId: "<your business phone number id>",
graphApiToken: "<your graph api token>",
});
(async () => {
const response = await WhatsApp().SendTemplateMessage({
languageCode: "en_US",
name: "verification_code",
phoneNumber: "<your phone number... or someone else's>",
bodyComponent: new BodyComponent([new TextParam("123456")]),
buttonComponents: [new UrlButtonComponent("123456", 0)],
});
response.match({
Ok(response) {
console.log("The message template was sent successfully");
console.log("Response:\n", JSON.stringify(response, undefined, 2));
},
Err(error) {
error.match({
FetchError(value) {
console.log("An error occurred while communicating with Meta", value);
},
ParseError(value) {
console.log("The response sent by Meta was not a valid JSON", value);
},
ResponseError(value) {
console.log("Meta sent an error response", value);
switch (value.code) {
case WSResponseErrorCode.MetaChoseNotToDeliver:
console.log("Meta didn't like your message");
break;
case WSResponseErrorCode.RateLimitIssues:
console.log("You can no longer send more messages for today");
break;
default:
console.log("Some other error");
break;
}
},
});
},
});
})();