0.0.38 • Published 1 year ago
whatsapp_client v0.0.38
Whatsapp Client
Install library
npm i whatsapp_client
Import library
var { WhatsappClient } = require("whatsapp_client");
Quickstart
var { WhatsappClient, UpdateWa } = require("whatsapp_client");
var readline = require('readline');
var rl = readline.createInterface({ input: process.stdin, output: process.stdout });
async function question(text) {
return new Promise(function (resolve) {
rl.question(text, resolve);
});
}
async function main() {
var rl = readline.createInterface({ input: process.stdin, output: process.stdout });
async function question(text) {
return new Promise(function (resolve) {
rl.question(text, resolve);
});
}
var wa = new WhatsappClient({
clientOption: {
"app_name": "Whatsapp Client",
"database_directory": "./db/client_0",
"database_key": "",
"is_mobile_api": false,
"print_qr_in_terminal": true,
},
});
wa.on("update", async function (updateWa) {
if (!updateWa.update_wa) {
return;
}
if (!updateWa.update_wa.update) {
return;
}
var update = updateWa.update_wa.update;
if (!update) {
return;
}
if (typeof update["@type"] != "string") {
var events = update;
if (events['creds.update']) {
}
if (events['connection.update']) {
var update = events['connection.update'];
var { connection, lastDisconnect, qr } = update;
}
return;
}
if (update["@type"] == "updateAuthorizationState") {
var authorization_state = update["authorization_state"];
if (authorization_state) {
if (authorization_state["@type"] == "authorizationStateWaitsScanQr") {
console.log("qr");
console.log(authorization_state);
}
if (authorization_state["@type"] == "authorizationStateWaitPhoneNumber") {
console.log("phone_number");
var res = await wa.invoke({
"method": "setAuthenticationPhoneNumber",
"parameters": {
"phone_number": await question('Please enter your mobile phone number: ')
},
waClient: updateWa.update_wa.waClient,
});
console.log(res);
return;
}
if (authorization_state["@type"] == "authorizationStateWaitCode") {
console.log("code");
var res = await wa.invoke({
"method": "checkAuthenticationCode",
"parameters": {
"code": await question('Please enter code: ')
},
waClient: updateWa.update_wa.waClient,
});
console.log(res);
return;
}
}
}
if (update["@type"] == "updateNewMessage" && update["message"]) {
var msg = update["message"];
var chat_id = msg["chat"]["id"];
var text = msg["text"];
console.log(update);
try {
if (text) {
if (RegExp("[/!\.]ping", "i").test(text)) {
await wa.invoke({
"method": "readMessage",
"parameters": {
"chat_id": chat_id,
"message_id": msg["id"],
},
waClient: updateWa.update_wa.waClient,
});
var res = await wa.invoke({
"method": "sendMessage",
"parameters": {
"chat_id": chat_id,
"text": `Pong`,
},
waClient: updateWa.update_wa.waClient,
});
console.log(JSON.stringify(res, null, 2));
}
}
} catch (e) {
console.log(e);
await wa.invoke({
"method": "sendMessage",
"parameters": {
"chat_id": chat_id,
"text": `Error: ${JSON.stringify(e)}`,
},
waClient: updateWa.update_wa.waClient,
});
}
}
});
await wa.initClient();
}
main();
0.0.38
1 year ago
0.0.37
1 year ago
0.0.35
2 years ago
0.0.36
2 years ago
0.0.20
2 years ago
0.0.21
2 years ago
0.0.22
2 years ago
0.0.23
2 years ago
0.0.24
2 years ago
0.0.25
2 years ago
0.0.16
2 years ago
0.0.17
2 years ago
0.0.18
2 years ago
0.0.19
2 years ago
0.0.30
2 years ago
0.0.31
2 years ago
0.0.32
2 years ago
0.0.33
2 years ago
0.0.34
2 years ago
0.0.26
2 years ago
0.0.27
2 years ago
0.0.28
2 years ago
0.0.11
2 years ago
0.0.12
2 years ago
0.0.13
2 years ago
0.0.14
2 years ago
0.0.15
2 years ago
0.0.9
2 years ago
0.0.7
2 years ago
0.0.6
3 years ago
0.0.5
3 years ago
0.0.4
3 years ago
0.0.3
3 years ago
0.0.2
3 years ago
0.0.1
3 years ago
0.0.0
3 years ago