WhiskeySockets / Baileys

Lightweight full-featured typescript/javascript WhatsApp Web API
https://baileys.whiskeysockets.io/
MIT License
3.63k stars 1.23k forks source link

[BUG] stream:error code='515' after entering pairing code on phone #313

Closed olalonde closed 1 year ago

olalonde commented 1 year ago

Describe the bug

When entering pairing code on phone, I am getting a 515 code error and the connection closes.

To Reproduce

Baileys 6.4.0. Full reproduction code (just edit BOT_NUMBER):

#!/bin/env node
// Just a single file to help explore/debug the WA protocol and Baileys library
import Baileys, {
  makeWASocket,
  fetchLatestBaileysVersion,
  makeCacheableSignalKeyStore,
  makeInMemoryStore,
  useMultiFileAuthState,
  WAMessageContent,
  WAMessageKey,
  WAVersion,
  AuthenticationState,
  delay,
  isJidBroadcast, // proto,
} from "@whiskeysockets/baileys";
import { mkdir } from "fs/promises";
import path from "path";
import P from "pino";

// enter your phone here
const BOT_NUMBER = "14150001234";
const DATA_PATH = "./data/explore";

export function dbg(val: any) {
  console.dir(val, { depth: null });
}

const logger = P({
  level: "trace",
  transport: {
    target: "pino-pretty",
    options: {
      colorize: true,
    },
  },
});

type Store = ReturnType<typeof makeInMemoryStore>;

interface StartSockConfig {
  store: Store;
  version: WAVersion;
  auth: AuthenticationState;
  saveCreds?: () => Promise<void>;
}

const asyncNoop = async () => {};

// type Sock = ReturnType<typeof makeWASocket>;
async function startSock({
  store,
  version,
  auth,
  saveCreds = asyncNoop,
}: StartSockConfig) {
  const sock = makeWASocket({
    version,
    logger,
    printQRInTerminal: false,
    // using MD api instead
    mobile: false,
    // syncFullHistory: true,
    auth: {
      ...auth,
      /** caching makes the store faster to send/recv messages */
      keys: makeCacheableSignalKeyStore(auth.keys, logger),
    },
    // msgRetryCounterCache,
    generateHighQualityLinkPreview: true,
    shouldIgnoreJid: (jid) => isJidBroadcast(jid),
  });

  // store?.bind(sock.ev);

  // Pairing code for Web clients
  if (!sock.authState.creds.registered) {
    console.log(
      "Waiting 5s for socket to be ready, otherwise we get 428 Precondition Required error and the socket disconnects..."
    );
    await delay(5000);
    const phoneNumber = BOT_NUMBER;
    console.log({ phoneNumber });
    console.log("Requesting pairing code...");
    const code = await sock.requestPairingCode(phoneNumber);
    console.log(`Pairing code: ${code}`);
  }

  // the process function lets you process all events that just occurred
  // efficiently in a batch
  sock.ev.process(async (event) => {
    dbg({ event });
    // credentials updated -- save them
    if (event["creds.update"]) {
      await saveCreds();
    }
  });
}

export async function createBot() {
  // fetch latest version of WA Web
  const { version, isLatest } = await fetchLatestBaileysVersion();
  console.log(`using WA v${version.join(".")}, isLatest: ${isLatest}`);

  // the store maintains the data of the WA connection in memory
  // can be written out to a file & read from it
  const store = makeInMemoryStore({ logger });
  const storePath = path.join(DATA_PATH, "store", "store_multi.json");
  // creates path if it doesn't exist
  await mkdir(path.dirname(storePath), { recursive: true });
  store.readFromFile(storePath);
  // save every 10s
  setInterval(() => {
    store.writeToFile(storePath);
  }, 10_000);

  // Load auth state if previously saved
  const { state: auth, saveCreds } = await useMultiFileAuthState(
    path.join(DATA_PATH, "auth")
  );
  // console.log("Loaded auth:");
  // dbg({ auth });

  // start a connection

  return await startSock({ store, version, auth, saveCreds });
}

await createBot();

Expected behavior

There should be no error.

Environment (please complete the following information):

Additional context

See reproduction code.

andresayac commented 1 year ago

retry with

// Pairing code for Web clients
  if (!sock.authState.creds.registered) {
    const phoneNumber = BOT_NUMBER;
    console.log({ phoneNumber });

    await sock.waitForConnectionUpdate((update) => !!update.qr) 

    console.log("Requesting pairing code...");
    const code = await sock.requestPairingCode(phoneNumber);
    console.log(`Pairing code: ${code}`);
  }
olalonde commented 1 year ago

Thanks. That seemed to work.

olalonde commented 1 year ago

Oops, never mind. I just deleted my auth state, "logged out" the device form my phone and tried again. Same error as before.

edit: It does avoid the 5s delay hack I had before though, but doesn't solve the issue.

andresayac commented 1 year ago

Oops, never mind. I just deleted my auth state, "logged out" the device form my phone and tried again. Same error as before.

edit: It does avoid the 5s delay hack I had before though, but doesn't solve the issue.

it is possible that you share the error that you are presenting

BochilGaming commented 1 year ago

You need to implement reconnect logic because when the connection is successful to log in the first time, you need to restart the connection https://github.com/WhiskeySockets/Baileys/blob/555cc9b3145ea2a94aeb34eec30eb966154b16f8/Example/example.ts#L149-L162

olalonde commented 1 year ago

Ok thanks, that works.