Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/__tests__/http_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
import nock from 'nock';
import { WAConfigType } from '../types/config';
import { HttpMethodsEnum } from '../types/enums';

import HttpsClient from '../httpsClient';
import { NoParamCallback } from 'fs';
import { RequestHeaders } from '../types/httpsClient';

describe('HTTPS client tests', () => {
const sdkConfig: WAConfigType = (global as any).sdkConfig;
const basePath = `/${sdkConfig.CLOUD_API_VERSION}/${sdkConfig.WA_PHONE_NUMBER_ID}`;
const reqHeaders = {
const reqHeaders: RequestHeaders = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${sdkConfig.CLOUD_API_ACCESS_TOKEN}`,
};
Expand Down
15 changes: 8 additions & 7 deletions src/__tests__/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import nock from 'nock';
import { WAConfigType } from '../types/config';
import MessagesAPI from '../api/messages';
import WhatsApp from '../WhatsApp';
import { MessagesResponseObject } from '../types/messages';

describe('WhatsApp Messages API', () => {
const testRecipient = 1234;
Expand All @@ -29,7 +30,7 @@ describe('WhatsApp Messages API', () => {

const wa = new WhatsApp();
const basePath = `/${sdkConfig.CLOUD_API_VERSION}/${sdkConfig.WA_PHONE_NUMBER_ID}`;
const defaultMessagesResponseBody = {
const defaultMessagesResponseObjectBody: MessagesResponseObject = {
messaging_product: 'whatsapp',
contacts: [{ input: '16505076520', wa_id: '16505076520' }],
messages: [
Expand Down Expand Up @@ -57,12 +58,12 @@ describe('WhatsApp Messages API', () => {
.delay(200)
.delayBody(200)
.delayConnection(200)
.reply(200, defaultMessagesResponseBody);
.reply(200, defaultMessagesResponseObjectBody);

const response = await wa.messages.text({ body: 'test' }, testRecipient);

expect(await response.responseBodyToJSON()).toStrictEqual(
defaultMessagesResponseBody,
defaultMessagesResponseObjectBody,
);
scope.isDone();
});
Expand All @@ -73,7 +74,7 @@ describe('WhatsApp Messages API', () => {
.delay(200)
.delayBody(200)
.delayConnection(200)
.reply(200, defaultMessagesResponseBody);
.reply(200, defaultMessagesResponseObjectBody);

const meta_hosted_audio = {
id: '123456abcde',
Expand All @@ -84,7 +85,7 @@ describe('WhatsApp Messages API', () => {
const response = await wa.messages.audio(meta_hosted_audio, testRecipient);

expect(await response.responseBodyToJSON()).toStrictEqual(
defaultMessagesResponseBody,
defaultMessagesResponseObjectBody,
);
scope.isDone();
});
Expand All @@ -95,7 +96,7 @@ describe('WhatsApp Messages API', () => {
.delay(200)
.delayBody(200)
.delayConnection(200)
.reply(200, defaultMessagesResponseBody);
.reply(200, defaultMessagesResponseObjectBody);

const selfHostedAudio = {
link: new URL('https://example.com/example_1234.mp4').href,
Expand All @@ -106,7 +107,7 @@ describe('WhatsApp Messages API', () => {
const response = await wa.messages.audio(selfHostedAudio, testRecipient);

expect(await response.responseBodyToJSON()).toStrictEqual(
defaultMessagesResponseBody,
defaultMessagesResponseObjectBody,
);
scope.isDone();
});
Expand Down
24 changes: 12 additions & 12 deletions src/api/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {

send(
body: RequestData,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.client.sendCAPIRequest(
this.commonMethod,
this.commonEndpoint,
Expand All @@ -70,7 +70,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.AudioMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -87,7 +87,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: [m.ContactObject],
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -104,7 +104,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.DocumentMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -121,7 +121,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.ImageMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -138,7 +138,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.InteractiveObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -155,7 +155,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.LocationObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -172,7 +172,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.StickerMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -189,7 +189,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.MessageTemplateObject<ComponentTypesEnum>,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -206,7 +206,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.TextObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
LOGGER.log(body);
return this.send(
JSON.stringify(
Expand All @@ -224,7 +224,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {
body: m.VideoMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
return this.send(
JSON.stringify(
this.bodyBuilder(
Expand All @@ -239,7 +239,7 @@ export default class MessagesAPI extends BaseAPI implements m.MessagesClass {

async status(
body: m.StatusObject,
): Promise<RequesterResponseInterface<m.MessagesResponse>> {
): Promise<RequesterResponseInterface<m.MessagesResponseObject>> {
const mp: m.GeneralMessageBody = { messaging_product: 'whatsapp' };
const bodyToSend: m.StatusRequestBody = Object.assign(mp, body);

Expand Down
3 changes: 3 additions & 0 deletions src/api/twoStepVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export default class TwoStepVerificationAPI
pin: number,
): Promise<RequesterResponseInterface<tsv.SetPinResponseObject>> {
const body: tsv.TwoStepVerificationObject = { pin: pin.toString() };
LOGGER.log(
`Updating 2-step verificaiton pin for ${this.config.WA_PHONE_NUMBER_ID}`,
);
return this.client.sendCAPIRequest(
this.commonMethod,
this.commonEndpoint,
Expand Down
2 changes: 1 addition & 1 deletion src/httpsServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class Httpserver implements h.HttpserverClass {
return this.listening;
}

public close(cb?: (err?: Error) => any) {
public close(cb?: ((err?: Error | undefined) => void) | undefined) {
for (const socket of this.sockets) {
socket.destroy();
this.sockets.delete(socket);
Expand Down
32 changes: 17 additions & 15 deletions src/types/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ export type MessageRequestBody<T extends MessageTypesEnum> =
type MetaAudioMediaObject = {
id: string;
link?: never;
caption?: string;
};

type HostedAudioMediaObject = {
id?: never;
link: string;
caption?: string;
};

export type AudioMediaObject = MetaAudioMediaObject | HostedAudioMediaObject;
Expand All @@ -63,7 +65,7 @@ export type AudioMessageRequestBody =
[MessageTypesEnum.Audio]: [AudioMediaObject];
};

type AddressesObject = {
type AddressObject = {
street?: string;
city?: string;
state?: string;
Expand Down Expand Up @@ -105,7 +107,7 @@ type URLObject = {
};

export type ContactObject = {
addresses?: AddressesObject[];
addresses?: AddressObject[];
birthday?: `${number}${number}${number}${number}-${number}${number}-${number}${number}`;
emails?: EmailObject[];
name: NameObject;
Expand Down Expand Up @@ -345,7 +347,7 @@ type DateTimeObject = {

type DateTimeParametersObject =
ParametersObject<ParametersTypesEnum.Currency> & {
date_time: CurrencyObject;
date_time: DateTimeObject;
};

type DocumentParametersObject = ParametersObject<ParametersTypesEnum.Document> &
Expand Down Expand Up @@ -409,7 +411,7 @@ export type LocationMessageRequestBody =
[MessageTypesEnum.Location]: [LocationObject];
};

export type MessagesResponse = GeneralMessageBody & {
export type MessagesResponseObject = GeneralMessageBody & {
contacts: [
{
input: string;
Expand All @@ -428,53 +430,53 @@ export declare class MessagesClass extends BaseClass {
body: AudioMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
contacts(
body: [ContactObject],
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
document(
body: DocumentMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
image(
body: ImageMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
interactive(
body: InteractiveObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
location(
body: LocationObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
status(
body: StatusObject,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
sticker(
body: StickerMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
template(
body: MessageTemplateObject<ComponentTypesEnum>,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
text(
body: TextObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
video(
body: VideoMediaObject,
recipient: number,
replyMessageId?: string,
): Promise<RequesterResponseInterface<MessagesResponse>>;
): Promise<RequesterResponseInterface<MessagesResponseObject>>;
}
1 change: 1 addition & 0 deletions src/types/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export type MessagesObject = {
interactive?: InteractiveObject;
order?: Order_Object;
referral: ReferralObject;
sticker?: StickerObject;
system?: SystemObject;
text?: TextObject;
timestamp: string;
Expand Down
20 changes: 8 additions & 12 deletions website/docs/api-reference/messages/audio.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,25 @@ Send an existing self-hosted or Meta hosted audio file. Supported audio formats:
- OGG *Note: The base audio/ogg type is not supported.*

## Example:
Send a Meta-hosted message and then send an externally hosted audio file to the phone number `12345678901`.
Send a Meta-hosted message and then send an externally hosted audio file to the phone number `17815754340`.
```js
import WhatsApp from 'whatsapp';

const senderNumber = 12345678901234567890;
const wa = new WhatsApp( senderNumber );
const senderNumberId = 12345678901234567890;
const wa = new WhatsApp( senderNumberId );

const meta_hosted_audio =
{
"id" : "123456abcde",
"caption" : "My audio file",
"filename" : "example.mp4"
"id" : "123456abcde"
};

const selfHostedAudio =
{
"link" : new URL( "https://example.com/example_1234.mp4" ).href,
"caption" : "My audio file",
"filename" : "example.mp4"
"link" : new URL( "https://example.com/example_1234.mp4" ).href
};

await wa.messages.audio( meta_hosted_audio, 12345678901 );
wa.messages.audio( selfHostedAudio, 12345678901 );
await wa.messages.audio( meta_hosted_audio, 17815754340 );
wa.messages.audio( selfHostedAudio, 17815754340 );
```

## Arguments
Expand All @@ -44,4 +40,4 @@ wa.messages.audio( selfHostedAudio, 12345678901 );
3. `replyMessageId` : string (optional) — the received WhatsApp message Id to reply back to.

## Returns
Promise — Server response object on success.
Promise — Server response object on success. A successful response body JSON will be of type [MessagesResponseObject](../types/MessagesResponseObject).
Loading