chatbot-whatsapp-backend-package
v1.0.0
Published
This is a package for the econsulate chatbot backend
Readme
Econ Chatbot Whatsapp Backend Package
This npm package offers a robust solution for integrating WhatsApp with chatbot systems. It handles webhook verification, processes incoming messages, manages conversation sessions, and stores data in a database. With flexible customization options, it ensures seamless communication between WhatsApp and chatbots.
Table of Contents
Features
Webhook Verification
Verifies WhatsApp webhook requests to establish secure communication with the WhatsApp API.
Message Processing
Processes incoming WhatsApp messages and prepares them for handling by the chatbot.
Conversation Management
Manages conversation sessions, including creation, tracking, and continuity of conversations.
Data Persistence
Saves conversation and message metadata into a database, ensuring conversation history is stored
Message Conversion
Converts bot-generated messages into a format suitable for WhatsApp before sending.
WhatsApp Message Sending
Sends processed bot responses back to WhatsApp users through the WhatsApp API.
Installation
- Download Github repository (URL)
- Install Modules
npm install. - Compile the Node Module
npm run build. - Link build files with the global NPM packages
npm link. - Insert package into another repository
npm link chatbot-whatsapp-backend-package.
Usage
Importing components
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.
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.
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
WhatsAppPackageMiddlewareAuthfrom npm package. This middleware ensures that only authorized requests are processed by verifying the incoming webhook calls against the WhatsApp API.
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
);
};For Contributors
For more details on development practices, refer to Developer Guide.
License
TBD
Subscription Plan
TBD
