Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new multidevice authentication #63

Merged
merged 3 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ cache/*
!cache/readme.txt
public/*
!public/readme.txt
session/assets/qr.svg
.wwebjs_auth/*
.wwebjs_auth
session.zip
session.txt
session-output/*
session-output
session.secure
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<p align="center">
<a href="https://github.com/tuhinpal/WhatsBot/releases">
<img src="https://shields.io/badge/WHATSBOT-Version--1.6.0-red?logo=whatsapp&style=for-the-badge"
<img src="https://shields.io/badge/WHATSBOT-Version--3.0.0-red?logo=whatsapp&style=for-the-badge"
alt="Version"></a><br>
<a href="https://github.com/tuhinpal/WhatsBot/wiki">
<img src="https://shields.io/badge/WIKI-red?style=for-the-badge" alt="Wiki"></a>
Expand Down Expand Up @@ -64,7 +64,7 @@
### Deploy :

[![Deploy with Heroku](https://www.herokucdn.com/deploy/button.svg "Deploy with Heroku")](https://heroku.com/deploy?template=https://github.com/tuhinpal/WhatsBot "Deploy with Heroku")<br>
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new/template?template=https%3A%2F%2Fgithub.com%2Ftuhinpal%2FWhatsBot&plugins=mongodb&envs=SESSION%2CPMPERMIT_ENABLED%2CDEFAULT_TR_LANG%2CENABLE_DELETE_ALERT%2COCR_SPACE_API_KEY&optionalEnvs=OCR_SPACE_API_KEY&SESSIONDesc=Puppeteer+Session.+Ge+it+by+running+genToken.js&PMPERMIT_ENABLEDDesc=Enable+Pmpermit+write+true+or+false+only&DEFAULT_TR_LANGDesc=Default+Translation+Language&ENABLE_DELETE_ALERTDesc=If+true+and+if+someone+delete+message+in+PM%2C+Bot+will+send+the+deleted+message+in+that+chat+%28Exclude+Media%29&OCR_SPACE_API_KEYDesc=Get+it+from+https%3A%2F%2Focr.space%2FOCRAPI&PMPERMIT_ENABLEDDefault=true&DEFAULT_TR_LANGDefault=en&referralCode=tuhin)
[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new/template/RTyLts?referralCode=tuhin)

### Commands :

Expand Down
8 changes: 6 additions & 2 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
"keywords": ["whatsbot", "whatsapp-bot", "whatsapp"],
"stack": "container",
"env": {
"SESSION": {
"description": "Puppeteer Session. Get it by running genToken.js",
"SESSION_KEY": {
"description": "Session encryption password",
"value": ""
},
"SESSION_URL": {
"description": "session.secure public url (It is safe because it is encrypted, But still if you want maybe you can create some sort of authorization stuff)",
"value": ""
},
"PMPERMIT_ENABLED": {
Expand Down
5 changes: 1 addition & 4 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ const fs = require("fs");
require("dotenv").config();

module.exports = {
session: JSON.parse(
process.env.SESSION ||
fs.readFileSync(__dirname + "/session.json", { encoding: "utf8" })
), //if not using env vars create a file named session.json
session_key: process.env.SESSION_KEY,
pmpermit_enabled: process.env.PMPERMIT_ENABLED || "true",
mongodb_url: process.env.MONGODB_URL || process.env.MONGO_URL || "",
default_tr_lang: process.env.DEFAULT_TR_LANG || "en",
Expand Down
2 changes: 1 addition & 1 deletion example.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SESSION = ''
SESSION_KEY="tuhin"
PMPERMIT_ENABLED = 'true'
MONGODB_URL = ''
DEFAULT_TR_LANG = 'en'
Expand Down
9 changes: 6 additions & 3 deletions examples/example.env.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ The `.env` file is used to initialize all enviroment variables in the developmen
Create the `.env` file in the root of your app and add your variables and values to it. These are all the variables that are needed by WhatsBot. Description about these can be found in [app.json](./app.json).

```env
SESSION = ""
SESSION_KEY=""
SESSION_URL=""
PMPERMIT_ENABLED = ""
MONGODB_URL = ""
YT_DATA_API_KEY = ""
Expand All @@ -19,13 +20,15 @@ It is not mandatory to add all the variables in the `.env` file. Most of these h
At the bare minimum, you need to initialize atleast the following variables to make it work in your local environment.

```env
SESSION = ""
SESSION_KEY = ""
MONGODB_URL = ""
YT_DATA_API_KEY = ""
OCR_SPACE_API_KEY = ""
```

- SESSION : Puppeteer Session. Get it by running genToken.js. As getToken.js creates a session.json file, this might not be necessary in a local environment as this variable has a fallback to the session.json file. It is mentioned here as it is the most inportant information needed by the bot to work.
- SESSION_KEY : Your `session.secure` encryption key.

- SESSION_URL : You can retrieve your session from an public hosted endpoint, This is just a file and this is secured with `SESSION_KEY`. Don't put it if you are using VPS or your Local machine.

- YT_DATA_API_KEY : Youtube DATA API key, grab it from <https:/>/cloud.google.com>.

Expand Down
6 changes: 2 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
//jshint esversion:8
const express = require("express");
const app = express();
const { Client, LegacySessionAuth } = require("whatsapp-web.js");
const { Client, LocalAuth } = require("whatsapp-web.js");
const pmpermit = require("./helpers/pmpermit");
const config = require("./config");
const fs = require("fs");
const logger = require("./logger");

const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
authStrategy: new LegacySessionAuth({
session: config.session,
}),
authStrategy: new LocalAuth({ clientId: "whatsbot" }),
});

client.commands = new Map();
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "whatsbot",
"version": "2.1.1",
"version": "3.0.0",
"description": "Plugable U***b*t for WhatsApp",
"main": "main.js",
"scripts": {
"start": "node main.js",
"start": "node startProcess.js",
"gentoken": "node session/genToken.js",
"gentokenlite": "node session/genTokenLite.js"
"gentokenwithdownload": "node session/genTokenWithDownload.js"
},
"repository": {
"type": "git",
Expand All @@ -23,6 +23,8 @@
"homepage": "https://github.com/tuhinpal/WhatsBot#readme",
"dependencies": {
"@iamtraction/google-translate": "^1.1.2",
"adm-zip": "^0.5.9",
"aes-encrypt-stream": "^0.2.0",
"axios": "^0.21.0",
"child_process": "^1.0.2",
"dotenv": "^10.0.0",
Expand Down
Binary file removed session/assets/loading.gif
Binary file not shown.
67 changes: 0 additions & 67 deletions session/assets/qr.html

This file was deleted.

57 changes: 32 additions & 25 deletions session/genToken.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,45 @@
const { Client, LegacySessionAuth } = require("whatsapp-web.js");
const { Client, LocalAuth } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");
const logger = require("../logger");
const { write, clean } = require("./manage");
const readline = require("readline");

clean();

const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
authStrategy: new LegacySessionAuth(),
});
client.initialize();

client.on("qr", (qr) => {
console.log(`Scan this QR Code and copy the JSON\n`);
qrcode.generate(qr, { small: true });
authStrategy: new LocalAuth({ clientId: "whatsbot" }),
});

var token = "";
let password = null;

client.on("authenticated", (session) => {
token = session;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

client.on("ready", async () => {
// console.log(JSON.stringify(token));
rl.question(
"Enter password to encrypt session (You need to put this in ENV): ",
(answer) => {
password = answer;
console.log("Password set to:", password);
console.log("Generating QR Code...");
rl.close();
client.initialize();
}
);

await logger(
client,
`Here is your session token. Please keep is as a secret. You can delete the file if you don't need it.\n\n*Generated at:* ${new Date()}`
);
await logger(client, JSON.stringify(token));

console.log(
"\n\nPlease open your Whatsapp and see your chat. Your session token will be saved there."
);
client.on("qr", (qr) => {
console.log(`Scan this QR Code and copy the JSON\n`);
qrcode.generate(qr, { small: true });
});

setTimeout(() => {
client.on("ready", () => {
client.destroy();
console.log("Please wait...");
// wait because filesystem is busy
setTimeout(async () => {
console.log("Session has been created");
await write(password);
process.exit();
}, 5000);
}, 3000);
});
60 changes: 0 additions & 60 deletions session/genTokenLite.js

This file was deleted.

54 changes: 54 additions & 0 deletions session/genTokenWithDownload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { Client, LocalAuth } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");
const { write, clean } = require("./manage");
const readline = require("readline");
const app = require("express")();

clean();

const client = new Client({
puppeteer: { headless: true, args: ["--no-sandbox"] },
authStrategy: new LocalAuth({ clientId: "whatsbot" }),
});

let password = null;

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

rl.question(
"Enter password to encrypt session (You need to put this in ENV): ",
(answer) => {
password = answer;
console.log("Password set to:", password);
console.log("Generating QR Code...");
rl.close();
client.initialize();
}
);

client.on("qr", (qr) => {
console.log(`Scan this QR Code and copy the JSON\n`);
qrcode.generate(qr, { small: true });
});

client.on("ready", () => {
client.destroy();
console.log("Please wait...");
// wait because filesystem is busy
setTimeout(async () => {
console.log("Session has been created");
await write(password);
app.listen(8080, () => {
console.log(
"Go to http://{app_url}/session.secure to download the session"
);
});
}, 3000);
});

app.get("/session.secure", (req, res) => {
res.download("./session.secure");
});
Loading