pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
14.62k stars 3.48k forks source link

No Reply If I Scan Qr Code From Whatsapp Business #1745

Closed El3omda closed 11 months ago

El3omda commented 1 year ago

Is there an existing issue for this?

Describe the bug

When I Scan QR Code And Send Any Message There Is No Reply Messages Just Be Seen

Expected behavior

Reply With Own Typed Message Which I Have Add To app File

Steps to Reproduce the Bug or Issue

When I Scan QR Code And Send Any Message There Is No Reply Messages Just Be Seen

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Windows

Additional context

No response

mraydok commented 1 year ago

After scanning the qr on whatsapp, it says "syncing. keep the app open". Message is not sent.

smilingOrange commented 1 year ago

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the bug

When I Scan QR Code And Send Any Message There Is No Reply Messages Just Be Seen

Expected behavior

Reply With Own Typed Message Which I Have Add To app File

Steps to Reproduce the Bug or Issue

When I Scan QR Code And Send Any Message There Is No Reply Messages Just Be Seen

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Windows

Additional context

No response

Share your code.

El3omda commented 1 year ago

This Is My Code

const {
  Client,
  MessageMedia,
  LocalAuth,
  Buttons,
  List,
} = require("whatsapp-web.js");
const express = require("express");
const EventEmitter = require("events");
const emitter = new EventEmitter();
emitter.setMaxListeners(50);
const { body, validationResult } = require("express-validator");
const socketIO = require("socket.io");
const qrcode = require("qrcode");
const http = require("http");
const fs = require("fs");
const { phoneNumberFormatter } = require("./helpers/formatter");
const fileUpload = require("express-fileupload");
const axios = require("axios");
const mime = require("mime-types");

const port = process.env.PORT || 8000;

const app = express();
const server = http.createServer(app);
const io = socketIO(server);

app.use(express.json());
app.use(
  express.urlencoded({
    extended: true,
  })
);

/**
 * BASED ON MANY QUESTIONS
 * Actually ready mentioned on the tutorials
 *
 * Many people confused about the warning for file-upload
 * So, we just disabling the debug for simplicity.
 */
app.use(
  fileUpload({
    debug: false,
  })
);

app.get("/", (req, res) => {
  res.sendFile("index.html", {
    root: __dirname,
  });
});

const client = new Client({
  restartOnAuthFail: true,
  puppeteer: {
    headless: true,
    args: [
      "--no-sandbox",
      "--disable-setuid-sandbox",
      "--disable-dev-shm-usage",
      "--disable-accelerated-2d-canvas",
      "--no-first-run",
      "--no-zygote",
      "--single-process", // <- this one doesn't works in Windows
      "--disable-gpu",
    ],
  },
  authStrategy: new LocalAuth(),
});

// Support No

// const supportNo = phoneNumberFormatter("966507610054");

