Rank 3: Container
Hey can i create a function which uses whatsapp web js (which under the hood opens chromium browser via pupeteer) because getting this err:-
Error: Failed to launch the browser process! spawn /usr/local/server/src/function/node_modules/puppeteer/.local-chromium/linux-982053/chrome-linux/chrome ENOENT
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
CODE:-
const qrcode = require("qrcode-terminal");
module.exports = async (context) => { const whatsappclient = new Client({ authStrategy: new LocalAuth() // puppeteer: { args: ['--no-sandbox', '--disable-setuid-sandbox'] } });
whatsappclient.initialize();
whatsappclient.on("qr", (qr) => { qrcode.generate(qr, { small: true }); context.log(qr); });
whatsappclient.on("message", async (msg) => { context.log("MESSAGE:- " + msg.body); });
if (req.method === 'GET') { if (req.path === '/message') { const phoneNumber = req.query.phoneNumber; const message = req.query.message; const chatId = phoneNumber + "@c.us";
try { // Wait for client to be ready before sending message await new Promise((resolve) => whatsappclient.on("ready", resolve));
const response = await whatsappclient.sendMessage(chatId, message); context.log("Message sent to:", phoneNumber, chatId); context.log(response); return res.send("Message sent successfully."); } catch (err) { context.error("Error sending message:", err); return res.send("Error sending message."); }
} } // Consider returning a placeholder response to indicate processing // (optional, depending on your needs) // return new Promise((resolve) => setTimeout(resolve, 1000));};