Baileys icon indicating copy to clipboard operation
Baileys copied to clipboard

"requestPairingCode" Returns Invalid Code

Open joaoporth opened this issue 1 year ago • 10 comments

The function "requestPairingCode" returns a pairing code, but it is invalid. This issue has been tested on more than 10 accounts with the same result. Please investigate and fix the problem.

joaoporth avatar Feb 23 '25 16:02 joaoporth

Investigated and the problem is from your code not baileys

ghost avatar Feb 23 '25 19:02 ghost

I think I found the reason for the error. When I define 'browser' in 'makeWASocket', the code doesn't connect.

joaoporth avatar Feb 23 '25 20:02 joaoporth

Show us how the code looks where you create your connection (makeWASocket)

guiireal avatar Mar 02 '25 14:03 guiireal

@joaoporth Yep, seems to be true.

I was implementing the Pairing and found that if I use the browser config, it does not work. Baileys does generate a CODE, but the standard Whatsapp notification does not arrive.

I will update the exact version of Browser that works here if any.

AkashSaikia avatar Mar 11 '25 12:03 AkashSaikia

any update @AkashSaikia ?

canove avatar Apr 24 '25 19:04 canove

@AkashSaikia please update.

marab2 avatar May 19 '25 11:05 marab2

same issue here i tried, with and without socket browserconfig, with every value I could think of and I didn't manage to make ir work

DanNisenson avatar May 19 '25 15:05 DanNisenson

any update on this?

requestPairingCode is not working for me. returns a code, but always invalid code

"@whiskeysockets/baileys": "^6.7.17",

const sock = makeWASocket({
    version,
    logger,
    printQRInTerminal: false,
    markOnlineOnConnect: false,
    auth: {
        creds: state.creds,
        keys: makeCacheableSignalKeyStore(state.keys, logger),
    },
    defaultQueryTimeoutMs: undefined,
    msgRetryCounterCache,
    generateHighQualityLinkPreview: true,
    browser: Browsers.macOS('Google Chrome'),
    syncFullHistory: true
});


    if (!client.authState.creds.registered) {
        // Request pairing code
        const formattedNumber = phoneNumber.replace(/[^0-9]/g, '');
         // Set the browser to Google Chrome for pairing code request
        client.authState.creds.browser = Browsers.macOS("Google Chrome");
        const code = await client.requestPairingCode(formattedNumber);

}

Yaniv101 avatar May 20 '25 20:05 Yaniv101

I think I found the reason for the error. When I define 'browser' in 'makeWASocket', the code doesn't connect.

solved

wilkdevs avatar May 28 '25 06:05 wilkdevs

any updates

It generates a code, but it is not valid and no WhatsApp notification is received.

