Skip to content

Commit

Permalink
Merge pull request #405 from muniter/jl-flow-calls
Browse files Browse the repository at this point in the history
feat: flow calls using the flow_name
  • Loading branch information
Secreto31126 authored Jan 27, 2025
2 parents fa225fd + b0a2ed5 commit e406f9f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 135 deletions.
43 changes: 19 additions & 24 deletions EXAMPLES/interactive.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,42 +105,37 @@ const interactive_catalog_message = new Interactive(
## Navigate Flow

```ts
import {
Interactive,
ActionNavigateFlow,
Body
} from "whatsapp-api-js/messages";
import { Interactive, ActionFlow, Body } from "whatsapp-api-js/messages";

const interactive_navigate_flow_message = new Interactive(
new ActionNavigateFlow(
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
"Hello World",
"form_screen",
{
name: "John Doe",
age: 42
new ActionFlow({
flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
flow_name: "my_welcome_flow", // Can also use flow_id instead
flow_cta: "Start the Flow!",
mode: "published",
flow_action: "navigate", // Default
flow_action_payload: {
screen: "FIRST_SCREEN",
data: { name: "John" }
}
),
}),
new Body("How was your experience today?")
);
```

## Data Exchange Flow

```ts
import {
Interactive,
ActionDataExchangeFlow,
Body
} from "whatsapp-api-js/messages";
import { Interactive, ActionFlow, Body } from "whatsapp-api-js/messages";

const interactive_data_exchange_flow_message = new Interactive(
new ActionDataExchangeFlow(
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
"5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
"Hello World"
),
new ActionFlow({
flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
flow_name: "my_welcome_flow", // Can also use flow_id instead
flow_cta: "Start the Flow!",
mode: "published",
flow_action: "data_exchange"
}),
new Body("Hello World")
);
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatsapp-api-js",
"version": "5.0.0",
"version": "6.0.0",
"author": "Secreto31126",
"description": "A TypeScript server agnostic Whatsapp's Official API framework",
"license": "MIT",
Expand Down
168 changes: 58 additions & 110 deletions src/messages/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,21 +232,22 @@ export class Interactive extends ClientMessage {
* ```ts
* import {
* Interactive,
* ActionNavigateFlow,
* ActionFlow,
* Body
* } from "whatsapp-api-js/messages";
*
* const interactive_navigate_flow_message = new Interactive(
* new ActionNavigateFlow(
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
* "Hello World",
* "form_screen",
* {
* name: "John Doe",
* age: 42
* new ActionFlow({
* flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
* flow_name: "my_welcome_flow", // Can also use flow_id instead
* flow_cta: "Start the Flow!",
* mode: "published",
* flow_action: "navigate", // Default
* flow_action_payload: {
* screen: "FIRST_SCREEN",
* data: { name: "John" }
* }
* ),
* }),
* new Body("How was your experience today?")
* );
* ```
Expand All @@ -255,20 +256,21 @@ export class Interactive extends ClientMessage {
* ```ts
* import {
* Interactive,
* ActionDataExchangeFlow,
* ActionFlow,
* Body
* } from "whatsapp-api-js/messages";
*
* const interactive_data_exchange_flow_message = new Interactive(
* new ActionDataExchangeFlow(
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
* "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
* "Hello World"
* ),
* new Body("Hello World")
* new ActionFlow({
* flow_token: "5f9b3b4f-2b7a-4f4f-8f4f-4f4f4f4f4f4f",
* flow_name: "my_welcome_flow", // Can also use flow_id instead
* flow_cta: "Start the Flow!",
* mode: "published",
* flow_action: "data_exchange",
* }),
* new Body("Hello World") ```
* );
* ```
*
* @param action - The action for the interactive message
* @param body - The body for the interactive message
* @param header - The header of type text for the interactive message, it may be undefined if not needed
Expand Down Expand Up @@ -843,7 +845,7 @@ export class ActionCTA implements InteractiveAction {
*
* @group Interactive
*/
export abstract class ActionFlow implements InteractiveAction {
export class ActionFlow implements InteractiveAction {
/**
* The name of the component
*/
Expand All @@ -858,27 +860,23 @@ export abstract class ActionFlow implements InteractiveAction {
/**
* The Flow can be in either draft or published mode
*/
mode: "published" | "draft";
mode?: "published" | "draft";
/**
* The Flow version, must be 3
*/
flow_message_version: "3";
flow_message_version?: "3";
/**
* Flow token that is generated by the business to serve as an identifier
*/
flow_token: string;
/**
* Unique ID of the Flow provided by WhatsApp
*/
flow_id: string;
flow_token?: string;
/**
* Text on the CTA button, character limit - 20 characters (no emoji)
*/
flow_cta: string;
/**
* The Flow type, if set to "navigate", flow_action_payload must be provided
* The Flow type, if set to "navigate", flow_action_payload must be provided default to "navigate"
*/
flow_action: "navigate" | "data_exchange";
flow_action?: "navigate" | "data_exchange";
/**
* Required if flow_action is "navigate", should be omitted otherwise
*/
Expand All @@ -894,7 +892,7 @@ export abstract class ActionFlow implements InteractiveAction {
};
} & (
| {
flow_action: "navigate";
flow_action?: "navigate";
flow_action_payload: {
screen: string;
data?: unknown;
Expand All @@ -904,7 +902,23 @@ export abstract class ActionFlow implements InteractiveAction {
flow_action: "data_exchange";
flow_action_payload?: never;
}
);
) &
(
| {
/**
* Unique ID of the Flow provided by WhatsApp
*/
flow_id: string;
flow_name?: never;
}
| {
/**
* Flow name provided by the business as an alternative to flow_id
*/
flow_name: string;
flow_id?: never;
}
);

/**
* @override
Expand All @@ -922,6 +936,12 @@ export abstract class ActionFlow implements InteractiveAction {
* @throws If parameters.flow_cta contains emojis
*/
constructor(parameters: ActionFlow["parameters"]) {
parameters = {
flow_message_version: "3",
flow_action: "navigate",
...parameters
};

if (!parameters.flow_cta.length || parameters.flow_cta.length > 20) {
throw new Error("Flow CTA must be between 1 and 20 characters");
}
Expand All @@ -930,88 +950,16 @@ export abstract class ActionFlow implements InteractiveAction {
throw new Error("Flow CTA must not contain emoji");
}

this.parameters = parameters;
}
}

/**
* Action API object
*
* @group Interactive
*/
export class ActionNavigateFlow extends ActionFlow {
/**
* Builds a navigate flow component for an Interactive message
*
* @param flow_token - Flow token that is generated by the business to serve as an identifier
* @param flow_id - ID of the Flow provided by WhatsApp
* @param flow_cta - Text on the CTA button, character limit - 20 characters (no emoji)
* @param screen - The ID of the first Screen
* @param data - Optional input data for the first Screen of the Flow. If provided, this must be a non-empty object.
* @param mode - The Flow can be in either "draft" or "published" mode
* @param flow_message_version - The Flow version, must be "3"
* @throws If flow_cta is empty or over 20 characters
* @throws If flow_cta contains emojis
* @throws If data is provided and is an empty object
*/
constructor(
flow_token: string,
flow_id: string,
flow_cta: string,
screen: string,
data?: unknown,
mode: "published" | "draft" = "published",
flow_message_version: "3" = "3"
) {
super({
mode,
flow_message_version,
flow_token,
flow_id,
flow_cta,
flow_action: "navigate",
flow_action_payload: {
screen,
data
}
});

if (data && !Object.keys(data).length) {
throw new Error("Flow data must be a non-empty object if provided");
if (
parameters.flow_action === "navigate" &&
!Object.keys(parameters.flow_action_payload).length
) {
throw new Error(
"Flow data must be a non-empty object when flow_action is navigate"
);
}
}
}

/**
* Action API object
*
* @group Interactive
*/
export class ActionDataExchangeFlow extends ActionFlow {
/**
* Builds a data exchange flow component for an Interactive message
*
* @param flow_token - Flow token that is generated by the business to serve as an identifier
* @param flow_id - ID of the Flow provided by WhatsApp
* @param flow_cta - Text on the CTA button, character limit - 20 characters (no emoji)
* @param mode - Must be "published" or "draft"
* @param flow_message_version - Must be "3"
*/
constructor(
flow_token: string,
flow_id: string,
flow_cta: string,
mode: "published" | "draft" = "published",
flow_message_version: "3" = "3"
) {
super({
mode,
flow_message_version,
flow_token,
flow_id,
flow_cta,
flow_action: "data_exchange"
});
this.parameters = parameters;
}
}

Expand Down

0 comments on commit e406f9f

Please sign in to comment.