whatsapp-api-js
Advanced tools
Comparing version
@@ -101,2 +101,8 @@ import type { ClientMessage, ClientMessageRequest, ServerMessage, ServerMessageResponse, ServerConversation, ServerPricing, ServerError, PostData } from "./types.d.ts"; | ||
/** | ||
* Block the user who sent the message | ||
* | ||
* @returns The {@link WhatsAppAPI.blockUser} return value | ||
*/ | ||
block: () => ReturnType<WhatsAppAPI["blockUser"]>; | ||
/** | ||
* Utility function for offloading code from the main thread, | ||
@@ -103,0 +109,0 @@ * useful for long running tasks such as AI generation |
/** @module WhatsAppAPI */ | ||
import { ClientMessage, type WhatsAppAPIConstructorArguments, type PostData, type GetParams, type ServerMessageResponse, type ServerMarkAsReadResponse, type ServerCreateQRResponse, type ServerRetrieveQRResponse, type ServerUpdateQRResponse, type ServerDeleteQRResponse, type ServerMediaRetrieveResponse, type ServerMediaUploadResponse, type ServerMediaDeleteResponse } from "./types.js"; | ||
import { ClientMessage, type WhatsAppAPIConstructorArguments, type PostData, type GetParams, type ServerMessageResponse, type ServerMarkAsReadResponse, type ServerCreateQRResponse, type ServerRetrieveQRResponse, type ServerUpdateQRResponse, type ServerDeleteQRResponse, type ServerMediaRetrieveResponse, type ServerMediaUploadResponse, type ServerMediaDeleteResponse, type ServerBlockResponse, type ServerUnblockResponse } from "./types.js"; | ||
import type { OnMessage, OnSent, OnStatus } from "./emitters.d.ts"; | ||
@@ -341,2 +341,24 @@ /** | ||
/** | ||
* Block a user from sending messages to the bot | ||
* | ||
* The block API has 2 restrictions: | ||
* - You can only block users that have messaged your business in the last 24 hours | ||
* - You can only block up to 64k users | ||
* | ||
* @param phoneID - The bot's phone ID from which to block | ||
* @param users - The user phone numbers to block (the API doesn't fail if it's empty) | ||
* @returns The server response | ||
*/ | ||
blockUser(phoneID: string, ...users: string[]): Promise<ServerBlockResponse | Response>; | ||
/** | ||
* Unblock a user from the bot's block list | ||
* | ||
* @remarks Contrary to blocking, unblocking isn't restricted by the 24 hours rule | ||
* | ||
* @param phoneID - The bot's phone ID from which to unblock | ||
* @param users - The user phone numbers to unblock (the API doesn't fail if it's empty) | ||
* @returns The server response | ||
*/ | ||
unblockUser(phoneID: string, ...users: string[]): Promise<ServerUnblockResponse | Response>; | ||
/** | ||
* POST helper, must be called inside the post function of your code. | ||
@@ -343,0 +365,0 @@ * When setting up the webhook, only subscribe to messages. Other subscritions support might be added later. |
@@ -508,2 +508,56 @@ import { | ||
} | ||
// #endregion | ||
// #region Block Operations | ||
/** | ||
* Block a user from sending messages to the bot | ||
* | ||
* The block API has 2 restrictions: | ||
* - You can only block users that have messaged your business in the last 24 hours | ||
* - You can only block up to 64k users | ||
* | ||
* @param phoneID - The bot's phone ID from which to block | ||
* @param users - The user phone numbers to block (the API doesn't fail if it's empty) | ||
* @returns The server response | ||
*/ | ||
async blockUser(phoneID, ...users) { | ||
const promise = this.$$apiFetch$$( | ||
`https://graph.facebook.com/${phoneID}/block_users`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
messaging_product: "whatsapp", | ||
block_users: users.map((user) => ({ user })) | ||
}) | ||
} | ||
); | ||
return this.getBody(promise); | ||
} | ||
/** | ||
* Unblock a user from the bot's block list | ||
* | ||
* @remarks Contrary to blocking, unblocking isn't restricted by the 24 hours rule | ||
* | ||
* @param phoneID - The bot's phone ID from which to unblock | ||
* @param users - The user phone numbers to unblock (the API doesn't fail if it's empty) | ||
* @returns The server response | ||
*/ | ||
async unblockUser(phoneID, ...users) { | ||
const promise = this.$$apiFetch$$( | ||
`https://graph.facebook.com/${phoneID}/block_users`, | ||
{ | ||
method: "DELETE", | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
messaging_product: "whatsapp", | ||
block_users: users.map((user) => ({ user })) | ||
}) | ||
} | ||
); | ||
return this.getBody(promise); | ||
} | ||
async post(data, raw_body, signature) { | ||
@@ -540,2 +594,3 @@ if (this.secure) { | ||
), | ||
block: () => this.blockUser(phoneID, from), | ||
offload: WhatsAppAPI.offload, | ||
@@ -542,0 +597,0 @@ Whatsapp: this |
@@ -718,2 +718,31 @@ /** | ||
export type ServerMediaDeleteResponse = ServerSuccessResponse | ServerErrorResponse; | ||
export type ServerBlockedError = Pick<ServerErrorResponse["error"], "message" | "type" | "code"> & { | ||
error_data: { | ||
details: string; | ||
}; | ||
}; | ||
export type ServerBlockedUser = { | ||
input: string; | ||
wa_id: string; | ||
}; | ||
export type ServerBlockFailedUser = { | ||
input: string; | ||
errors: Omit<ServerBlockedError, "type">[]; | ||
}; | ||
export type ServerBlockResponse = { | ||
messaging_product: "whatsapp"; | ||
block_users: { | ||
added_users: ServerBlockedUser[]; | ||
failed_users?: ServerBlockFailedUser[]; | ||
}; | ||
errors?: ServerBlockedError; | ||
} | ServerErrorResponse; | ||
export type ServerUnblockResponse = { | ||
messaging_product: "whatsapp"; | ||
block_users: { | ||
removed_users: ServerBlockedUser[]; | ||
failed_users?: ServerBlockFailedUser[]; | ||
}; | ||
errors?: ServerBlockedError; | ||
} | ServerErrorResponse; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "whatsapp-api-js", | ||
"version": "5.1.2", | ||
"version": "5.2.0", | ||
"author": "Secreto31126", | ||
@@ -143,26 +143,26 @@ "description": "A TypeScript server agnostic Whatsapp's Official API framework", | ||
"devDependencies": { | ||
"@adonisjs/http-server": "7.4.0", | ||
"@eslint/js": "9.21.0", | ||
"@adonisjs/http-server": "7.6.0", | ||
"@azure/functions": "^4.7.0", | ||
"@eslint/js": "9.23.0", | ||
"@reporters/github": "1.7.2", | ||
"@reporters/silent": "1.2.7", | ||
"@types/eslint__js": "8.42.3", | ||
"@types/express": "5.0.0", | ||
"@types/node": "18.19.76", | ||
"@vercel/node": "5.1.8", | ||
"@types/express": "5.0.1", | ||
"@types/node": "18.19.86", | ||
"@vercel/node": "5.1.14", | ||
"all-contributors-cli": "6.26.1", | ||
"c8": "10.1.3", | ||
"dotenv": "16.4.7", | ||
"esbuild": "0.25.0", | ||
"eslint": "9.21.0", | ||
"eslint-config-prettier": "10.0.1", | ||
"esbuild": "0.25.2", | ||
"eslint": "9.23.0", | ||
"eslint-config-prettier": "10.1.1", | ||
"eslint-plugin-tsdoc": "0.4.0", | ||
"glob": "11.0.1", | ||
"globals": "15.15.0", | ||
"prettier": "3.5.1", | ||
"sinon": "19.0.2", | ||
"typedoc": "0.27.8", | ||
"typescript": "5.7.3", | ||
"typescript-eslint": "8.24.1", | ||
"undici": "7.3.0" | ||
"globals": "16.0.0", | ||
"prettier": "3.5.3", | ||
"sinon": "20.0.0", | ||
"typedoc": "0.28.1", | ||
"typescript": "5.8.2", | ||
"typescript-eslint": "8.29.0", | ||
"undici": "7.7.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
550954
3.52%127
3.25%8506
3.47%