const sock = makeWASocket({ version, logger, printQRInTerminal: true, auth: { creds: state.creds, keys: makeCacheableSignalKeyStore(state.keys, logger), }, // browser: ["WhatsApp Desktop", "Firefox", "1.0.0"], connectTimeoutMs: 60000, defaultQueryTimeoutMs: 60000, emitOwnEvents: true, markOnlineOnConnect: true, syncFullHistory: false, });

  // Bağlantı durumunu dinle
  sock.ev.on("connection.update", async (update) => {
    const { connection, lastDisconnect, qr } = update;
    console.log(`[${formattedNumber}] Bağlantı durumu:`, connection);

    if (qr) {
      console.log(`[${formattedNumber}] QR kodu görüntülendi.`);
    }

    if (connection === "close") {
      const statusCode = lastDisconnect?.error?.output?.statusCode;
      console.log(
        `[${formattedNumber}] Bağlantı kapandı. Sebep: ${statusCode}`
      );
      console.log(
        `[${formattedNumber}] Detay:`,
        lastDisconnect?.error?.message
      );

      // Aktif bağlantıyı kaldır
      activeConnections.delete(formattedNumber);

      // Auth klasörünü sil
      try {
        const authPath = path.join(__dirname, "auth_info", formattedNumber);
        if (fs.existsSync(authPath)) {
          fs.rmSync(authPath, { recursive: true, force: true });
          console.log(`[${formattedNumber}] Auth klasörü silindi`);
        }
      } catch (error) {
        console.error(`[${formattedNumber}] Auth klasörü silme hatası:`, error);
      }

      // Yeniden bağlanma mantığı
      if (
        statusCode !== DisconnectReason.loggedOut &&
        statusCode !== DisconnectReason.connectionReplaced
      ) {
        console.log(`[${formattedNumber}] Yeniden bağlanılıyor...`);
        setTimeout(() => {
          startWhatsAppSession(formattedNumber).then(resolve).catch(reject);
        }, 10000);
      } else {
        console.log(
          `[${formattedNumber}] Oturum kapatıldı, yeni oturum gerekli`
        );
        reject(new Error("Oturum sonlandırıldı"));
      }
    } else if (connection === "open") {
      console.log(`[${formattedNumber}] ✅ WhatsApp bağlantısı kuruldu!`);
      await WhatsAppSession.findOneAndUpdate(
        { phoneNumber: formattedNumber },
        { isActive: true, isVerified: true }
      );

      // Event dinleyicileri ekle
      setupEventListeners(sock, formattedNumber);

      // Aktif bağlantıyı kaydet
      activeConnections.set(formattedNumber, sock);
      resolve(sock);
    } else if (connection === "connecting") {
      // Eğer başlangıç kimliği yoksa ve bağlanıyorsa, eşleştirme kodu iste
      if (!state.creds.me) {
        console.log(
          `[${formattedNumber}] İlk bağlantı, kimlik doğrulama gerekli`
        );

        setTimeout(async () => {
          try {
            console.log(`[${formattedNumber}] Kod isteniyor...`);
            if (!sock.authState.creds.registered) {
              console.log(`[${formattedNumber}] Pairing code isteniyor...`);
              const code = await sock.requestPairingCode(formattedNumber);
              if (code) {
                console.log(
                  `[${formattedNumber}] 🔐 GİRİŞ KODU: ${code} 🔐`
                );
                console.log(
                  `[${formattedNumber}] Giriş kodu SMS olarak gönderildi`
                );
              }
            }
          } catch (error) {
            console.error(
              `[${formattedNumber}] Kod gönderme hatası:`,
              error
            );
            if (error.message.includes("429")) {
              console.log(
                `[${formattedNumber}] ⚠️ Çok fazla deneme yapıldı. Lütfen 30 saniye bekleyin.`
              );
            }
          }
        }, 6000);
      }
    }
  });

  // Kimlik bilgilerini kaydet
  sock.ev.on("creds.update", saveCreds);
})

"@whiskeysockets/baileys": "^6.6.0",

TAYFUN-KAYA avatar May 29 '25 11:05 TAYFUN-KAYA

@SheIITear why did you close it?

im having the same issue without initializing the browser. the code is created but invalid for connection.

"@whiskeysockets/baileys": "^6.7.17",

const sock = makeWASocket({
    version,
    logger,
    printQRInTerminal: false,
    markOnlineOnConnect: false,
    auth: {
        creds: state.creds,
        keys: makeCacheableSignalKeyStore(state.keys, logger),
    },
    defaultQueryTimeoutMs: undefined,
    msgRetryCounterCache,
    generateHighQualityLinkPreview: true,
    //browser: Browsers.macOS('Google Chrome'),
    syncFullHistory: true
});


    if (!client.authState.creds.registered) {
               const code = await client.requestPairingCode(formattedNumber);
       }

Yaniv101 avatar May 30 '25 06:05 Yaniv101

@Yaniv101, ### I solved the problem. You can use the following:

const sock = makeWASocket({ version, logger, printQRInTerminal: true, auth: { creds: state.creds, keys: makeCacheableSignalKeyStore(state.keys, logger), }, connectTimeoutMs: 60000, defaultQueryTimeoutMs: 60000, emitOwnEvents: true, markOnlineOnConnect: true, syncFullHistory: false, });

sock.ev.on("creds.update", saveCreds);

if (connection === "connecting") if (!sock.authState.creds.registered) { console.log([${formattedNumber}] waiting for code...); const code = await sock.requestPairingCode(formattedNumber); if (code) { console.log( [${formattedNumber}] 🔐 Code: ${code} 🔐 ); } }

"@whiskeysockets/baileys": "^6.7.18",

TAYFUN-KAYA avatar May 30 '25 09:05 TAYFUN-KAYA

Working great now Thank you

Yaniv101 avatar May 30 '25 16:05 Yaniv101