1.0.11 • Published 4 years ago

@ngochipx/zalo v1.0.11

Weekly downloads
-
License
-
Repository
-
Last release
4 years ago

Welcome to @ngochipx/zalo!

Module to communicate with Zalo on nodeJS

Install

yarn add @ngochipx/zalo

##Note: In the first login, Zalo may start to verify your account. So you'll need to run it on your computer before deploying.

After logging in for the first time. Cookies will be stored in the tmpdir directory. The next time, Cookies will be used instead of the username and & password.

Api List

  • init
  • login
  • verifyLogin
  • loginWithQR
  • phoneToUser
  • sentMessageToPhone
  • getGroupInfo
  • joinGroup
  • sentMessageToGroup

Using

init:

const Zalo = require('@ngochipx/zalo')
...

let ZAccount = new Zalo();
await ZAccount.init({
    username: "YOUR_PHONE_NUMBER",
    password: "PASSWORD",
    tmpdir: process.cwd() + "/tmp"
});
NameDescriptionDefault
usernameusername or phone number used to log into the bot account.Empty
passwordpassword used to log into the bot account.Empty
tmpdirThe directory stores the data of the account after login. Please store it in a safe place./tmp
cookieDefault cookie. In case you already have a zalo cookie and uuid, use it to log in. (The username is still required in this case.)Empty
uuidDefault z_uuid. In case you already have a zalo cookie and uuid, use it to log in. (The username is still required in this case.)Empty

Login:

When logging in, you will usually need to verify step 2 by identifying 3 out of 12 people. The selected people are the people you have talked to recently.

So in the login function, the needVerify field will tell you whether this login session has to verify or not. If needVerify: true, the response will include an array of 12 people. You need to choose 3 out of 12 people. Then send their index into the verifyLogin function

...
let ZAccount = new Zalo();
function login(){
  await ZAccount.init({
      username: "YOUR_PHONE_NUMBER",
      password: "PASSWORD",
      tmpdir: process.cwd() + "/tmp"
  });
  let response = await ZAccount.login();
  if(response.needVerify){
    //need verify here
    //The response.data returned is an array consisting of avatar and username. Choose 3 out of 12 to send to the verify function.
  }
  else{
    //logged in
  }
}
...
function verify(){
  let choosed = [0,3,11];
  let response = await ZAccount.verifyLogin(choosed)
  if(response.cookie){
    //logged in
  }
} 

Login with QR code:

Another login option. You need to log in to this account on a phone device.

When calling the function, A QR code will be returned, you need to use Zalo in your phone to scan this QR code and agree to the new device to login. After the scan is complete, the system will automatically callback to save Cookies. Normally, you will not know if the system has successfully logged in, so you need to call any API to check the actual login status.

...
let ZAccount = new Zalo();
function loginWithQR(){
  await ZAccount.init({
      username: "YOUR_PHONE_NUMBER",
      password: "PASSWORD",
      tmpdir: process.cwd() + "/tmp"
  });
  let response = await ZAccount.loginWithQR();
  if(response.needVerify){
    //need verify here
    //The response.qrcode is QRcode's image data.
  }
  else{
    //logged in
  }
}
...
function verify(){
  //Call any API to see if it is logged in.
  let response = await ZAccount.phoneToUser("YOUR_TEST_PHONE_NUMBER", false);
} 

Get user info from phone number:

...
let ZAccount = new Zalo();
await ZAccount.init({
    username: "YOUR_PHONE_NUMBER",
    password: "PASSWORD",
    tmpdir: process.cwd() + "/tmp"
});
let userInfo = await ZAccount.phoneToUser("USERS_PHONE_NUMBER", false); //cache default: true

Sent Message to User:

...
let ZAccount = new Zalo();
await ZAccount.init({
    username: "YOUR_PHONE_NUMBER",
    password: "PASSWORD",
    tmpdir: process.cwd() + "/tmp"
});
let userInfo = await ZAccount.sentMessageToPhone("USERS_PHONE_NUMBER", "CONTENT");

