@@ -232,21 +232,22 @@ export class Interactive extends ClientMessage {
232
232
* ```ts
233
233
* import {
234
234
* Interactive,
235
- * ActionNavigateFlow ,
235
+ * ActionFlow ,
236
236
* Body
237
237
* } from "whatsapp-api-js/messages";
238
238
*
239
239
* 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" }
248
249
* }
249
- * ),
250
+ * } ),
250
251
* new Body("How was your experience today?")
251
252
* );
252
253
* ```
@@ -255,20 +256,21 @@ export class Interactive extends ClientMessage {
255
256
* ```ts
256
257
* import {
257
258
* Interactive,
258
- * ActionDataExchangeFlow ,
259
+ * ActionFlow ,
259
260
* Body
260
261
* } from "whatsapp-api-js/messages";
261
262
*
262
263
* 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") ```
269
272
* );
270
273
* ```
271
- *
272
274
* @param action - The action for the interactive message
273
275
* @param body - The body for the interactive message
274
276
* @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 {
843
845
*
844
846
* @group Interactive
845
847
*/
846
- export abstract class ActionFlow implements InteractiveAction {
848
+ export class ActionFlow implements InteractiveAction {
847
849
/**
848
850
* The name of the component
849
851
*/
@@ -858,27 +860,23 @@ export abstract class ActionFlow implements InteractiveAction {
858
860
/**
859
861
* The Flow can be in either draft or published mode
860
862
*/
861
- mode : "published" | "draft" ;
863
+ mode ? : "published" | "draft" ;
862
864
/**
863
865
* The Flow version, must be 3
864
866
*/
865
- flow_message_version : "3" ;
867
+ flow_message_version ? : "3" ;
866
868
/**
867
869
* Flow token that is generated by the business to serve as an identifier
868
870
*/
869
- flow_token : string ;
870
- /**
871
- * Unique ID of the Flow provided by WhatsApp
872
- */
873
- flow_id : string ;
871
+ flow_token ?: string ;
874
872
/**
875
873
* Text on the CTA button, character limit - 20 characters (no emoji)
876
874
*/
877
875
flow_cta : string ;
878
876
/**
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"
880
878
*/
881
- flow_action : "navigate" | "data_exchange" ;
879
+ flow_action ? : "navigate" | "data_exchange" ;
882
880
/**
883
881
* Required if flow_action is "navigate", should be omitted otherwise
884
882
*/
@@ -894,7 +892,7 @@ export abstract class ActionFlow implements InteractiveAction {
894
892
} ;
895
893
} & (
896
894
| {
897
- flow_action : "navigate" ;
895
+ flow_action ? : "navigate" ;
898
896
flow_action_payload : {
899
897
screen : string ;
900
898
data ?: unknown ;
@@ -904,7 +902,23 @@ export abstract class ActionFlow implements InteractiveAction {
904
902
flow_action : "data_exchange" ;
905
903
flow_action_payload ?: never ;
906
904
}
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
+ ) ;
908
922
909
923
/**
910
924
* @override
@@ -922,6 +936,12 @@ export abstract class ActionFlow implements InteractiveAction {
922
936
* @throws If parameters.flow_cta contains emojis
923
937
*/
924
938
constructor ( parameters : ActionFlow [ "parameters" ] ) {
939
+ parameters = {
940
+ flow_message_version : "3" ,
941
+ flow_action : "navigate" ,
942
+ ...parameters
943
+ } ;
944
+
925
945
if ( ! parameters . flow_cta . length || parameters . flow_cta . length > 20 ) {
926
946
throw new Error ( "Flow CTA must be between 1 and 20 characters" ) ;
927
947
}
@@ -930,88 +950,16 @@ export abstract class ActionFlow implements InteractiveAction {
930
950
throw new Error ( "Flow CTA must not contain emoji" ) ;
931
951
}
932
952
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
+ ) ;
981
960
}
982
- }
983
- }
984
961
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 ;
1015
963
}
1016
964
}
1017
965
0 commit comments