### Install node-zalo-bot Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Installs the node-zalo-bot package using npm. This is the first step to integrate the library into your Node.js project. ```bash npm install node-zalo-bot ``` -------------------------------- ### Example: Zalo Bot with Webhook Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code An example of setting up a Zalo bot with webhooks using Express.js. It includes middleware for JSON parsing, a route for receiving updates, and webhook registration. ```javascript const ZaloBot = require('node-zalo-bot'); const express = require('express'); require('dotenv').config(); const WEBHOOK_URL = "https://your-ngrok-or-domain/webhook"; const WEBHOOK_SECRET_TOKEN = "YOUR_WEBHOOK_SECRET_TOKEN"; const app = express(); const bot = new ZaloBot(process.env.BOT_TOKEN, { polling: false, // Disable polling when using webhook }); // Middleware to parse JSON app.use(express.json()); // Route to receive webhook from Zalo app.post('/webhook', (req, res) => { if (req.headers['x-bot-api-secret-token'] !== WEBHOOK_SECRET_TOKEN) { console.log('Unauthorized request', req.headers); return res.status(403).json({ message: "Unauthorized" }); } const update = req.body; bot.processUpdate(update); res.sendStatus(200); }); // Handle messages bot.on('message', (msg) => { bot.sendMessage(msg.chat.id, 'Xin chào!'); }); // Register webhook with Zalo bot.setWebHook(WEBHOOK_URL, { secret_token: WEBHOOK_SECRET_TOKEN }).then(() => { console.log('Webhook configured successfully'); }).catch((error) => { console.error('Error registering webhook:', error); }); // Start server const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }); ``` -------------------------------- ### Example: Zalo Bot with Polling Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code A complete example demonstrating how to set up and run a Zalo bot using the polling method. It includes basic message handling and text response. ```javascript const ZaloBot = require('node-zalo-bot'); require('dotenv').config(); const bot = new ZaloBot(process.env.BOT_TOKEN, { polling: true }); bot.on('message', (msg) => { bot.sendMessage(msg.chat.id, 'Chào bạn!'); }); bot.onText(//start (.+)/, (msg, match) => { bot.sendMessage(msg.chat.id, `Bạn vừa gửi: ${match[1]}`); }); ``` -------------------------------- ### Get Bot Information Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Fetches information about the Zalo bot itself. ```javascript bot.getMe([options]); ``` -------------------------------- ### Get Webhook Information Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Retrieves information about the currently configured webhook. ```javascript bot.getWebHookInfo([options]); ``` -------------------------------- ### Start Polling for Updates Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Initiates the polling mechanism to continuously fetch updates from the Zalo server. This is an alternative to using webhooks. ```javascript bot.startPolling([options]); ``` -------------------------------- ### Get Updates Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Retrieves new updates from the Zalo server, typically used when polling is enabled. ```javascript bot.getUpdates([options]); ``` -------------------------------- ### Configure Environment Variables Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Sets up the necessary environment variables for the Zalo bot, primarily the BOT_TOKEN. This is typically done by creating a .env file. ```dotenv BOT_TOKEN=your_zalo_bot_token ``` -------------------------------- ### Initialize Zalo Bot Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Initializes a new Zalo bot instance using the BOT_TOKEN and configures it for either polling or webhook updates. Requires the 'dotenv' package to load environment variables. ```javascript const ZaloBot = require('node-zalo-bot'); require('dotenv').config(); const bot = new ZaloBot(process.env.BOT_TOKEN, { polling: true, // or false if using webhook }); ``` -------------------------------- ### Listen for Messages Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Sets up an event listener to receive and process incoming messages from Zalo users. The callback function receives the message object and metadata. ```javascript bot.on('message', (msg, metadata) => { console.log('Received message:', msg); }); ``` -------------------------------- ### Send Chat Action (Typing) Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Indicates that the bot is currently typing a response to the user. This provides a better user experience by showing activity. ```javascript // Show typing indicator bot.sendChatAction(chatId, 'typing'); ``` -------------------------------- ### Listen for Text Messages with Regex Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Listens for text messages that match a specific regular expression. The matched groups are available in the callback function. ```javascript bot.onText(//start (.+)/, (msg, match) => { // match[1] is the part that matches (.+) }); ``` -------------------------------- ### Register Webhook Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Registers a webhook URL with Zalo to receive updates. This requires a publicly accessible URL and optionally a secret token for security. ```javascript bot.setWebHook(url, [options]); ``` -------------------------------- ### Send Text Message Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Sends a text message to a specified chat ID. Optional parameters can be provided for message formatting. ```javascript bot.sendMessage(chatId, 'Hello!'); ``` -------------------------------- ### Delete Webhook Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Removes the previously registered webhook from Zalo. ```javascript bot.deleteWebHook([options]); ``` -------------------------------- ### Check if Polling is Active Source: https://www.npmjs.com/package/node-zalo-bot/index_activetab=code Returns a boolean indicating whether the bot is currently polling for updates. ```javascript bot.isPolling(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.