Get Group Info:

getGroupInfo(groupLink, cache) groupLink: URL to access the group. You can find it opening the "share group" section. Example: https://chat.zalo.me/?g=jbnxpt718 cache: boolean. Default: true. If true, it will check the data cached in the previous file instead of requesting it directly. Note: Using cache may cause data to be inaccurate.

...
let ZAccount = new Zalo();
await ZAccount.init({
    username: "YOUR_PHONE_NUMBER",
    password: "PASSWORD",
    tmpdir: process.cwd() + "/tmp"
});
let group = await ZAccount.getGroupInfo("https://chat.zalo.me/?g=jbnxpt718", false);
/*
RESPONSE:
{
    "error_code": 0,
    "error_message": "Successful.",
    "data": {
      "groupId": "2349104230218671553",
      "name": "Test API",
      "desc": "",
      "type": 1,
      "creatorId": "5868708411519468029",
      "avt": "",
      "fullAvt": "",
      "memberIds": [],
      "currentMems": [
        {
          "id": "1571339566031530675",
          "dName": "Đỗ Duy Ngọc",
          "zaloName": "Đỗ Duy Ngọc",
          "avatar": "//s120.avatar.talk.zdn.vn/default",
          "avatar_25": "//s120.avatar.talk.zdn.vn/default",
          "accountStatus": 0
        },
        {
          "id": "5868708411519468029",
          "dName": "NgocHip",
          "zaloName": "NgocHip",
          "avatar": "//s120.avatar.talk.zdn.vn/default",
          "avatar_25": "//s120.avatar.talk.zdn.vn/default",
          "accountStatus": 0
        },
        {
          "id": "1633134080116981206",
          "dName": "Hải Ngô",
          "zaloName": "Hải Ngô",
          "avatar": "//s120-ava-talk.zadn.vn/f/b/4/e/2/120/97a3a2a6b4bbca00348f3d5be39e1f96.jpg",
          "avatar_25": "//s35-ava-talk.zadn.vn/f/b/4/e/2/35/97a3a2a6b4bbca00348f3d5be39e1f96.jpg",
          "accountStatus": 0
        }
      ],
      "updateMems": [],
      "admins": [],
      "hasMoreMember": 0,
      "subType": 1,
      "totalMember": 3,
      "maxMember": 1000,
      "setting": {
        "blockName": 0,
        "signAdminMsg": 1,
        "addMemberOnly": 0,
        "setTopicOnly": 0,
        "enableMsgHistory": 1,
        "lockCreatePost": 0,
        "lockCreatePoll": 0
      },
      "createdTime": 1584962925356,
      "visibility": 0,
      "extraInfo": {
        "enable_media_store": 1
      }
    }
  }
*/

Join Group:

joinGroup(groupLink) groupLink: URL to access the group. You can find it opening the "share group" section. Example: https://chat.zalo.me/?g=jbnxpt718

...
let ZAccount = new Zalo();
await ZAccount.init({
    username: "YOUR_PHONE_NUMBER",
    password: "PASSWORD",
    tmpdir: process.cwd() + "/tmp"
});
let group = await ZAccount.joinGroup("https://chat.zalo.me/?g=jbnxpt718");

Sent Message to Group:

sentMessageToGroup(groupId, message) groupId: is the identifier of each group. You can get it after calling the getGroupInfo API. (group.groupId) message: Content you want to send to the group.

...
let ZAccount = new Zalo();
await ZAccount.init({
    username: "YOUR_PHONE_NUMBER",
    password: "PASSWORD",
    tmpdir: process.cwd() + "/tmp"
});
let group = await ZAccount.getGroupInfo("https://chat.zalo.me/?g=jbnxpt718");
let result = await ZAccount.sentMessageToGroup(group.groupId, "ngochip test")

NgocHip

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.0

4 years ago