Back

Appwrite Function X Pupeteer ??

  • 0
  • Functions
  • Cloud
Akash Srinivasan
11 Mar, 2024, 16:04

Hey can i create a function which uses whatsapp web js (which under the hood opens chromium browser via pupeteer) because getting this err:-

TypeScript
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:-

TypeScript
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));
};
TL;DR
Developers are trying to create a function using WhatsApp Web JS, which opens a Chromium browser via Puppeteer, but they are encountering an error related to launching the browser process. The solution involves setting up the build command to install Chromium in the functions container and updating WhatsAppClient/Puppeteer to reference the new location of Chromium. Code snippet provided for reference: - Initialize WhatsApp client with specific authentication strategy - Use QR code to set up client - Handle incoming messages and send messages - Implement error handling for message sending Ensure correct environment variables, build settings, and dependencies are in place for successful implementation.
Kenny
11 Mar, 2024, 16:09

You'll need to setup your build command to install chromium in your functions container, then set your whatsappclient/puppeteer to reference that new location of chromium.

Kenny
11 Mar, 2024, 16:09

I believe the function containers are alpine linux.

Kenny
11 Mar, 2024, 16:11

https://github.com/dishwasher-detergent/screenshot

Here is something I made that uses puppeteer, the two pages that would mean the most to you are the browser.ts file, and the appwrite.json file.

Evdog
11 Mar, 2024, 16:23

Check out https://discord.com/channels/564160730845151244/1215654642353176656, I have a working sample here, just ensure you have the correct environment variables and build settings along with the dependency declared in package.json and you should be good

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more