This is a package for the econsulate chatbot backend
npm install chatbot-whatsapp-backend-packagenpm install .
npm run build.
npm link.
npm link chatbot-whatsapp-backend-package.
sh
import { WhatsAppPackageMiddlewareAuth, WhatsappPackageBackendController } from 'chatbot-whatsapp-backend-package';
`
#### Creating Route file
- Create a route file to define the endpoints for WhatsApp webhook verification and message handling. By setting up these routes, you allow the application to listen for incoming webhook events from WhatsApp and process them accordingly.
`typescript
class WhatsAppMessageRoute implements IRoutes {
public path = /${ROUTES.v1}webhook;
public router = Router();
private whatsAppMessageController = new WhatsAppMessageController();
constructor() {
this.initializeRoutes();
}
private initializeRoutes() {
this.router.get(
${this.path},
this.whatsAppMessageController.verifyWebhook
);
this.router.post(
${this.path},
whatsappAuthentication,
this.whatsAppMessageController.whatsappCallbackHandler
);
}
}
export default WhatsAppMessageRoute;
`
#### Creating Controller file
- Create a controller file to handle WhatsApp webhook verification and message processing. And need to import essential components from the npm package, such as WhatsappPackageBackendController, to manage these tasks efficiently.
`typescript
import { WhatsappPackageBackendController } from "chatbot-whatsapp-backend-package";
class WhatsAppMessageController {
private messageService: MessageService = new MessageService();
private messageController: MessageController = new MessageController();
private dbInstance: DatabaseConnection = new DatabaseConnection();
// handling the WhatsApp webhook verification and callback events
private whatsappPackageBackendController =
new WhatsappPackageBackendController(
logger,
handleError,
HttpException,
this.dbInstance,
this.messageService,
this.messageController,
MessengerSchema,
CustomerSchema,
WhatsappUserConversationSchema
);
// Verify the webhook
public verifyWebhook = async (req: Request, res: Response) => {
await this.whatsappPackageBackendController.verifyWebhook(req, res);
};
// Creates a new text message and sends it
public whatsappCallbackHandler = async (req: Request, res: Response) => {
await this.whatsappPackageBackendController.whatsappCallbackHandler(
req,
res
);
};
}
export default WhatsAppMessageController;
`
#### Creating Athentication middleware
- Create an authentication middleware to secure the WhatsApp webhook requests. And need to import WhatsAppPackageMiddlewareAuth from npm package. This middleware ensures that only authorized requests are processed by verifying the incoming webhook calls against the WhatsApp API.
`typescript
import { WhatsAppPackageMiddlewareAuth } from "chatbot-whatsapp-backend-package";
const cache = new NodeCache();
// Middleware to authenticate WhatsApp webhook requests
export const whatsappAuthentication = async (
req: Request,
res: Response,
next: NextFunction
) => {
const dbInstance = new DatabaseConnection();
await WhatsAppPackageMiddlewareAuth(
req,
res,
next,
dbInstance,
ClientModel,
MessengerSettingSchema,
cache,
handleError
);
};
``