### Initialize WABAClient in TypeScript Source: https://github.com/marcosnicolau/whatsapp-business-sdk/blob/main/README.md Demonstrates how to initialize the WABAClient for interacting with the WhatsApp Business Cloud API. It requires account ID, API token, and business phone ID. Error handling for API responses is included. ```typescript import { WABAClient, WABAErrorAPI } from "whatsapp-business"; //You cant get it from the meta for developers app administration const client = new WABAClient({ accountId: "", apiToken: "", phoneId: "", }); const foo = async () => { try { const res = await client.getBusinessPhoneNumbers(); console.log(res); } catch (err) { const error: WABAErrorAPI = err; console.error(error.message); } }; foo(); ``` -------------------------------- ### Initialize and Handle WhatsApp Webhooks (TypeScript) Source: https://github.com/marcosnicolau/whatsapp-business-sdk/blob/main/README.md Initializes the WebhookClient for WhatsApp, sets up event handlers for different message types like text messages, and uses WABAClient to interact with the WhatsApp API (e.g., marking messages as read, sending reactions, and replying). This requires validation token, path, port, account ID, phone ID, and API token. ```typescript import { WebhookClient, WABAClient } from "./index"; //The token and path must match the values you set on the application management const webhookClient = new WebhookClient({ token: "", path: "/whatsapp/webhook", port: 8080, }); const wabaClient = new WABAClient({ accountId: "", phoneId: "", apiToken: "", }); //Starts a server and triggers the received functions based on the webhook event type webhookClient.initWebhook({ onStartListening: () => { console.log("Server started listening"); }, onTextMessageReceived: async (payload, contact) => { try { const messageId = payload.id.toString(); const contactNumber = contact.wa_id; //Mark message as read await wabaClient.markMessageAsRead(messageId); //React to message await wabaClient.sendMessage({ to: contactNumber, type: "reaction", reaction: { message_id: messageId, emoji: "😄" }, }); //Respond to message await wabaClient.sendMessage({ type: "text", to: contactNumber, text: { body: "Ok!" }, //This is optional, it enables reply-to feature context: { message_id: messageId, }, }); } catch (err) { console.log(err); } }, }); ``` -------------------------------- ### Access Default Express App for Webhooks (TypeScript) Source: https://github.com/marcosnicolau/whatsapp-business-sdk/blob/main/README.md Shows how to access the default Express application instance created by the WebhookClient when no custom app is provided. This allows for further configuration of the default app, such as setting trust proxy settings. ```typescript import { WebhookClient } from "./index"; const webhookClient = new WebhookClient({ token: "", path: "/whatsapp/webhook", port: 8080, }); const app = webhookClient.expressApp.app; //Your configuration... app.set("trust proxy", true); webhookClient.initWebhook({ onStartListening: () => { console.log("Server started listening"); }, }); ``` -------------------------------- ### Integrate WhatsApp Webhooks with Custom Express App (TypeScript) Source: https://github.com/marcosnicolau/whatsapp-business-sdk/blob/main/README.md Demonstrates how to integrate the WhatsApp WebhookClient with an existing Express application. The client is configured with an `expressApp` object, setting `shouldStartListening` to false to allow manual control of the Express server's listening process. ```typescript import { WebhookClient } from "./index"; import express from "express"; const myApp = express(); const webhookClient = new WebhookClient({ token: "", path: "/whatsapp/webhook", expressApp: { //Set to false if you want to initialize the server yourself //Otherwise, it will start listening when firing initWebhook() shouldStartListening: false, app: myApp, }, }); myApp.listen(8080, () => { console.log("My server nows listens to whatsapp webhooks"); }); ``` -------------------------------- ### Send Image Message using WhatsApp Business SDK Source: https://github.com/marcosnicolau/whatsapp-business-sdk/blob/main/README.md Illustrates how to send an image message with a caption using the WhatsApp Business Cloud API. Requires a URL to the image and the recipient's phone number. Error handling is implemented for the API call. ```typescript const sendPictureMessage = async ({ link, caption }: MediaObject, to: string) => { try { const res = await client.sendMessage({ to, type: "image", image: { link, caption } }); console.log(res); } catch (err) { const error: WABAErrorAPI = err; console.error(error.message); } }; sendPictureMessage( { link: "", caption: "" }, "" ); ``` -------------------------------- ### Send Text Message using WhatsApp Business SDK Source: https://github.com/marcosnicolau/whatsapp-business-sdk/blob/main/README.md Provides a function to send a text message via the WhatsApp Business Cloud API. It takes the message body and the recipient's phone number as input. Includes error handling for the sending process. ```typescript const sendTextMessage = async (body: string, to: string) => { try { const res = await client.sendMessage({ to, type: "text", text: { body } }); console.log(res); } catch (err) { const error: WABAErrorAPI = err; console.error(error.message); } }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.