Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
♻️ Add Text component and currency component
Browse files Browse the repository at this point in the history
  • Loading branch information
ThallesP committed Jun 27, 2022
1 parent 71ad4ae commit 1fe60e5
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 107 deletions.
8 changes: 2 additions & 6 deletions examples/sendTemplateMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "dotenv/config";

import { Client } from "../src";
import { TextComponent } from "../src/components/TextComponent";

const client = new Client({
auth: {
Expand All @@ -13,11 +14,6 @@ const client = new Client({
await client.messages.sendTemplateMessage({
to: process.env.DST_NUMBER || "",
templateName: "welcome_message",
bodyComponents: [
{
type: "text",
text: "Sr. Brega",
},
],
components: [new TextComponent().setText("Sr. Brega")],
});
})();
21 changes: 21 additions & 0 deletions src/@types/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,24 @@ export interface IAPIRawListAction {
button: string;
sections: IAPIRawSection[];
}

export interface IAPIRawDateTime {
fallback_value: string;
/* component: {
day_of_week?: number;
year?: number;
month?: number;
day_of_month?: number;
hour?: number;
minute?: number;
calendar?: "GREGORIAN" | "SOLAR_HIJRI";
}; */
}

export interface IAPIRawDateTimeComponent {
type: string;
parameters: {
type: "date_time";
date_time: IAPIRawDateTime;
}[];
}
16 changes: 3 additions & 13 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@ export interface IDateTimeComponent {
fallback_value: Date | string;
}

export type IComponent =
| ITextComponent
| ICurrencyComponent
| IDateTimeComponent;

export type IBodyComponents =
| ITextComponent[]
| ICurrencyComponent[]
| IDateTimeComponent[];

export type IHeaderComponents = ICurrencyComponent[] | IDateTimeComponent[];

export type IButtonComponents = ICurrencyComponent[] | IDateTimeComponent[];
export interface IGenericComponent {
toAPIObject(): any;
}
3 changes: 2 additions & 1 deletion src/components/CurrencyComponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IGenericComponent } from "../@types";
import { CurrencyCodes } from "../utils/CurrencyCodes";

export class CurrencyComponent {
export class CurrencyComponent implements IGenericComponent {
amountMultipliedBy1000 = 0;
fallbackValue = "";
type: "body" | "header" = "body";
Expand Down
50 changes: 50 additions & 0 deletions src/components/DateTimeComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { IGenericComponent } from "../@types";
import { IAPIRawDateTimeComponent } from "../@types/api";

export class DateTimeComponent implements IGenericComponent {
public type: "header" | "body" = "body";
public fallbackValue: string = new Date().toString();

setType(type: "body" | "header"): this {
this.type = type;

return this;
}

setFallbackValue(fallbackValue: string | Date): this {
this.fallbackValue = fallbackValue.toString();

return this;
}

// For some reason, I can't get this to work and the whatsapp reference documentation doesn't even mention this
// I will remove it and test it later
/* setDate(date: Date) {
this.date = date;
return this;
} */

toAPIObject(): IAPIRawDateTimeComponent {
return {
type: this.type,
parameters: [
{
type: "date_time",
date_time: {
fallback_value: this.fallbackValue,
/* component: {
calendar: "GREGORIAN",
day_of_month: this.date.getDate(),
day_of_week: this.date.getDay(),
month: this.date.getMonth(),
hour: this.date.getHours(),
minute: this.date.getMinutes(),
year: this.date.getFullYear(),
}, See comment above */
},
},
],
};
}
}
22 changes: 22 additions & 0 deletions src/components/TextComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IGenericComponent } from "../@types";

export class TextComponent implements IGenericComponent {
public type: "body" | "header" | "button" = "body";
public text = "";

setType(type: "body" | "header" | "button"): this {
this.type = type;

return this;
}

setText(text: string): this {
this.text = text;

return this;
}

toAPIObject(): any {
return { type: this.type, parameters: [{ type: "text", text: this.text }] };
}
}
91 changes: 4 additions & 87 deletions src/managers/MessageManager.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import axios, { AxiosInstance } from "axios";

import {
IAuthOptions,
IBodyComponents,
IButtonComponents,
IComponent,
IHeaderComponents,
} from "../@types";
import { IAuthOptions, IGenericComponent } from "../@types";
import { IAPIRawButtonsAction, IAPIRawListAction } from "../@types/api";
import { Client } from "../client/client";
import { ReplyButton } from "../components/ReplyButton";
Expand All @@ -29,37 +23,7 @@ export interface ISendTemplateMessageOptions {
*/
templateName: string;

/**
* The template components for the body of the message.
*
* The components will be replaced by the variables in your template.
*
* Example:
* Here's your one time password {{1}} // "1" will be replaced by the component specified
* */
bodyComponents?: IBodyComponents;

/**
* The template components for the header of the message.
*
* They'll be replaced by the variables in your template.
*
* Example:
* Thanks for choosing us {{1}}. // "1" will be replaced by the component specified
* */
headerComponents?: IHeaderComponents;

/**
* The template components for the button of the message.
*
* They'll be replaced by the variables in your template.
*
* Example:
* Click here to buy {{1}}. // "1" will be replaced by the component specified
* */
buttonComponents?: IButtonComponents;

// components: any[]; TODO: work on this later
components: IGenericComponent[];
}

export interface IBaseSendInteractiveMessage {
Expand Down Expand Up @@ -157,55 +121,9 @@ export class MessageManager {
async sendTemplateMessage({
to,
templateName,
bodyComponents,
buttonComponents,
headerComponents,
// components,
components,
language = "en_US",
}: ISendTemplateMessageOptions) {
const components = [];

const convertToAPIComponent = (component: IComponent) => {
if (component.type === "date_time") {
const { fallback_value } = component;
return {
type: "date_time",
date_time: {
fallback_value,
},
};
}

return component;
};

if (bodyComponents) {
components.push({
type: "body",
parameters: bodyComponents.map((component) =>
convertToAPIComponent(component)
),
});
}

if (buttonComponents) {
components.push({
type: "button",
parameters: buttonComponents.map((component) =>
convertToAPIComponent(component)
),
});
}

if (headerComponents) {
components.push({
type: "header",
parameters: headerComponents.map((component) =>
convertToAPIComponent(component)
),
});
}

await this.request.post("/messages", {
messaging_product: "whatsapp",
to,
Expand All @@ -215,8 +133,7 @@ export class MessageManager {
language: {
code: language,
},
// components: components.map((component) => component.toAPIObject()),
components,
components: components.map((component) => component.toAPIObject()),
},
});
}
Expand Down

0 comments on commit 1fe60e5

Please sign in to comment.