Skip to content

Commit

Permalink
Merge pull request #190 from vasani-arpit/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
vasani-arpit authored Oct 16, 2020
2 parents 3de0307 + b6b1168 commit 9a1c7a9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wbot",
"version": "0.14.5",
"version": "0.15.0",
"description": "A simple whatsapp reply bot using puppeteer.",
"main": "src/index.js",
"scripts": {
Expand Down
7 changes: 6 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ this.DEFAULT_CHROMIUM_ARGS = [
"--disable-accelerated-video-decode",
"--num-raster-threads=1",
];
this.DEFAULT_DATA_DIR = path.join(process.cwd(), "chromium-data");
this.DEFAULT_DATA_DIR = path.join(process.cwd(), "chromium-data");

/**
* Name of the file that stores bot configuration
*/
this.BOT_SETTINGS_FILE = "bot.json";
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var argv = require('yargs').argv;
var rev = require("./detectRev");
var constants = require("./constants");
var configs = require("../bot");
var settings = require('./settings');
var fs = require("fs");

//console.log(ps);

Expand Down Expand Up @@ -106,7 +108,19 @@ async function Main() {
spinner.stop("Opening Whatsapp ... done!");
page.exposeFunction("log", (message) => {
console.log(message);
})
});

// When the settings file is edited multiple calls are sent to function. This will help
// to prevent from getting corrupted settings data
let timeout = 5000;

// Register a filesystem watcher
fs.watch(constants.BOT_SETTINGS_FILE, (event, filename) => {
setTimeout(()=> {
settings.LoadBotSettings(event, filename, page);
}, timeout);
});

page.exposeFunction("getFile", utils.getFileInBase64);
page.exposeFunction("resolveSpintax", spintax.unspin);
}
Expand Down
35 changes: 35 additions & 0 deletions src/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { constants } = require("crypto");
/**
* Live settings loader
*/

var utils = require("./utils");

/**
* This function will load settings from JSON configuration file
* @param {*} event Event name
* @param {*} filename Name of the file
* @param {*} page Handle to webpage
*/
function loadSettings(event, filename, page)
{
// Check event type
if(event === "change")
{
// console.log("Settings changed");

// Load JSON settings file
let botJson = utils.externalInjection(filename);

// Update settings
botJson.then((data) => {
page.evaluate(`var intents = ${data}`);
}).catch((err) => {
console.log(`there was an error ${err}`);
});
} else if(event === "rename") {
console.log("warn: filename changed");
}
}

this.LoadBotSettings = loadSettings;

0 comments on commit 9a1c7a9

Please sign in to comment.