@rkitwork/whatsapp-ramukjar-cloud-sdk 1.0.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/client.d.ts +10 -0
  2. package/dist/client.js +13 -0
  3. package/dist/helpers.d.ts +39 -0
  4. package/dist/helpers.js +41 -0
  5. package/dist/index.d.ts +4 -6
  6. package/dist/index.js +4 -6
  7. package/dist/payloads/audio.d.ts +7 -0
  8. package/dist/payloads/document.d.ts +8 -0
  9. package/dist/payloads/document.js +2 -0
  10. package/dist/payloads/image.d.ts +8 -0
  11. package/dist/payloads/image.js +2 -0
  12. package/dist/payloads/index.d.ts +19 -0
  13. package/dist/payloads/index.js +25 -0
  14. package/dist/payloads/interactive.d.ts +13 -0
  15. package/dist/payloads/interactive.js +2 -0
  16. package/dist/payloads/location.d.ts +9 -0
  17. package/dist/payloads/location.js +2 -0
  18. package/dist/payloads/reaction.d.ts +7 -0
  19. package/dist/payloads/reaction.js +2 -0
  20. package/dist/payloads/template.d.ts +10 -0
  21. package/dist/payloads/template.js +2 -0
  22. package/dist/payloads/text.d.ts +7 -0
  23. package/dist/payloads/text.js +2 -0
  24. package/dist/payloads/video.d.ts +8 -0
  25. package/dist/payloads/video.js +2 -0
  26. package/dist/sender.d.ts +2 -0
  27. package/dist/sender.js +12 -0
  28. package/package.json +11 -12
  29. package/src/client.ts +22 -0
  30. package/src/helpers.ts +45 -0
  31. package/src/index.ts +4 -8
  32. package/src/payloads/audio.ts +4 -0
  33. package/src/payloads/document.ts +4 -0
  34. package/src/payloads/image.ts +4 -0
  35. package/src/payloads/index.ts +30 -0
  36. package/src/payloads/interactive.ts +9 -0
  37. package/src/payloads/location.ts +9 -0
  38. package/src/payloads/reaction.ts +7 -0
  39. package/src/payloads/template.ts +8 -0
  40. package/src/payloads/text.ts +4 -0
  41. package/src/payloads/video.ts +4 -0
  42. package/src/sender.ts +18 -0
  43. package/dist/core/WhatsAppClient.d.ts +0 -8
  44. package/dist/core/WhatsAppClient.js +0 -29
  45. package/dist/core/payloads.d.ts +0 -14
  46. package/dist/core/payloads.js +0 -71
  47. package/dist/core/types.d.ts +0 -109
  48. package/dist/express/webhook.d.ts +0 -3
  49. package/dist/express/webhook.js +0 -18
  50. package/dist/nest/webhook.controller.d.ts +0 -6
  51. package/dist/nest/webhook.controller.js +0 -41
  52. package/dist/nest/whatsapp.module.d.ts +0 -2
  53. package/dist/nest/whatsapp.module.js +0 -22
  54. package/dist/nest/whatsapp.service.d.ts +0 -4
  55. package/dist/nest/whatsapp.service.js +0 -23
  56. package/src/core/WhatsAppClient.ts +0 -31
  57. package/src/core/payloads.ts +0 -93
  58. package/src/core/types.ts +0 -102
  59. package/src/express/webhook.ts +0 -18
  60. package/src/nest/webhook.controller.ts +0 -16
  61. package/src/nest/whatsapp.module.ts +0 -10
  62. package/src/nest/whatsapp.service.ts +0 -14
  63. /package/dist/{core/types.js → payloads/audio.js} +0 -0
