Skip to content

Commit

Permalink
Merge pull request #7 from vasani-arpit/development
Browse files Browse the repository at this point in the history
Dark theme, Group flag and headless flag
  • Loading branch information
vasani-arpit authored Apr 24, 2019
2 parents b12b235 + 580cc5b commit 5435b28
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Basic Configuration is in bot.json file like replying to Hi, hello and happy bir

This is where all the application related(node application behavior and such things) config will stay. will add more in future.

- **headless** Whether to start chrome as headless or not. this is regarding #4. Apparently, Whatsapp doesn't allow headless instances.
- **darkmode** Enables dark theme in Whatsapp web. Useless if you select headless true
- **isGroupReply** Whether to send replies in group or not. If set to false Bot will not reply if message received in group chat.

**bot**

An array of object. Properties of Object are self explanatory.
Expand Down
4 changes: 3 additions & 1 deletion bot.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"appconfig": {
"headless": false
"headless": false,
"darkmode": true,
"isGroupReply":false
},
"bot": [{
"contains": [],
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"homepage": "https://github.com/vasani-arpit/WBOT#readme",
"dependencies": {
"cli-progress": "^2.1.1",
"node-fetch": "^2.3.0",
"ora": "^3.0.0",
"puppeteer": "^1.11.0",
"qrcode-terminal": "^0.12.0",
Expand Down
31 changes: 31 additions & 0 deletions src/detectRev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var os = require("os");
var platform = os.platform();
var chromePlatform;
const fetch = require("node-fetch");

switch (platform) {
case "win32":
chromePlatform = "win";
break;
case "darwin":
chromePlatform = "mac";
break;
default:
chromePlatform = "linux"
break;

}

this.getRevNumber = function () {
return new Promise((resolve) => {
fetch('https://omahaproxy.appspot.com/all.json', { method: "get" })
.then(res => res.json())
.then(json => {
//console.log(json);
var currentOS = json.find(x => x.os == chromePlatform);
var betaChannel = currentOS.versions.find(x => x.channel == "beta").branch_base_position;
resolve(betaChannel);
});
})

}
28 changes: 15 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var utils = require("./utils");
var qrcode = require('qrcode-terminal');
var path = require("path");
var argv = require('yargs').argv;
var rev = require("./detectRev");

//console.log(ps);

Expand All @@ -32,14 +33,16 @@ async function Main() {
*/
async function downloadAndStartThings() {
let botjson = utils.externalInjection("bot.json");
var appconfig = await utils.externalInjection("bot.json");
appconfig = JSON.parse(appconfig);
spinner.start("Downloading chrome\n");
const browserFetcher = puppeteer.createBrowserFetcher({
path: process.cwd()
});
const progressBar = new _cliProgress.Bar({}, _cliProgress.Presets.shades_grey);
progressBar.start(100, 0);

const revisionInfo = await browserFetcher.download("619290", (download, total) => {
var revNumber = await rev.getRevNumber();
const revisionInfo = await browserFetcher.download(revNumber, (download, total) => {
//console.log(download);
var percentage = (download * 100) / total;
progressBar.update(percentage);
Expand All @@ -50,11 +53,11 @@ async function Main() {
spinner.start("Launching Chrome");
var pptrArgv = [];
if (argv.proxyURI) {
pptrArgv.push( '--proxy-server=' + argv.proxyURI );
pptrArgv.push('--proxy-server=' + argv.proxyURI);
}
const browser = await puppeteer.launch({
executablePath: revisionInfo.executablePath,
headless: true,
headless: appconfig.appconfig.headless,
userDataDir: path.join(process.cwd(), "ChromeSession"),
devtools: false,
args: pptrArgv
Expand All @@ -68,18 +71,21 @@ async function Main() {
if (page.length > 0) {
page = page[0];
if (argv.proxyURI) {
await page.authenticate({ username: argv.username , password: argv.password });
await page.authenticate({ username: argv.username, password: argv.password });
}
page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3336.0 Safari/537.36")
page.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36");
await page.goto('https://web.whatsapp.com', {
waitUntil: 'networkidle0',
timeout: 0
});
if (appconfig.appconfig.darkmode) {
page.addStyleTag({ path: "./src/style.css" });
}
//console.log(contents);
var filepath = path.join(__dirname, "WAPI.js");
await page.addScriptTag({path: require.resolve(filepath)});
await page.addScriptTag({ path: require.resolve(filepath) });
filepath = path.join(__dirname, "inject.js");
await page.addScriptTag({path: require.resolve(filepath)});
await page.addScriptTag({ path: require.resolve(filepath) });
botjson.then((data) => {
page.evaluate("var intents = " + data);
//console.log(data);
Expand All @@ -95,14 +101,10 @@ async function Main() {
//TODO: avoid using delay and make it in a way that it would react to the event.
utils.delay(3000);
//console.log("loaded");
var output = await page.evaluate("WAPI.isLoggedIn();");
var output = await page.evaluate("localStorage['last-wid']");
//console.log("\n" + output);
if (output) {
spinner.stop("Looks like you are already logged in");
console.log(await page.evaluate("window.chrome;"));
console.log(await page.evaluate("window.outerWidth;"));
console.log(await page.evaluate("window.outerHeight;"));

} else {
spinner.info("You are not logged in. Please scan the QR below");
}
Expand Down
4 changes: 4 additions & 0 deletions src/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ WAPI.waitNewMessages(false, (data) => {
}
if (message.type == "chat") {
//message.isGroupMsg to check if this is a group
if (message.isGroupMsg == true && intents.appconfig.isGroupReply == false) {
console.log("Message received in group and group reply is off. so will not take any actions.");
return;
}
var exactMatch = intents.bot.find(obj => obj.exact.find(ex => ex == message.body.toLowerCase()));
if (exactMatch != undefined) {
WAPI.sendSeen(message.from._serialized);
Expand Down
Loading

0 comments on commit 5435b28

Please sign in to comment.