Skip to content

Commit

Permalink
notification action & inline reply
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzombiee2361 committed Apr 22, 2022
1 parent eb5dd0a commit 47ae1e4
Show file tree
Hide file tree
Showing 10 changed files with 861 additions and 256 deletions.
584 changes: 343 additions & 241 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "src/main.js",
"scripts": {
"start": "electron .",
"clean": "rm -rf dist",
"dist": "npm run prod && npm run clean && npm run rebuild-deps && npm run build",
"clean": "rm -rf dist && rm -rf public/js/*",
"dist": "npm run clean && npm run prod && npm run rebuild-deps && npm run build",
"build": "electron-builder",
"rebuild-deps": "electron-rebuild",
"dev": "npm run development",
Expand Down Expand Up @@ -88,6 +88,7 @@
"electron-store": "^8.0.1",
"electron-updater": "^4.6.5",
"electron-window-state": "^5.0.3",
"get-pixels": "^3.3.3",
"get-port-sync": "^1.0.1",
"is-online": "^9.0.1",
"lsb-release": "^0.1.0",
Expand All @@ -96,6 +97,6 @@
"puppeteer-in-electron": "^3.0.5",
"vue-router": "^3.5.3",
"vuex": "^3.6.2",
"whatsapp-web-electron.js": "^1.16.5-1"
"whatsapp-web-electron.js": "^1.16.6-1"
}
}
1 change: 1 addition & 0 deletions src/Main/InstanceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ module.exports = class InstanceManager {
'archiveAllChats',
'markAllChatsAsRead',
'integrateToDesktop',
'chatNotification',
].forEach((func) => {
ipcMain.handle(`instance.main.${func}`, (event, id, ...args) => {
this.instances[id].main[func](...args);
Expand Down
66 changes: 64 additions & 2 deletions src/Main/MainWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const path = require('path');
const fs = require('fs');
const homedir = require('os').homedir();
const createDesktopShortcut = require('create-desktop-shortcuts');
const { app, dialog, BrowserWindow, Notification } = require("electron");
const { app, dialog, BrowserWindow, Notification, nativeImage } = require("electron");
const windowStateKeeper = require("electron-window-state");
const settings = require('./settings');
const pie = require("puppeteer-in-electron");
const puppeteer = require("puppeteer-core");
const { Client } = require('whatsapp-web-electron.js');
const { Notify } = require('./Notify');
const getPixels = require("get-pixels");
// const getPortSync = require('get-port-sync');

const ICON_PATH = path.join(__dirname, '../icons/logo360x360.png');
Expand Down Expand Up @@ -219,6 +221,63 @@ module.exports = class MainWindow extends BrowserWindow {
});
}

async getImageData(dataUrl) {
const iconImage = nativeImage.createFromDataURL(dataUrl);
const { width, height } = iconImage.getSize();

return new Promise((resolve) => {
getPixels(iconImage.toPNG(), 'image/png', (err, pixels) => {
const imageData = [];
if(err) {
console.log("Bad image path", err)
resolve(null);
return;
}
for (let y = 0; y < pixels.shape[1]; y++) {
for (let x = 0; x < pixels.shape[0]; x++) {
[0, 1, 2, 3].forEach((i) => {
imageData.push(pixels.get(x, y, i));
});
}
}

resolve({
width,
height,
hasAlpha: true,
data: imageData,
});
});
});
}

async chatNotification(options) {
const { title, body, icon, tag } = options;
const desktopEntry = path.join(homedir, '.local/share/applications/WALC.desktop');
const notif = new Notify({
summary: title,
body,
timeout: 5000,
appName: 'WALC',
hints: {
desktopEntry,
imageData: await this.getImageData(icon),
},
});

notif.addAction('Mark as read', async() => {
console.log('marked as read');
(await this.whatsapp.getChatById(tag)).sendSeen();
});

notif.addInlineReply('Reply', async (reply) => {
console.log('replied', reply);
(await this.whatsapp.getChatById(tag)).sendMessage(reply);
});

notif.show();
}

async initWhatsapp() {
this.loadURL('about:blank', { userAgent }).then(async () => {
const pieBrowser = await pie.connect(app, puppeteer);
Expand All @@ -227,11 +286,14 @@ module.exports = class MainWindow extends BrowserWindow {
this.whatsapp = new Client(pieBrowser, this);
this.whatsappReady = false;

this.whatsapp.on('ready', () => {
this.whatsapp.on('ready', async () => {
console.log('Whatsapp client ready');
this.whatsappReady = true;
this.webContents.send('ready', this._id);
this.emit('ready');

const chats = await this.whatsapp.getChats();
console.log(chats[0].id._serialized);
});

this.whatsapp.on("change_battery", (batteryInfo) => {
Expand Down
Loading

0 comments on commit 47ae1e4

Please sign in to comment.