client.on("message", (msg) => {
  if (msg.body == "test") {
    console.log(msg._data);
  }

  if (msg.body == "B") {
  }

  const section1 = {
    title: "",
    rows: [
      {
        title: "اعطال فنية",
        id: "test-2",
      },
      {
        title: "برمجة",
      },
      // {
      //   title: "شراء منتجات",
      //   id: "test-4",
      // },
      {
        title: "أخري",
        id: "test-5",
      },
    ],
  };

  const list1 = new List(
    "انا روبوت الدردشة من سمارتا لحلول المنزل الذكي ،\n ويمكنني مساعدتك في رفع بلاغات الاعطال وبرمجة منزلك الذكي",
    "الخيارات",
    [section1],
    "مرحبا بك 👋",
    "footer"
  );

  const list2 = new List(
    " روبوت الدردشة من سمارتا لحلول المنزل الذكي ،\n ويمكنني مساعدتك في رفع بلاغات الاعطال وبرمجة منزلك الذكي",
    "الخيارات",
    [section1],
    "مرحبا بك 👋",
    "footer"
  );

  const list3 = new List(
    " روبوت الدردشة من سمارتا لحلول المنزل الذكي ،\n ويمكنني مساعدتك في رفع بلاغات الاعطال وبرمجة منزلك الذكي",
    "الخيارات",
    [section1],
    "مرحبا بك 👋",
    "footer"
  );

  const list4 = new List(
    " روبوت الدردشة من سمارتا لحلول المنزل الذكي ،\n ويمكنني مساعدتك في رفع بلاغات الاعطال وبرمجة منزلك الذكي",
    "الخيارات",
    [section1],
    " مرحبا بك 👋",
    "footer"
  );

  const list5 = new List(
    "انا روبوت الدردشة من سمارتا لحلول المنزل الذكي \n، ويمكنني مساعدتك في رفع بلاغات الاعطال وبرمجة منزلك الذكي",
    "الخيارات",
    [section1],
    " مرحبا بك 👋",
    "footer"
  );

  const list500 = new List(
    "مرحبا مجدداً \nكيف يمكنني مساعدتك ؟",
    "الخيارات",
    [section1],
    "القائمة الرئيسية",
    "footer"
  );

  const section2 = {
    title: "",
    rows: [
      {
        title: "مفتاح الإنارة الذكي",
        id: "test-6",
      },
      {
        title: "قفل الدخول الذكى",
      },
      {
        title: "لوحة الجرس",
        id: "test-7",
      },
      {
        title: "القائمة الرئيسية",
        id: "test-8",
      },
    ],
  };

  const pdf1 = MessageMedia.fromFilePath('./files/جهاز التحكم بالتلفاز والتكييف - IR.pdf');
  const pdf2 = MessageMedia.fromFilePath('./files/بوابة التحكم بأجهزة المنزل الذكي  Smart Gateway.pdf');
  const pdf3 = MessageMedia.fromFilePath('./files/حساس الحركة PIR Motion Sensor.pdf');
  const pdf4 = MessageMedia.fromFilePath('./files/حساس حركة الأبواب والنوافذ - Door and window motion sensor.pdf');
  const pdf5 = MessageMedia.fromFilePath('./files/مفتاح الانارة الذكي Smart Light Switch.pdf');
  const pdf6 = MessageMedia.fromFilePath('./files/مفتاح الأنماط الذكية Smart Scene Switch.pdf');
  const pdf7 = MessageMedia.fromFilePath('./files/شاشات المنزل الذكي Smart Screen.pdf');

  const list6 = new List(
    " اختار نوع الجهاز",
    "الاجهزه",
    [section2],
    "",
    "footer"
  );

  const section3 = {
    title: "",
    rows: [
      {
        title: "بوابة المنزل الذكي",
        id: "test-8",
      },
      {
        title: "مفتاح الانارة الذكي",
      },
      {
        title: "مفتاح الانماط الذكية",
        id: "test-9",
      },
      {
        title: "حساس حركة الابواب و النوافذ",
        id: "test-10",
      },
      {
        title: "حساس الحركة",
        id: "test-11",
      },
      {
        title: "جهاز التحكم بالتلفاز و التكيف IR",
        id: "test-12",
      },
      {
        title: "شاشة التحكم الذكية",
        id: "test-133",
      },
      {
        title: "قفل الدخول الذكي",
        id: "test-13",
      },
      {
        title: "طلب زيارة برمجة المنزل الذكي",
        id: "test-14",
      },
      {
        title: "القائمة الرئيسية",
        id: "test-15",
      },
    ],
  };

  const list7 = new List(
    " ",
    "عرض الاجهزة",
    [section3],
    "اختار نوع الجهاز",
    "footer"
  );

  if (msg.body == "سلام عليكم") {
    client.sendMessage(msg._data.from, list1);
  }

  if (msg.body == "السلام عليكم") {
    client.sendMessage(msg._data.from, list1);
  }

  if (msg.body == "السلام عليكم و رحمة الله و بركاتة") {
    client.sendMessage(msg._data.from, list1);
  }

  if (msg.body == "السلام عليكم و رحمه الله و بركاته") {
    client.sendMessage(msg._data.from, list1);
  }

  if (msg.body == "السلام عليكم و رحمه الله") {
    client.sendMessage(msg._data.from, list1);
  }

  if (msg.body == "اهلا" || msg.body == "أهلا" || msg.body == "أهلاً") {
    client.sendMessage(msg._data.from, list2);
  }

  if (msg.body == "صباح الخير") {
    client.sendMessage(msg._data.from, list3);
  }

  if (msg.body == "مساء الخير") {
    client.sendMessage(msg._data.from, list4);
  }

  if (msg.body == "مرحبا") {
    client.sendMessage(msg._data.from, list5);
  }

  if (msg.body == "القائمة الرئيسية") {
    client.sendMessage(msg._data.from, list500);
  }

  if (msg.body == "اعطال فنية") {
    client.sendMessage(msg._data.from, list6);
  } else if (msg.body == "برمجة") {
    client.sendMessage(msg._data.from, list7);
  } else if (msg.body == "شراء منتجات") {
    // msg.reply("تفضل بزيارة متجرنا الالكتروني :\n رابط المتجر");
  }

  if (msg.body == "أخري") {
    msg.reply(
      "تفضل بترك رسالتك وسيقوم فريق خدمة العملاء بالتواصل معك بأقرب وقت ممكن."
    );
    client.sendMessage(
      supportNo,
      'هناك عميل جديد قام بارسال بياناته'
    );
  } else if (msg.body == "بوابة المنزل الذكي") {
    msg.reply(
      "بامكانك برمجة بوابة المنزل الذكي من خلال الطرق التالية:\n1 - الدليل الارشادي المرفق\n2 - اطلع على فيديو شرح برمجة بوابة المنزل الذكي على قناة اليوتيوب: \nhttps://youtu.be/ZGdB4eFexMU\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    msg.reply(pdf2);
  } 
  if (msg.body == "مفتاح الانارة الذكي") {
    msg.reply(
      "بامكانك برمجة مفتاح الانارة الذكي  من خلال الطرق التالية:\n1 - الدليل الارشادي المرفق\n2 - اطلع على فيديو شرح برمجة مفتاح الانارة الذكي على قناة اليوتيوب: \nhttps://youtu.be/Ag1e7GfzpNk\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    msg.reply(pdf5);
  }

  if (msg.body == "مفتاح الانماط الذكية") {
    msg.reply(
      "بامكانك برمجة مفتاح الانماط الذكية  من خلال الطرق التالية:\n1 - الدليل الارشادي المرفق\n2 - اطلع على فيديو شرح برمجة مفتاح الانماط الذكية على قناة اليوتيوب: \nhttps://youtu.be/ins658T5FIs\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    msg.reply(pdf6);
  } else if (msg.body == "حساس حركة الابواب و النوافذ") {
    msg.reply(
      "بامكانك برمجة حساس حركة الابواب و النوافذ  من خلال الطرق التالية:\n1 - الدليل الارشادي المرفق\n2 - اطلع على فيديو شرح برمجة حساس حركة الابواب و النوافذ على قناة اليوتيوب: \nhttps://youtu.be/i9Db4MWKqPI\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    msg.reply(pdf4);
  } else if (msg.body == "حساس الحركة") {
    msg.reply(
      "بامكانك برمجة حساس الحركة من خلال الطرق التالية:\n1 - الدليل الارشادي المرفق\n2 - اطلع على فيديو شرح برمجة حساس الحركة على قناة اليوتيوب: \nhttps://youtu.be/ins658T5FIs\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    msg.reply(pdf3);
  }
  if (msg.body == "جهاز التحكم بالتلفاز و التكيف IR") {
    msg.reply(
      "بامكانك برمجة جهاز التحكم بالتلفاز و التكيف IR من خلال الطرق التالية:\n1 - الدليل الارشادي المرفق\n2 - اطلع على فيديو شرح جهاز التحكم بالتلفاز و التكيف IR على قناة اليوتيوب: \nhttps://youtu.be/Evt5I9L8eRA\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    msg.reply(pdf1);
  }
  if (msg.body == "شاشة التحكم الذكية") {
    msg.reply(
      "بامكانك برمجة شاشة التحكم الذكية IR من خلال الطرق التالية:\nالدليل الارشادي المرفق\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    msg.reply(pdf7);
  }
  if (msg.body == "قفل الدخول الذكي") {
    msg.reply(
      "نأمل ارسال صورة القفل الذكي ليتم تزويدكم بالدليل الارشادي ورابط برمجة القفل بقناة اليوتيوب\nاو بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
  } else if (msg.body == "طلب زيارة برمجة المنزل الذكي") {
    msg.reply(
      " بامكانك طلب خدمة البرمجة المدفوعة بالضغط على الرابط أدناه:\nhttps://smarta.com.sa/service"
    );
    // client.sendMessage(supportNo, "هناك عميل جديد قام بطلب زيارة المختص");
  }  else if (msg.body == "مفتاح الإنارة الذكي") {
    msg.reply(
      "سوف يقوم الفني بالتواصل معك الرجاء تزويدنا بالبيانات التالية :\nالأسم\nرقم التواصل\nاسم المشروع\nرقم المبني\nرقم الوحدة"
    );
    // client.sendMessage(supportNo, "هناك عميل جديد قام بطلب زيارة المختص");
  }  else if (msg.body == "قفل الدخول الذكى") {
    msg.reply(
      "سوف يقوم الفني بالتواصل معك الرجاء تزويدنا بالبيانات التالية :\nالأسم\nرقم التواصل\nاسم المشروع\nرقم المبني\nرقم الوحدة"
    );
    // client.sendMessage(supportNo, "هناك عميل جديد قام بطلب زيارة المختص");
  }  else if (msg.body == "لوحة الجرس") {
    msg.reply(
      "سوف يقوم الفني بالتواصل معك الرجاء تزويدنا بالبيانات التالية :\nالأسم\nرقم التواصل\nاسم المشروع\nرقم المبني\nرقم الوحدة"
    );
    // client.sendMessage(supportNo, "هناك عميل جديد قام بطلب زيارة المختص");
  } else if (msg.body == "جهاز أخر") {
    msg.reply(
      "سوف يقوم الفني بالتواصل معك الرجاء تزويدنا بالبيانات التالية :\nالأسم\nرقم التواصل\nاسم المشروع\nرقم المبني\nرقم الوحدة"
    );
    // client.sendMessage(supportNo, "هناك عميل جديد قام بطلب زيارة المختص");
  }

  // NOTE!
  // UNCOMMENT THE SCRIPT BELOW IF YOU WANT TO SAVE THE MESSAGE MEDIA FILES
  // Downloading media
  // if (msg.hasMedia) {
  //   msg.downloadMedia().then(media => {
  //     // To better understanding
  //     // Please look at the console what data we get
  //     console.log(media);

  //     if (media) {
  //       // The folder to store: change as you want!
  //       // Create if not exists
  //       const mediaPath = './downloaded-media/';

  //       if (!fs.existsSync(mediaPath)) {
  //         fs.mkdirSync(mediaPath);
  //       }

  //       // Get the file extension by mime-type
  //       const extension = mime.extension(media.mimetype);

  //       // Filename: change as you want!
  //       // I will use the time for this example
  //       // Why not use media.filename? Because the value is not certain exists
  //       const filename = new Date().getTime();

  //       const fullFilename = mediaPath + filename + '.' + extension;

  //       // Save to file
  //       try {
  //         fs.writeFileSync(fullFilename, media.data, { encoding: 'base64' });
  //         console.log('File downloaded successfully!', fullFilename);
  //       } catch (err) {
  //         console.log('Failed to save the file:', err);
  //       }
  //     }
  //   });
  // }
});

client.initialize();

// Socket IO
io.on("connection", function (socket) {
  socket.emit("message", "يتم الاتصال");

  client.on("qr", (qr) => {
    console.log("QR RECEIVED", qr);
    qrcode.toDataURL(qr, (err, url) => {
      socket.emit("qr", url);
      socket.emit("message", "من فضلك قم بمسح الرمز");
    });
  });

  client.on("ready", () => {
    socket.emit("ready", "تم الربط بنجاح");
    socket.emit("message", "تم الربط بنجاح");
  });

  client.on("authenticated", () => {
    socket.emit("authenticated", "تم الربط بنجاح");
    socket.emit("message", "تم الربط بنجاح");
    console.log("AUTHENTICATED");
  });

  client.on("auth_failure", function (session) {
    socket.emit("message", "تتم اعادة المحاولة");
  });

  client.on("disconnected", (reason) => {
    socket.emit("message", "تم قطع الاتصال");
    client.destroy();
    client.initialize();
  });
});

const checkRegisteredNumber = async function (number) {
  const isRegistered = await client.isRegisteredUser(number);
  return isRegistered;
};

// Send message
app.post(
  "/send-message",
  [body("number").notEmpty(), body("message").notEmpty()],
  async (req, res) => {
    const errors = validationResult(req).formatWith(({ msg }) => {
      return msg;
    });

    if (!errors.isEmpty()) {
      return res.status(422).json({
        status: false,
        message: errors.mapped(),
      });
    }

    const number = phoneNumberFormatter(req.body.number);
    const message = req.body.message;

    const isRegisteredNumber = await checkRegisteredNumber(number);

    if (!isRegisteredNumber) {
      return res.status(422).json({
        status: false,
        message: "The number is not registered",
      });
    }

    client
      .sendMessage(number, message)
      .then((response) => {
        res.status(200).json({
          status: true,
          response: response,
        });
      })
      .catch((err) => {
        res.status(500).json({
          status: false,
          response: err,
        });
      });
  }
);

// Send media
app.post("/send-media", async (req, res) => {
  const number = phoneNumberFormatter(req.body.number);
  const caption = req.body.caption;
  const fileUrl = req.body.file;

  // const media = MessageMedia.fromFilePath('./image-example.png');
  // const file = req.files.file;
  // const media = new MessageMedia(file.mimetype, file.data.toString('base64'), file.name);
  let mimetype;
  const attachment = await axios
    .get(fileUrl, {
      responseType: "arraybuffer",
    })
    .then((response) => {
      mimetype = response.headers["content-type"];
      return response.data.toString("base64");
    });

  const media = new MessageMedia(mimetype, attachment, "Media");

  client
    .sendMessage(number, media, {
      caption: caption,
    })
    .then((response) => {
      res.status(200).json({
        status: true,
        response: response,
      });
    })
    .catch((err) => {
      res.status(500).json({
        status: false,
        response: err,
      });
    });
});

const findGroupByName = async function (name) {
  const group = await client.getChats().then((chats) => {
    return chats.find(
      (chat) => chat.isGroup && chat.name.toLowerCase() == name.toLowerCase()
    );
  });
  return group;
};

// Send message to group
// You can use chatID or group name, yea!
app.post(
  "/send-group-message",
  [
    body("id").custom((value, { req }) => {
      if (!value && !req.body.name) {
        throw new Error("Invalid value, you can use `id` or `name`");
      }
      return true;
    }),
    body("message").notEmpty(),
  ],
  async (req, res) => {
    const errors = validationResult(req).formatWith(({ msg }) => {
      return msg;
    });

    if (!errors.isEmpty()) {
      return res.status(422).json({
        status: false,
        message: errors.mapped(),
      });
    }

    let chatId = req.body.id;
    const groupName = req.body.name;
    const message = req.body.message;

    // Find the group by name
    if (!chatId) {
      const group = await findGroupByName(groupName);
      if (!group) {
        return res.status(422).json({
          status: false,
          message: "No group found with name: " + groupName,
        });
      }
      chatId = group.id._serialized;
    }

    client
      .sendMessage(chatId, message)
      .then((response) => {
        res.status(200).json({
          status: true,
          response: response,
        });
      })
      .catch((err) => {
        res.status(500).json({
          status: false,
          response: err,
        });
      });
  }
);

// Clearing message on spesific chat
app.post("/clear-message", [body("number").notEmpty()], async (req, res) => {
  const errors = validationResult(req).formatWith(({ msg }) => {
    return msg;
  });

  if (!errors.isEmpty()) {
    return res.status(422).json({
      status: false,
      message: errors.mapped(),
    });
  }

  const number = phoneNumberFormatter(req.body.number);

  const isRegisteredNumber = await checkRegisteredNumber(number);

  if (!isRegisteredNumber) {
    return res.status(422).json({
      status: false,
      message: "The number is not registered",
    });
  }

  const chat = await client.getChatById(number);

  chat
    .clearMessages()
    .then((status) => {
      res.status(200).json({
        status: true,
        response: status,
      });
    })
    .catch((err) => {
      res.status(500).json({
        status: false,
        response: err,
      });
    });
});

server.listen(port, function () {
  console.log("App running on *: " + port);
});
smilingOrange commented 1 year ago

Try running "client.initialise()" after the QR code is scanned.

If that des not work, try running the exmaple.js script. If that is working fine then the issue is with your own code rather than whatsapp-web.

El3omda commented 1 year ago

not worked when i tried to make a new npm i whatsapp-web.js as a new project and tried to scan qr code with whatsapp business account and send "!ping" the whatsapp Which Connected to Whatsapp-web.js not respond too

rgblumetti commented 1 year ago

Same error here on a working production project. Stopped sending messages even after re-scanning QR Code. Is there a breaking change on WhatsApp? Using WhatsApp Business here.

isabellacalfa commented 1 year ago

same here... it was working, but stopped 10h ago. not pinging.

mraydok commented 1 year ago

I watched what was returned in chrome by doing headless:false in puppeteer and got such a result : capture The message is not sent and the send button disappears. Also, whatsapp is not syncing. This problem started yesterday.

rgblumetti commented 1 year ago

Workaround to solve this with this issue comment --> [https://github.com/pedroslopez/whatsapp-web.js/issues/1741#issuecomment-1284715884]

budisatrio85 commented 1 year ago

Workaround to solve this with this issue comment --> [https://github.com/[/issues/1741](https://github.com/pedroslopez/whatsapp-web.js/issues/1741)#issuecomment-1284715884]

works for me, thanks for pointing out

JonDeev commented 1 year ago

the solution for me was to update to the latest version of whatsapp-web.js the 1.18.0

Crv5heR commented 1 year ago

use "whatsapp-web.js": "^1.13.1", It's the most stable version for me