Skip to content

Commit e406f9f

Browse files
authored
Merge pull request #405 from muniter/jl-flow-calls
feat: flow calls using the flow_name
2 parents fa225fd + b0a2ed5 commit e406f9f

File tree

3 files changed

+78
-135
lines changed

3 files changed

+78
-135
lines changed

EXAMPLES/interactive.md

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -105,42 +105,37 @@ const interactive_catalog_message = new Interactive(
105105
## Navigate Flow
106106

107107
```ts
108-
import {
109-
Interactive,
110-
ActionNavigateFlow,
111-
Body
112-
} from "whatsapp-api-js/messages";
108+
import { Interactive, ActionFlow, Body } from "whatsapp-api-js/messages";
113109

114110
const interactive_navigate_flow_message = new Interactive(
115-
new ActionNavigateFlow(
116-
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
117-
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
118-
"Hello World",
119-
"form_screen",
120-
{
121-
name: "John Doe",
122-
age: 42
111+
new ActionFlow({
112+
flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
113+
flow_name: "my_welcome_flow", // Can also use flow_id instead
114+
flow_cta: "Start the Flow!",
115+
mode: "published",
116+
flow_action: "navigate", // Default
117+
flow_action_payload: {
118+
screen: "FIRST_SCREEN",
119+
data: { name: "John" }
123120
}
124-
),
121+
}),
125122
new Body("How was your experience today?")
126123
);
127124
```
128125

129126
## Data Exchange Flow
130127

131128
```ts
132-
import {
133-
Interactive,
134-
ActionDataExchangeFlow,
135-
Body
136-
} from "whatsapp-api-js/messages";
129+
import { Interactive, ActionFlow, Body } from "whatsapp-api-js/messages";
137130

138131
const interactive_data_exchange_flow_message = new Interactive(
139-
new ActionDataExchangeFlow(
140-
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
141-
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
142-
"Hello World"
143-
),
132+
new ActionFlow({
133+
flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
134+
flow_name: "my_welcome_flow", // Can also use flow_id instead
135+
flow_cta: "Start the Flow!",
136+
mode: "published",
137+
flow_action: "data_exchange"
138+
}),
144139
new Body("Hello World")
145140
);
146141
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "whatsapp-api-js",
3-
"version": "5.0.0",
3+
"version": "6.0.0",
44
"author": "Secreto31126",
55
"description": "A TypeScript server agnostic Whatsapp's Official API framework",
66
"license": "MIT",

src/messages/interactive.ts

Lines changed: 58 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,22 @@ export class Interactive extends ClientMessage {
232232
* ```ts
233233
* import {
234234
* Interactive,
235-
* ActionNavigateFlow,
235+
* ActionFlow,
236236
* Body
237237
* } from "whatsapp-api-js/messages";
238238
*
239239
* const interactive_navigate_flow_message = new Interactive(
240-
* new ActionNavigateFlow(
241-
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
242-
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
243-
* "Hello World",
244-
* "form_screen",
245-
* {
246-
* name: "John Doe",
247-
* age: 42
240+
* new ActionFlow({
241+
* flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
242+
* flow_name: "my_welcome_flow", // Can also use flow_id instead
243+
* flow_cta: "Start the Flow!",
244+
* mode: "published",
245+
* flow_action: "navigate", // Default
246+
* flow_action_payload: {
247+
* screen: "FIRST_SCREEN",
248+
* data: { name: "John" }
248249
* }
249-
* ),
250+
* }),
250251
* new Body("How was your experience today?")
251252
* );
252253
* ```
@@ -255,20 +256,21 @@ export class Interactive extends ClientMessage {
255256
* ```ts
256257
* import {
257258
* Interactive,
258-
* ActionDataExchangeFlow,
259+
* ActionFlow,
259260
* Body
260261
* } from "whatsapp-api-js/messages";
261262
*
262263
* const interactive_data_exchange_flow_message = new Interactive(
263-
* new ActionDataExchangeFlow(
264-
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
265-
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
266-
* "Hello World"
267-
* ),
268-
* new Body("Hello World")
264+
* new ActionFlow({
265+
* flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
266+
* flow_name: "my_welcome_flow", // Can also use flow_id instead
267+
* flow_cta: "Start the Flow!",
268+
* mode: "published",
269+
* flow_action: "data_exchange",
270+
* }),
271+
* new Body("Hello World") ```
269272
* );
270273
* ```
271-
*
272274
* @param action - The action for the interactive message
273275
* @param body - The body for the interactive message
274276
* @param header - The header of type text for the interactive message, it may be undefined if not needed
@@ -843,7 +845,7 @@ export class ActionCTA implements InteractiveAction {
843845
*
844846
* @group Interactive
845847
*/
846-
export abstract class ActionFlow implements InteractiveAction {
848+
export class ActionFlow implements InteractiveAction {
847849
/**
848850
* The name of the component
849851
*/
@@ -858,27 +860,23 @@ export abstract class ActionFlow implements InteractiveAction {
858860
/**
859861
* The Flow can be in either draft or published mode
860862
*/
861-
mode: "published" | "draft";
863+
mode?: "published" | "draft";
862864
/**
863865
* The Flow version, must be 3
864866
*/
865-
flow_message_version: "3";
867+
flow_message_version?: "3";
866868
/**
867869
* Flow token that is generated by the business to serve as an identifier
868870
*/
869-
flow_token: string;
870-
/**
871-
* Unique ID of the Flow provided by WhatsApp
872-
*/
873-
flow_id: string;
871+
flow_token?: string;
874872
/**
875873
* Text on the CTA button, character limit - 20 characters (no emoji)
876874
*/
877875
flow_cta: string;
878876
/**
879-
* The Flow type, if set to "navigate", flow_action_payload must be provided
877+
* The Flow type, if set to "navigate", flow_action_payload must be provided default to "navigate"
880878
*/
881-
flow_action: "navigate" | "data_exchange";
879+
flow_action?: "navigate" | "data_exchange";
882880
/**
883881
* Required if flow_action is "navigate", should be omitted otherwise
884882
*/
@@ -894,7 +892,7 @@ export abstract class ActionFlow implements InteractiveAction {
894892
};
895893
} & (
896894
| {
897-
flow_action: "navigate";
895+
flow_action?: "navigate";
898896
flow_action_payload: {
899897
screen: string;
900898
data?: unknown;
@@ -904,7 +902,23 @@ export abstract class ActionFlow implements InteractiveAction {
904902
flow_action: "data_exchange";
905903
flow_action_payload?: never;
906904
}
907-
);
905+
) &
906+
(
907+
| {
908+
/**
909+
* Unique ID of the Flow provided by WhatsApp
910+
*/
911+
flow_id: string;
912+
flow_name?: never;
913+
}
914+
| {
915+
/**
916+
* Flow name provided by the business as an alternative to flow_id
917+
*/
918+
flow_name: string;
919+
flow_id?: never;
920+
}
921+
);
908922

909923
/**
910924
* @override
@@ -922,6 +936,12 @@ export abstract class ActionFlow implements InteractiveAction {
922936
* @throws If parameters.flow_cta contains emojis
923937
*/
924938
constructor(parameters: ActionFlow["parameters"]) {
939+
parameters = {
940+
flow_message_version: "3",
941+
flow_action: "navigate",
942+
...parameters
943+
};
944+
925945
if (!parameters.flow_cta.length || parameters.flow_cta.length > 20) {
926946
throw new Error("Flow CTA must be between 1 and 20 characters");
927947
}
@@ -930,88 +950,16 @@ export abstract class ActionFlow implements InteractiveAction {
930950
throw new Error("Flow CTA must not contain emoji");
931951
}
932952

933-
this.parameters = parameters;
934-
}
935-
}
936-
937-
/**
938-
* Action API object
939-
*
940-
* @group Interactive
941-
*/
942-
export class ActionNavigateFlow extends ActionFlow {
943-
/**
944-
* Builds a navigate flow component for an Interactive message
945-
*
946-
* @param flow_token - Flow token that is generated by the business to serve as an identifier
947-
* @param flow_id - ID of the Flow provided by WhatsApp
948-
* @param flow_cta - Text on the CTA button, character limit - 20 characters (no emoji)
949-
* @param screen - The ID of the first Screen
950-
* @param data - Optional input data for the first Screen of the Flow. If provided, this must be a non-empty object.
951-
* @param mode - The Flow can be in either "draft" or "published" mode
952-
* @param flow_message_version - The Flow version, must be "3"
953-
* @throws If flow_cta is empty or over 20 characters
954-
* @throws If flow_cta contains emojis
955-
* @throws If data is provided and is an empty object
956-
*/
957-
constructor(
958-
flow_token: string,
959-
flow_id: string,
960-
flow_cta: string,
961-
screen: string,
962-
data?: unknown,
963-
mode: "published" | "draft" = "published",
964-
flow_message_version: "3" = "3"
965-
) {
966-
super({
967-
mode,
968-
flow_message_version,
969-
flow_token,
970-
flow_id,
971-
flow_cta,
972-
flow_action: "navigate",
973-
flow_action_payload: {
974-
screen,
975-
data
976-
}
977-
});
978-
979-
if (data && !Object.keys(data).length) {
980-
throw new Error("Flow data must be a non-empty object if provided");
953+
if (
954+
parameters.flow_action === "navigate" &&
955+
!Object.keys(parameters.flow_action_payload).length
956+
) {
957+
throw new Error(
958+
"Flow data must be a non-empty object when flow_action is navigate"
959+
);
981960
}
982-
}
983-
}
984961

985-
/**
986-
* Action API object
987-
*
988-
* @group Interactive
989-
*/
990-
export class ActionDataExchangeFlow extends ActionFlow {
991-
/**
992-
* Builds a data exchange flow component for an Interactive message
993-
*
994-
* @param flow_token - Flow token that is generated by the business to serve as an identifier
995-
* @param flow_id - ID of the Flow provided by WhatsApp
996-
* @param flow_cta - Text on the CTA button, character limit - 20 characters (no emoji)
997-
* @param mode - Must be "published" or "draft"
998-
* @param flow_message_version - Must be "3"
999-
*/
1000-
constructor(
1001-
flow_token: string,
1002-
flow_id: string,
1003-
flow_cta: string,
1004-
mode: "published" | "draft" = "published",
1005-
flow_message_version: "3" = "3"
1006-
) {
1007-
super({
1008-
mode,
1009-
flow_message_version,
1010-
flow_token,
1011-
flow_id,
1012-
flow_cta,
1013-
flow_action: "data_exchange"
1014-
});
962+
this.parameters = parameters;
1015963
}
1016964
}
1017965

0 commit comments

Comments
 (0)