-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
140 lines (129 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const pino = require("pino");
const path = require("path");
const colors = require("@colors/colors/safe");
const CFonts = require("cfonts");
const fs = require("fs-extra");
const chalk = require("chalk");
const readline = require("readline");
const {
default: makeWASocket,
useMultiFileAuthState,
delay,
PHONENUMBER_MCC,
} = require("@whiskeysockets/baileys");
global.sessionName = "auth-info";
const pairingCode = process.argv.includes("--use-pairing-code");
if (!pairingCode) {
console.log(chalk.redBright("Use --use-pairing-code"));
process.exit(1);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const question = (text) => new Promise((resolve) => rl.question(text, resolve));
CFonts.say("Zyy Pairing", {
font: "tiny",
align: "center",
colors: ["system"],
});
CFonts.say(
"Simple To Connect Whatsapp Bot Use Pairing Code\nWith Baileys Library\n\nGithub : https://github.com/rizzlogy/zyypairing",
{
colors: ["system"],
font: "console",
align: "center",
},
);
async function main() {
const sessionExists = await fs.pathExists(path.join(__dirname, sessionName));
if (sessionExists) {
console.log(chalk.greenBright("Clearing up session"));
await fs.emptyDir(path.join(__dirname, sessionName));
await delay(800);
ZyyPairing();
} else {
console.log(chalk.greenBright("Starting Code Pairing"));
ZyyPairing();
}
}
async function ZyyPairing() {
const { state, saveCreds } = await useMultiFileAuthState("./" + sessionName);
try {
const socket = makeWASocket({
printQRInTerminal: !pairingCode,
logger: pino({
level: "silent",
}),
browser: ["Chrome (Linux)", "", ""], // dont change this.
auth: state,
});
if (pairingCode && !socket.authState.creds.registered) {
let phoneNumber;
phoneNumber = await question(
chalk.bgBlack(chalk.greenBright(`Please type your WhatsApp number : `)),
);
phoneNumber = phoneNumber.replace(/[^0-9]/g, "");
// Ask again when entering the wrong number
if (
!Object.keys(PHONENUMBER_MCC).some((v) => phoneNumber.startsWith(v))
) {
console.log(
chalk.bgBlack(
chalk.redBright("Start with your country's WhatsApp code!"),
),
);
phoneNumber = await question(
chalk.bgBlack(
chalk.greenBright(`Please type your WhatsApp number : `),
),
);
phoneNumber = phoneNumber.replace(/[^0-9]/g, "");
rl.close();
}
setTimeout(async () => {
let code = await socket.requestPairingCode(phoneNumber);
code = code?.match(/.{1,4}/g)?.join("-") || code;
console.log(
chalk.black(chalk.bgGreen(`Your Pairing Code : `)),
chalk.black(chalk.white(code)),
);
}, 3000);
}
socket.ev.on(
"connection.update",
async ({ connection, lastDisconnect }) => {
if (connection === "open") {
let file = await socket.sendMessage(socket.user.id, {
document: fs.readFileSync("./" + sessionName + "/creds.json"),
mimetype: "json",
fileName: "creds.json",
});
await socket.sendMessage(
socket.user.id,
{ text: "Upload this session to ur bot multi auth state" },
{ quoted: file },
);
console.log(chalk.greenBright("DONE!"));
await fs.emptyDir("./" + sessionName);
process.exit(1);
} else if (
connection === "close" &&
lastDisconnect &&
lastDisconnect.error &&
lastDisconnect.error.output.statusCode &&
lastDisconnect.error.output.statusCode !== 401
) {
ZyyPairing();
await fs.emptyDir("./" + sessionName);
}
},
);
socket.ev.on("creds.update", saveCreds);
} catch (error) {
console.error(error);
await fs.emptyDir("./" + sessionName);
process.exit(1);
}
}
main().catch((error) => console.error(error));