Skip to content

Commit

Permalink
menu teste 13
Browse files Browse the repository at this point in the history
  • Loading branch information
frkr committed Oct 18, 2023
1 parent 05301d9 commit c456bfa
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "business-whatsapp",
"author": "frkr",
"license": "MIT",
"version": "18.0.11",
"version": "18.0.12",
"description": "WhatsApp Business API Client unofficial",
"homepage": "https://ideias.casa/",
"repository": {
Expand All @@ -11,7 +11,10 @@
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": ["dist"],
"files": [
"dist",
"src/WhatsApp.d.ts"
],
"keywords": [
"whatsapp",
"business",
Expand Down
90 changes: 42 additions & 48 deletions src/WhatsApp.ts → src/WhatsApp.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@

export class WAAuth {
accid: string;
apikey: string;
}

export class WhatsAppNotification {
interface WhatsAppNotification {
object: "whatsapp_business_account";
entry: Array<EntryEntity>;
}

export class EntryEntity {
interface EntryEntity {
id: string;
changes: Array<ValueObject>;
}

export class ValueObject {
interface ValueObject {
value: ValueEntity;
field: "messages";
}

export class ValueEntity {
interface ValueEntity {
messaging_product: "whatsapp";
metadata: MetadataEntity;
contacts?: Array<ContactsEntity>;
Expand All @@ -29,21 +23,21 @@ export class ValueEntity {
statuses: any // TODO
}

export class MetadataEntity {
interface MetadataEntity {
display_phone_number: string;
phone_number_id: string;
}

export class ContactsEntity {
interface ContactsEntity {
profile: ProfileEntity;
wa_id: string;
}

export class ProfileEntity {
interface ProfileEntity {
name: string;
}

export type MessageTypesRequest =
type MessageTypesRequest =
MediaTypes
| "text"
| "template"
Expand All @@ -53,11 +47,27 @@ export type MessageTypesRequest =
| "reaction"
| "location"
| "contacts"
export type MessageTypes = "button" | "system" | "unknown" | MessageTypesRequest
type MessageTypes = "button" | "system" | "unknown" | MessageTypesRequest

type MediaTypes = "audio" | "document" | "image" | "sticker" | "video"

interface MessageObjectRequest extends MessageObject {
messaging_product: "whatsapp";
recipient_type?: "individual"
to: string;
type: MessageTypesRequest;
//ttl?: number
template?: any // TODO
hsm?: any // TODO
}

export type MediaTypes = "audio" | "document" | "image" | "sticker" | "video"
interface MessageObjectEvent extends MessageObject {
from: string;
id: string;
timestamp: string;
}

export class MessageObject {
interface MessageObject {
type: MessageTypes;

audio?: MediaObject // TODO
Expand All @@ -76,80 +86,64 @@ export class MessageObject {
video?: MediaMessage;

}
export class MessageObjectRequest extends MessageObject {
messaging_product: "whatsapp";
recipient_type?: "individual"
to: string;
type: MessageTypesRequest=null;
//ttl?: number
template?: any // TODO
hsm?: any // TODO
}

export class MessageObjectEvent extends MessageObject {
from: string;
id: string;
timestamp: string;
}


export class MessageEntity {
interface MessageEntity {
}

export class InteractiveMessage {
interface InteractiveMessage {
type: "list";
header?: VariableEntity;
body?: VariableEntity;
footer?: VariableEntity;
action?: ActionEntity;
}

export class ActionEntity {
interface ActionEntity {
button: string;
sections: Array<SectionsEntity>;
}
export class SectionsEntity {
interface SectionsEntity {
title:string;
rows: Array<RowsEntity>;
}

export class RowsEntity {
id?: string;
title?: string;
description?: string;
interface RowsEntity {
id: string;
title: string;
description: string;
}

export class VariableEntity {
interface VariableEntity {
type?: 'text';
text?: string;
}

export class MenuRequest {
interface MenuRequest {
titulo?: string;
msg?: string;
rodape?: string;
itens: string[];
}

export class TextMessage extends MessageEntity {
interface TextMessage extends MessageEntity {
preview_url?: boolean;
body: string;
}

export class MediaMessage extends MessageEntity {
interface MediaMessage extends MessageEntity {
id?: string;
link?: string;
filename?: string;
provider?: string;

}

export class MediaObject {
messaging_product: "whatsapp";
interface MediaObject {
messaging_product: "whatsapp",
url?: string
mime_type?: string
sha256?: string
file_size?: number;
file_size?: number,
id?: string
caption?: string
}
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {InteractiveMessage, MediaMessage, MediaObject, MenuRequest, MessageObjectRequest, RowsEntity, WAAuth } from "./WhatsApp";
export interface WAAuth {
accid: string;
apikey: string;
}

export function onlyNumbers(waid: string): string {
return waid.replace(/[^0-9]/g, '');
Expand Down Expand Up @@ -149,8 +152,11 @@ export async function getMedia(apikey: string, url: string): Promise<Blob> {
}

export async function sendMenu(auth: WAAuth, waid: string, menu: MenuRequest): Promise<Response> {
let msgInteractive:InteractiveMessage = {
let msgInteractive: InteractiveMessage = {
type: "list",
header:null,
body: null,
footer: null,
action: {
button: 'Menu',
sections: [{
Expand All @@ -159,9 +165,9 @@ export async function sendMenu(auth: WAAuth, waid: string, menu: MenuRequest): P
return {
id: `${index}`,
title: `${index}`,
body: item,
description: item,
} as RowsEntity;
})
}),
}]
}
}
Expand All @@ -177,7 +183,7 @@ export async function sendMenu(auth: WAAuth, waid: string, menu: MenuRequest): P
}
}
if (menu.rodape) {
msgInteractive.footer= {
msgInteractive.footer = {
text: menu.rodape,
}
}
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"outDir": "./dist",
"declaration": true,
"esModuleInterop": true,
"types": [
"./src/WhatsApp.d.ts"
]
}
}

0 comments on commit c456bfa

Please sign in to comment.