@@ -0,0 +1,10 @@
1
+ import { WhatsAppPayload } from './payloads';
2
+ export declare class WhatsAppClient {
3
+ private readonly config;
4
+ constructor(config: {
5
+ token: string;
6
+ phoneNumberId: string;
7
+ apiVersion: string;
8
+ });
9
+ sendMessage(to: string, payload: WhatsAppPayload): Promise<any>;
10
+ }
package/dist/client.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WhatsAppClient = void 0;
4
+ const sender_1 = require("./sender");
5
+ class WhatsAppClient {
6
+ constructor(config) {
7
+ this.config = config;
8
+ }
9
+ sendMessage(to, payload) {
10
+ return (0, sender_1.sendMessage)(this.config.token, this.config.phoneNumberId, this.config.apiVersion, to, payload);
11
+ }
12
+ }
13
+ exports.WhatsAppClient = WhatsAppClient;
@@ -0,0 +1,39 @@
1
+ import { TextMessage } from './payloads';
2
+ export declare const WhatsApp: {
3
+ text(body: string): TextMessage;
4
+ image(link: string, caption?: string): {
5
+ type: "image";
6
+ image: {
7
+ link: string;
8
+ caption: string | undefined;
9
+ };
10
+ };
11
+ video(link: string, caption?: string): {
12
+ type: "video";
13
+ video: {
14
+ link: string;
15
+ caption: string | undefined;
16
+ };
17
+ };
18
+ audio(link: string): {
19
+ type: "audio";
20
+ audio: {
21
+ link: string;
22
+ };
23
+ };
24
+ document(link: string, filename?: string): {
25
+ type: "document";
26
+ document: {
27
+ link: string;
28
+ filename: string | undefined;
29
+ };
30
+ };
31
+ location(lat: number, lng: number, name?: string): {
32
+ type: "location";
33
+ location: {
34
+ latitude: number;
35
+ longitude: number;
36
+ name: string | undefined;
37
+ };
38
+ };
39
+ };
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WhatsApp = void 0;
4
+ exports.WhatsApp = {
5
+ text(body) {
6
+ return {
7
+ type: 'text',
8
+ text: { body },
9
+ };
10
+ },
11
+ image(link, caption) {
12
+ return {
13
+ type: 'image',
14
+ image: { link, caption },
15
+ };
16
+ },
17
+ video(link, caption) {
18
+ return {
19
+ type: 'video',
20
+ video: { link, caption },
21
+ };
22
+ },
23
+ audio(link) {
24
+ return {
25
+ type: 'audio',
26
+ audio: { link },
27
+ };
28
+ },
29
+ document(link, filename) {
30
+ return {
31
+ type: 'document',
32
+ document: { link, filename },
33
+ };
34
+ },
35
+ location(lat, lng, name) {
36
+ return {
37
+ type: 'location',
38
+ location: { latitude: lat, longitude: lng, name },
39
+ };
40
+ },
41
+ };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- export * from './core/WhatsAppClient';
2
- export * from './core/payloads';
3
- export * from './core/types';
4
- export * from './express/webhook';
5
- export * from './nest/whatsapp.module';
6
- export * from './nest/whatsapp.service';
1
+ export * from './client';
2
+ export * from './sender';
3
+ export * from './helpers';
4
+ export * from './payloads';
package/dist/index.js CHANGED
@@ -14,9 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./core/WhatsAppClient"), exports);
18
- __exportStar(require("./core/payloads"), exports);
19
- __exportStar(require("./core/types"), exports);
20
- __exportStar(require("./express/webhook"), exports);
21
- __exportStar(require("./nest/whatsapp.module"), exports);
22
- __exportStar(require("./nest/whatsapp.service"), exports);
17
+ __exportStar(require("./client"), exports);
18
+ __exportStar(require("./sender"), exports);
19
+ __exportStar(require("./helpers"), exports);
20
+ __exportStar(require("./payloads"), exports);
@@ -0,0 +1,7 @@
1
+ export interface AudioMessage {
2
+ type: 'audio';
3
+ audio: {
4
+ link?: string;
5
+ id?: string;
6
+ };
7
+ }
@@ -0,0 +1,8 @@
1
+ export interface DocumentMessage {
2
+ type: 'document';
3
+ document: {
4
+ link?: string;
5
+ id?: string;
6
+ filename?: string;
7
+ };
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export interface ImageMessage {
2
+ type: 'image';
3
+ image: {
4
+ link?: string;
5
+ id?: string;
6
+ caption?: string;
7
+ };
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,19 @@
1
+ export * from './text';
2
+ export * from './image';
3
+ export * from './video';
4
+ export * from './audio';
5
+ export * from './document';
6
+ export * from './template';
7
+ export * from './interactive';
8
+ export * from './location';
9
+ export * from './reaction';
10
+ import { TextMessage } from './text';
11
+ import { ImageMessage } from './image';
12
+ import { VideoMessage } from './video';
13
+ import { AudioMessage } from './audio';
14
+ import { DocumentMessage } from './document';
15
+ import { TemplateMessage } from './template';
16
+ import { InteractiveMessage } from './interactive';
17
+ import { LocationMessage } from './location';
18
+ import { ReactionMessage } from './reaction';
19
+ export type WhatsAppPayload = TextMessage | ImageMessage | VideoMessage | AudioMessage | DocumentMessage | TemplateMessage | InteractiveMessage | LocationMessage | ReactionMessage;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./text"), exports);
18
+ __exportStar(require("./image"), exports);
19
+ __exportStar(require("./video"), exports);
20
+ __exportStar(require("./audio"), exports);
21
+ __exportStar(require("./document"), exports);
22
+ __exportStar(require("./template"), exports);
23
+ __exportStar(require("./interactive"), exports);
24
+ __exportStar(require("./location"), exports);
25
+ __exportStar(require("./reaction"), exports);
@@ -0,0 +1,13 @@
1
+ export interface InteractiveMessage {
2
+ type: 'interactive';
3
+ interactive: {
4
+ type: 'button' | 'list';
5
+ body: {
6
+ text: string;
7
+ };
8
+ footer?: {
9
+ text: string;
10
+ };
11
+ action: any;
12
+ };
13
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,9 @@
1
+ export interface LocationMessage {
2
+ type: 'location';
3
+ location: {
4
+ latitude: number;
5
+ longitude: number;
6
+ name?: string;
7
+ address?: string;
8
+ };
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ export interface ReactionMessage {
2
+ type: 'reaction';
3
+ reaction: {
4
+ message_id: string;
5
+ emoji: string;
6
+ };
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ export interface TemplateMessage {
2
+ type: 'template';
3
+ template: {
4
+ name: string;
5
+ language: {
6
+ code: string;
7
+ };
8
+ components?: any[];
9
+ };
10
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ export interface TextMessage {
2
+ type: 'text';
3
+ text: {
4
+ body: string;
5
+ preview_url?: boolean;
6
+ };
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export interface VideoMessage {
2
+ type: 'video';
3
+ video: {
4
+ link?: string;
5
+ id?: string;
6
+ caption?: string;
7
+ };
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import { WhatsAppPayload } from './payloads';
2
+ export declare function sendMessage(token: string, phoneNumberId: string, apiVersion: string, to: string, payload: WhatsAppPayload): Promise<any>;
package/dist/sender.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.sendMessage = sendMessage;
7
+ const axios_1 = __importDefault(require("axios"));
8
+ async function sendMessage(token, phoneNumberId, apiVersion, to, payload) {
9
+ const url = `https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`;
10
+ const response = await axios_1.default.post(url, { messaging_product: 'whatsapp', to, ...payload }, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } });
11
+ return response.data;
12
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rkitwork/whatsapp-ramukjar-cloud-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.2.0",
4
4
  "description": "A secure WhatsApp Cloud API SDK for NestJS and Express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,20 +8,19 @@
8
8
  "build": "tsc",
9
9
  "prepublishOnly": "npm run build"
10
10
  },
11
- "keywords": ["whatsapp", "sdk", "nestjs", "express", "cloud-api"],
11
+ "keywords": [
12
+ "whatsapp",
13
+ "sdk",
14
+ "nestjs",
15
+ "express",
16
+ "cloud-api"
17
+ ],
12
18
  "author": "rkitwork",
13
19
  "license": "MIT",
14
- "peerDependencies": {
15
- "@nestjs/common": ">=9.0.0",
16
- "@nestjs/core": ">=9.0.0",
17
- "express": ">=4.17.0"
18
- },
19
- "devDependencies": {
20
- "typescript": "^5.3.3",
21
- "@types/express": "^4.17.21",
22
- "@types/node": "^20.0.0"
23
- },
24
20
  "dependencies": {
25
21
  "axios": "^1.7.0"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.9.3"
26
25
  }
27
26
  }
package/src/client.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { sendMessage } from './sender';
2
+ import { WhatsAppPayload } from './payloads';
3
+
4
+ export class WhatsAppClient {
5
+ constructor(
6
+ private readonly config: {
7
+ token: string;
8
+ phoneNumberId: string;
9
+ apiVersion: string;
10
+ }
11
+ ) {}
12
+
13
+ sendMessage(to: string, payload: WhatsAppPayload) {
14
+ return sendMessage(
15
+ this.config.token,
16
+ this.config.phoneNumberId,
17
+ this.config.apiVersion,
18
+ to,
19
+ payload
20
+ );
21
+ }
22
+ }
package/src/helpers.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { TextMessage } from './payloads';
2
+
3
+ export const WhatsApp = {
4
+ text(body: string): TextMessage {
5
+ return {
6
+ type: 'text',
7
+ text: { body },
8
+ };
9
+ },
10
+
11
+ image(link: string, caption?: string) {
12
+ return {
13
+ type: 'image' as const,
14
+ image: { link, caption },
15
+ };
16
+ },
17
+
18
+ video(link: string, caption?: string) {
19
+ return {
20
+ type: 'video' as const,
21
+ video: { link, caption },
22
+ };
23
+ },
24
+
25
+ audio(link: string) {
26
+ return {
27
+ type: 'audio' as const,
28
+ audio: { link },
29
+ };
30
+ },
31
+
32
+ document(link: string, filename?: string) {
33
+ return {
34
+ type: 'document' as const,
35
+ document: { link, filename },
36
+ };
37
+ },
38
+
39
+ location(lat: number, lng: number, name?: string) {
40
+ return {
41
+ type: 'location' as const,
42
+ location: { latitude: lat, longitude: lng, name },
43
+ };
44
+ },
45
+ };
package/src/index.ts CHANGED
@@ -1,8 +1,4 @@
1
- export * from './core/WhatsAppClient';
2
- export * from './core/payloads';
3
- export * from './core/types';
4
-
5
- export * from './express/webhook';
6
-
7
- export * from './nest/whatsapp.module';
8
- export * from './nest/whatsapp.service';
1
+ export * from './client';
2
+ export * from './sender';
3
+ export * from './helpers';
4
+ export * from './payloads';
@@ -0,0 +1,4 @@
1
+ export interface AudioMessage {
2
+ type: 'audio';
3
+ audio: { link?: string; id?: string };
4
+ }
@@ -0,0 +1,4 @@
1
+ export interface DocumentMessage {
2
+ type: 'document';
3
+ document: { link?: string; id?: string; filename?: string };
4
+ }
@@ -0,0 +1,4 @@
1
+ export interface ImageMessage {
2
+ type: 'image';
3
+ image: { link?: string; id?: string; caption?: string };
4
+ }
@@ -0,0 +1,30 @@
1
+ export * from './text';
2
+ export * from './image';
3
+ export * from './video';
4
+ export * from './audio';
5
+ export * from './document';
6
+ export * from './template';
7
+ export * from './interactive';
8
+ export * from './location';
9
+ export * from './reaction';
10
+
11
+ import { TextMessage } from './text';
12
+ import { ImageMessage } from './image';
13
+ import { VideoMessage } from './video';
14
+ import { AudioMessage } from './audio';
15
+ import { DocumentMessage } from './document';
16
+ import { TemplateMessage } from './template';
17
+ import { InteractiveMessage } from './interactive';
18
+ import { LocationMessage } from './location';
19
+ import { ReactionMessage } from './reaction';
20
+
21
+ export type WhatsAppPayload =
22
+ | TextMessage
23
+ | ImageMessage
24
+ | VideoMessage
25
+ | AudioMessage
26
+ | DocumentMessage
27
+ | TemplateMessage
28
+ | InteractiveMessage
29
+ | LocationMessage
30
+ | ReactionMessage;
@@ -0,0 +1,9 @@
1
+ export interface InteractiveMessage {
2
+ type: 'interactive';
3
+ interactive: {
4
+ type: 'button' | 'list';
5
+ body: { text: string };
6
+ footer?: { text: string };
7
+ action: any;
8
+ };
9
+ }
@@ -0,0 +1,9 @@
1
+ export interface LocationMessage {
2
+ type: 'location';
3
+ location: {
4
+ latitude: number;
5
+ longitude: number;
6
+ name?: string;
7
+ address?: string;
8
+ };
9
+ }
@@ -0,0 +1,7 @@
1
+ export interface ReactionMessage {
2
+ type: 'reaction';
3
+ reaction: {
4
+ message_id: string;
5
+ emoji: string;
6
+ };
7
+ }
@@ -0,0 +1,8 @@
1
+ export interface TemplateMessage {
2
+ type: 'template';
3
+ template: {
4
+ name: string;
5
+ language: { code: string };
6
+ components?: any[];
7
+ };
8
+ }
@@ -0,0 +1,4 @@
1
+ export interface TextMessage {
2
+ type: 'text';
3
+ text: { body: string; preview_url?: boolean };
4
+ }
@@ -0,0 +1,4 @@
1
+ export interface VideoMessage {
2
+ type: 'video';
3
+ video: { link?: string; id?: string; caption?: string };
4
+ }
package/src/sender.ts ADDED
@@ -0,0 +1,18 @@
1
+ import axios from 'axios';
2
+ import { WhatsAppPayload } from './payloads';
3
+
4
+ export async function sendMessage(
5
+ token: string,
6
+ phoneNumberId: string,
7
+ apiVersion: string,
8
+ to: string,
9
+ payload: WhatsAppPayload
10
+ ) {
11
+ const url = `https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`;
12
+ const response = await axios.post(
13
+ url,
14
+ { messaging_product: 'whatsapp', to, ...payload },
15
+ { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' } }
16
+ );
17
+ return response.data;
18
+ }
@@ -1,8 +0,0 @@
1
- import { WhatsAppPayload } from './types';
2
- export declare class WhatsAppClient {
3
- private readonly token;
4
- private readonly phoneNumberId;
5
- private readonly apiVersion;
6
- constructor(token: string, phoneNumberId: string, apiVersion?: string);
7
- sendMessage(to: string, payload: WhatsAppPayload): Promise<any>;
8
- }
@@ -1,29 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.WhatsAppClient = void 0;
7
- const axios_1 = __importDefault(require("axios"));
8
- class WhatsAppClient {
9
- constructor(token, phoneNumberId, apiVersion = 'v24.0') {
10
- this.token = token;
11
- this.phoneNumberId = phoneNumberId;
12
- this.apiVersion = apiVersion;
13
- }
14
- async sendMessage(to, payload) {
15
- const url = `https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}/messages`;
16
- const response = await axios_1.default.post(url, {
17
- messaging_product: 'whatsapp',
18
- to,
19
- ...payload,
20
- }, {
21
- headers: {
22
- Authorization: `Bearer ${this.token}`,
23
- 'Content-Type': 'application/json',
24
- },
25
- });
26
- return response.data;
27
- }
28
- }
29
- exports.WhatsAppClient = WhatsAppClient;
@@ -1,14 +0,0 @@
1
- import * as T from './types';
2
- export declare const textMessage: (body: string, preview?: boolean) => T.TextPayload;
3
- export declare const imageMessage: (link: string, caption?: string) => T.ImagePayload;
4
- export declare const audioMessage: (link: string) => T.AudioPayload;
5
- export declare const videoMessage: (link: string, caption?: string) => T.VideoPayload;
6
- export declare const documentMessage: (link: string, filename: string, caption?: string) => T.DocumentPayload;
7
- export declare const locationMessage: (latitude: number, longitude: number, name?: string, address?: string) => T.LocationPayload;
8
- export declare const contactMessage: (firstName: string, phone: string, lastName?: string) => T.ContactPayload;
9
- export declare const buttonMessage: (bodyText: string, buttons: {
10
- id: string;
11
- title: string;
12
- }[]) => T.ButtonPayload;
13
- export declare const listMessage: (bodyText: string, buttonText: string, sections: T.ListPayload["interactive"]["action"]["sections"]) => T.ListPayload;
14
- export declare const templateMessage: (name: string, language?: string, components?: any[]) => T.TemplatePayload;
@@ -1,71 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.templateMessage = exports.listMessage = exports.buttonMessage = exports.contactMessage = exports.locationMessage = exports.documentMessage = exports.videoMessage = exports.audioMessage = exports.imageMessage = exports.textMessage = void 0;
4
- const textMessage = (body, preview = false) => ({
5
- type: 'text',
6
- text: { body, preview_url: preview },
7
- });
8
- exports.textMessage = textMessage;
9
- const imageMessage = (link, caption) => ({
10
- type: 'image',
11
- image: { link, caption },
12
- });
13
- exports.imageMessage = imageMessage;
14
- const audioMessage = (link) => ({
15
- type: 'audio',
16
- audio: { link },
17
- });
18
- exports.audioMessage = audioMessage;
19
- const videoMessage = (link, caption) => ({
20
- type: 'video',
21
- video: { link, caption },
22
- });
23
- exports.videoMessage = videoMessage;
24
- const documentMessage = (link, filename, caption) => ({
25
- type: 'document',
26
- document: { link, filename, caption },
27
- });
28
- exports.documentMessage = documentMessage;
29
- const locationMessage = (latitude, longitude, name, address) => ({
30
- type: 'location',
31
- location: { latitude, longitude, name, address },
32
- });
33
- exports.locationMessage = locationMessage;
34
- const contactMessage = (firstName, phone, lastName) => ({
35
- type: 'contacts',
36
- contacts: [
37
- {
38
- name: { first_name: firstName, last_name: lastName },
39
- phones: [{ phone }],
40
- },
41
- ],
42
- });
43
- exports.contactMessage = contactMessage;
44
- const buttonMessage = (bodyText, buttons) => ({
45
- type: 'interactive',
46
- interactive: {
47
- type: 'button',
48
- body: { text: bodyText },
49
- action: {
50
- buttons: buttons.map((b) => ({
51
- type: 'reply',
52
- reply: b,
53
- })),
54
- },
55
- },
56
- });
57
- exports.buttonMessage = buttonMessage;
58
- const listMessage = (bodyText, buttonText, sections) => ({
59
- type: 'interactive',
60
- interactive: {
61
- type: 'list',
62
- body: { text: bodyText },
63
- action: { button: buttonText, sections },
64
- },
65
- });
66
- exports.listMessage = listMessage;
67
- const templateMessage = (name, language = 'en_US', components = []) => ({
68
- type: 'template',
69
- template: { name, language: { code: language }, components },
70
- });
71
- exports.templateMessage = templateMessage;
@@ -1,109 +0,0 @@
1
- export type LanguageCode = string;
2
- export interface BasePayload {
3
- type: string;
4
- }
5
- export interface TextPayload extends BasePayload {
6
- type: 'text';
7
- text: {
8
- body: string;
9
- preview_url?: boolean;
10
- };
11
- }
12
- export interface ImagePayload extends BasePayload {
13
- type: 'image';
14
- image: {
15
- link: string;
16
- caption?: string;
17
- };
18
- }
19
- export interface AudioPayload extends BasePayload {
20
- type: 'audio';
21
- audio: {
22
- link: string;
23
- };
24
- }
25
- export interface VideoPayload extends BasePayload {
26
- type: 'video';
27
- video: {
28
- link: string;
29
- caption?: string;
30
- };
31
- }
32
- export interface DocumentPayload extends BasePayload {
33
- type: 'document';
34
- document: {
35
- link: string;
36
- filename: string;
37
- caption?: string;
38
- };
39
- }
40
- export interface LocationPayload extends BasePayload {
41
- type: 'location';
42
- location: {
43
- latitude: number;
44
- longitude: number;
45
- name?: string;
46
- address?: string;
47
- };
48
- }
49
- export interface ContactPayload extends BasePayload {
50
- type: 'contacts';
51
- contacts: {
52
- name: {
53
- first_name: string;
54
- last_name?: string;
55
- };
56
- phones: {
57
- phone: string;
58
- }[];
59
- }[];
60
- }
61
- export interface ButtonPayload extends BasePayload {
62
- type: 'interactive';
63
- interactive: {
64
- type: 'button';
65
- body: {
66
- text: string;
67
- };
68
- action: {
69
- buttons: {
70
- type: 'reply';
71
- reply: {
72
- id: string;
73
- title: string;
74
- };
75
- }[];
76
- };
77
- };
78
- }
79
- export interface ListPayload extends BasePayload {
80
- type: 'interactive';
81
- interactive: {
82
- type: 'list';
83
- body: {
84
- text: string;
85
- };
86
- action: {
87
- button: string;
88
- sections: {
89
- title: string;
90
- rows: {
91
- id: string;
92
- title: string;
93
- description?: string;
94
- }[];
95
- }[];
96
- };
97
- };
98
- }
99
- export interface TemplatePayload extends BasePayload {
100
- type: 'template';
101
- template: {
102
- name: string;
103
- language: {
104
- code: LanguageCode;
105
- };
106
- components?: any[];
107
- };
108
- }
109
- export type WhatsAppPayload = TextPayload | ImagePayload | AudioPayload | VideoPayload | DocumentPayload | LocationPayload | ContactPayload | ButtonPayload | ListPayload | TemplatePayload;
@@ -1,3 +0,0 @@
1
- import { Request, Response } from 'express';
2
- export declare const verifyWebhook: (req: Request, res: Response) => Response<any, Record<string, any>>;
3
- export declare const handleWebhook: (req: Request, res: Response) => void;
@@ -1,18 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.handleWebhook = exports.verifyWebhook = void 0;
4
- const verifyWebhook = (req, res) => {
5
- const mode = req.query['hub.mode'];
6
- const token = req.query['hub.verify_token'];
7
- const challenge = req.query['hub.challenge'];
8
- if (mode === 'subscribe' && token === process.env.WHATSAPP_VERIFY_TOKEN) {
9
- return res.status(200).send(challenge);
10
- }
11
- return res.sendStatus(403);
12
- };
13
- exports.verifyWebhook = verifyWebhook;
14
- const handleWebhook = (req, res) => {
15
- console.log('Incoming webhook:', JSON.stringify(req.body, null, 2));
16
- res.sendStatus(200);
17
- };
18
- exports.handleWebhook = handleWebhook;
@@ -1,6 +0,0 @@
1
- export declare class WebhookController {
2
- verify(challenge: string): string;
3
- handle(body: any): Promise<{
4
- status: string;
5
- }>;
6
- }
@@ -1,41 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- var __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.WebhookController = void 0;
16
- const common_1 = require("@nestjs/common");
17
- let WebhookController = class WebhookController {
18
- verify(challenge) {
19
- return challenge;
20
- }
21
- handle(body) {
22
- return Promise.resolve({ status: 'ok' });
23
- }
24
- };
25
- exports.WebhookController = WebhookController;
26
- __decorate([
27
- (0, common_1.Get)(),
28
- __param(0, (0, common_1.Query)('hub.challenge')),
29
- __metadata("design:type", Function),
30
- __metadata("design:paramtypes", [String]),
31
- __metadata("design:returntype", String)
32
- ], WebhookController.prototype, "verify", null);
33
- __decorate([
34
- (0, common_1.Post)(),
35
- __metadata("design:type", Function),
36
- __metadata("design:paramtypes", [Object]),
37
- __metadata("design:returntype", Promise)
38
- ], WebhookController.prototype, "handle", null);
39
- exports.WebhookController = WebhookController = __decorate([
40
- (0, common_1.Controller)('webhook')
41
- ], WebhookController);
@@ -1,2 +0,0 @@
1
- export declare class WhatsAppModule {
2
- }
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.WhatsAppModule = void 0;
10
- const common_1 = require("@nestjs/common");
11
- const whatsapp_service_1 = require("./whatsapp.service");
12
- const webhook_controller_1 = require("./webhook.controller");
13
- let WhatsAppModule = class WhatsAppModule {
14
- };
15
- exports.WhatsAppModule = WhatsAppModule;
16
- exports.WhatsAppModule = WhatsAppModule = __decorate([
17
- (0, common_1.Module)({
18
- providers: [whatsapp_service_1.WhatsAppService],
19
- exports: [whatsapp_service_1.WhatsAppService],
20
- controllers: [webhook_controller_1.WebhookController],
21
- })
22
- ], WhatsAppModule);
@@ -1,4 +0,0 @@
1
- export declare class WhatsAppService {
2
- private client;
3
- sendMessage(to: string, payload: any): Promise<any>;
4
- }
@@ -1,23 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.WhatsAppService = void 0;
10
- const common_1 = require("@nestjs/common");
11
- const WhatsAppClient_1 = require("../core/WhatsAppClient");
12
- let WhatsAppService = class WhatsAppService {
13
- constructor() {
14
- this.client = new WhatsAppClient_1.WhatsAppClient(process.env.WHATSAPP_TOKEN, process.env.WHATSAPP_PHONE_ID);
15
- }
16
- sendMessage(to, payload) {
17
- return this.client.sendMessage(to, payload);
18
- }
19
- };
20
- exports.WhatsAppService = WhatsAppService;
21
- exports.WhatsAppService = WhatsAppService = __decorate([
22
- (0, common_1.Injectable)()
23
- ], WhatsAppService);
@@ -1,31 +0,0 @@
1
- import axios from 'axios';
2
- import { WhatsAppPayload } from './types';
3
-
4
- export class WhatsAppClient {
5
- constructor(
6
- private readonly token: string,
7
- private readonly phoneNumberId: string,
8
- private readonly apiVersion = 'v24.0'
9
- ) {}
10
-
11
- async sendMessage(to: string, payload: WhatsAppPayload) {
12
- const url = `https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}/messages`;
13
-
14
- const response = await axios.post(
15
- url,
16
- {
17
- messaging_product: 'whatsapp',
18
- to,
19
- ...payload,
20
- },
21
- {
22
- headers: {
23
- Authorization: `Bearer ${this.token}`,
24
- 'Content-Type': 'application/json',
25
- },
26
- }
27
- );
28
-
29
- return response.data;
30
- }
31
- }
@@ -1,93 +0,0 @@
1
- import * as T from './types';
2
-
3
- export const textMessage = (body: string, preview = false): T.TextPayload => ({
4
- type: 'text',
5
- text: { body, preview_url: preview },
6
- });
7
-
8
- export const imageMessage = (link: string, caption?: string): T.ImagePayload => ({
9
- type: 'image',
10
- image: { link, caption },
11
- });
12
-
13
- export const audioMessage = (link: string): T.AudioPayload => ({
14
- type: 'audio',
15
- audio: { link },
16
- });
17
-
18
- export const videoMessage = (link: string, caption?: string): T.VideoPayload => ({
19
- type: 'video',
20
- video: { link, caption },
21
- });
22
-
23
- export const documentMessage = (
24
- link: string,
25
- filename: string,
26
- caption?: string
27
- ): T.DocumentPayload => ({
28
- type: 'document',
29
- document: { link, filename, caption },
30
- });
31
-
32
- export const locationMessage = (
33
- latitude: number,
34
- longitude: number,
35
- name?: string,
36
- address?: string
37
- ): T.LocationPayload => ({
38
- type: 'location',
39
- location: { latitude, longitude, name, address },
40
- });
41
-
42
- export const contactMessage = (
43
- firstName: string,
44
- phone: string,
45
- lastName?: string
46
- ): T.ContactPayload => ({
47
- type: 'contacts',
48
- contacts: [
49
- {
50
- name: { first_name: firstName, last_name: lastName },
51
- phones: [{ phone }],
52
- },
53
- ],
54
- });
55
-
56
- export const buttonMessage = (
57
- bodyText: string,
58
- buttons: { id: string; title: string }[]
59
- ): T.ButtonPayload => ({
60
- type: 'interactive',
61
- interactive: {
62
- type: 'button',
63
- body: { text: bodyText },
64
- action: {
65
- buttons: buttons.map((b) => ({
66
- type: 'reply',
67
- reply: b,
68
- })),
69
- },
70
- },
71
- });
72
-
73
- export const listMessage = (
74
- bodyText: string,
75
- buttonText: string,
76
- sections: T.ListPayload['interactive']['action']['sections']
77
- ): T.ListPayload => ({
78
- type: 'interactive',
79
- interactive: {
80
- type: 'list',
81
- body: { text: bodyText },
82
- action: { button: buttonText, sections },
83
- },
84
- });
85
-
86
- export const templateMessage = (
87
- name: string,
88
- language = 'en_US',
89
- components: any[] = []
90
- ): T.TemplatePayload => ({
91
- type: 'template',
92
- template: { name, language: { code: language }, components },
93
- });
package/src/core/types.ts DELETED
@@ -1,102 +0,0 @@
1
- export type LanguageCode = string;
2
-
3
- export interface BasePayload {
4
- type: string;
5
- }
6
-
7
- export interface TextPayload extends BasePayload {
8
- type: 'text';
9
- text: { body: string; preview_url?: boolean };
10
- }
11
-
12
- export interface ImagePayload extends BasePayload {
13
- type: 'image';
14
- image: { link: string; caption?: string };
15
- }
16
-
17
- export interface AudioPayload extends BasePayload {
18
- type: 'audio';
19
- audio: { link: string };
20
- }
21
-
22
- export interface VideoPayload extends BasePayload {
23
- type: 'video';
24
- video: { link: string; caption?: string };
25
- }
26
-
27
- export interface DocumentPayload extends BasePayload {
28
- type: 'document';
29
- document: { link: string; filename: string; caption?: string };
30
- }
31
-
32
- export interface LocationPayload extends BasePayload {
33
- type: 'location';
34
- location: {
35
- latitude: number;
36
- longitude: number;
37
- name?: string;
38
- address?: string;
39
- };
40
- }
41
-
42
- export interface ContactPayload extends BasePayload {
43
- type: 'contacts';
44
- contacts: {
45
- name: { first_name: string; last_name?: string };
46
- phones: { phone: string }[];
47
- }[];
48
- }
49
-
50
- export interface ButtonPayload extends BasePayload {
51
- type: 'interactive';
52
- interactive: {
53
- type: 'button';
54
- body: { text: string };
55
- action: {
56
- buttons: {
57
- type: 'reply';
58
- reply: { id: string; title: string };
59
- }[];
60
- };
61
- };
62
- }
63
-
64
- export interface ListPayload extends BasePayload {
65
- type: 'interactive';
66
- interactive: {
67
- type: 'list';
68
- body: { text: string };
69
- action: {
70
- button: string;
71
- sections: {
72
- title: string;
73
- rows: {
74
- id: string;
75
- title: string;
76
- description?: string;
77
- }[];
78
- }[];
79
- };
80
- };
81
- }
82
-
83
- export interface TemplatePayload extends BasePayload {
84
- type: 'template';
85
- template: {
86
- name: string;
87
- language: { code: LanguageCode };
88
- components?: any[];
89
- };
90
- }
91
-
92
- export type WhatsAppPayload =
93
- | TextPayload
94
- | ImagePayload
95
- | AudioPayload
96
- | VideoPayload
97
- | DocumentPayload
98
- | LocationPayload
99
- | ContactPayload
100
- | ButtonPayload
101
- | ListPayload
102
- | TemplatePayload;
@@ -1,18 +0,0 @@
1
- import { Request, Response } from 'express';
2
-
3
- export const verifyWebhook = (req: Request, res: Response) => {
4
- const mode = req.query['hub.mode'];
5
- const token = req.query['hub.verify_token'];
6
- const challenge = req.query['hub.challenge'];
7
-
8
- if (mode === 'subscribe' && token === process.env.WHATSAPP_VERIFY_TOKEN) {
9
- return res.status(200).send(challenge);
10
- }
11
-
12
- return res.sendStatus(403);
13
- };
14
-
15
- export const handleWebhook = (req: Request, res: Response) => {
16
- console.log('Incoming webhook:', JSON.stringify(req.body, null, 2));
17
- res.sendStatus(200);
18
- };
@@ -1,16 +0,0 @@
1
- import { Controller, Get, Post, Query, Body } from '@nestjs/common';
2
-
3
- @Controller('webhook')
4
- export class WebhookController {
5
-
6
- @Get()
7
- verify(@Query('hub.challenge') challenge: string): string {
8
- return challenge;
9
- }
10
-
11
- @Post()
12
- handle(body: any): Promise<{ status: string }> {
13
- return Promise.resolve({ status: 'ok' });
14
- }
15
-
16
- }
@@ -1,10 +0,0 @@
1
- import { Module } from '@nestjs/common';
2
- import { WhatsAppService } from './whatsapp.service';
3
- import { WebhookController } from './webhook.controller';
4
-
5
- @Module({
6
- providers: [WhatsAppService],
7
- exports: [WhatsAppService],
8
- controllers: [WebhookController],
9
- })
10
- export class WhatsAppModule {}
@@ -1,14 +0,0 @@
1
- import { Injectable } from '@nestjs/common';
2
- import { WhatsAppClient } from '../core/WhatsAppClient';
3
-
4
- @Injectable()
5
- export class WhatsAppService {
6
- private client = new WhatsAppClient(
7
- process.env.WHATSAPP_TOKEN!,
8
- process.env.WHATSAPP_PHONE_ID!
9
- );
10
-
11
- sendMessage(to: string, payload: any) {
12
- return this.client.sendMessage(to, payload);
13
- }
14
- }
File without changes