yisbug / whatsapp-nodejs

This is a library based on whatsapp androd latest version, you can use it build your own app.
36 stars 12 forks source link
whatsapp yowsup

For business cooperation or customization needs, please contact the following email: yisbug@qq.com

About

中文

English

This is a WhatsApp communication protocol SDK based on WhatsApp Android client, which means that you can build any WhatsApp client based on this SDK, or complete some specific requirements.

The latest official version: https://www.whatsapp.com/android/

Latest SDK version: 2.22.24.79

Updates

2022/11/23

2022/11/21

How to update:

rm -rf node_modules
npm i // or yarn

And the repair file of the patches directory will be automatically moved to the node_modules.

Usage

Installation

requires:

You can use npm or yarn to install:

# npm install whatsapp-nodejs

For some reasons, the package is removed by npm, so you need to clone the local repository, and then use the npm link command to use this sdk.

git clone git@github.com:yisbug/whatsapp-nodejs.git
cd whatsapp-nodejs
npm install
cd ../your project
npm link ../whatsapp-nodejs

Get SMS

const { Whatsapp } = require('whatsapp-nodejs');

const main = async () => {
  const whatsapp = new Whatsapp({
    mongodb: 'mongodb://localhost:27017/whatsapp',
  });
  await whatsapp.init({
    mobile: '8613888888888', // cc is required, for example, china is +86
    proxy: {
      host: '127.0.0.1',
      port: 1080,
      userId: 'test',
      password: 'test',
    },
  });

  let res = null;

  res = await whatsapp.sms();
  // {status:'success'}
  // {status:'error'}
};

main();

Use SMS Code to register

const { Whatsapp } = require('whatsapp-nodejs');

const main = async () => {
  const whatsapp = new Whatsapp({
    mongodb: 'mongodb://localhost:27017/whatsapp',
  });
  await whatsapp.init({
    mobile: '8613888888888', // cc is required, for example, china is +86
    proxy: {
      host: '127.0.0.1',
      port: 1080,
      userId: 'test',
      password: 'test',
    },
  });

  let res = null;
  res = await whatsapp.register({ code: '352-002' });
  // {status:'success'}
  // {status:'error'}
};

main();

Login

const { Whatsapp } = require('whatsapp-nodejs');

const main = async () => {
  const whatsapp = new Whatsapp({
    mongodb: 'mongodb://localhost:27017/whatsapp',
  });
  await whatsapp.init({
    mobile: '8613888888888', // cc is required, for example, china is +86
    proxy: {
      host: '127.0.0.1',
      port: 1080,
      userId: 'test',
      password: 'test',
    },
  });

  let res = null;
  res = await whatsapp.login();
  // {status:'success'}
  // {status:'error'}
};

main();

Send Text Message

const { Whatsapp } = require('whatsapp-nodejs');

const main = async () => {
  const whatsapp = new Whatsapp({
    mongodb: 'mongodb://localhost:27017/whatsapp',
  });
  await whatsapp.init({
    mobile: '8613888888888', // cc is required, for example, china is +86
    proxy: {
      host: '127.0.0.1',
      port: 1080,
      userId: 'test',
      password: 'test',
    },
  });

  let res = null;
  res = await whatsapp.login();
  // {status:'success'}
  // {status:'error'}

  res = await whatsapp.sendContactTextMessage({
    jid: '8613666666666',
    message: 'hello world.',
  });
};

main();

Recv Message

const { Whatsapp } = require('whatsapp-nodejs');

const main = async () => {
  const whatsapp = new Whatsapp({
    mongodb: 'mongodb://localhost:27017/whatsapp',
  });
  await whatsapp.init({
    mobile: '8613888888888', // cc is required, for example, china is +86
    proxy: {
      host: '127.0.0.1',
      port: 1080,
      userId: 'test',
      password: 'test',
    },
  });

  let res = null;
  res = await whatsapp.login();
  // {status:'success'}
  // {status:'error'}

  whatsapp.on('message', message => {
    console.log('on message', message);
  });
};

main();

More example: test/whatsapp.test.js