Skip to content

Commit

Permalink
Release 1.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
pandroid-sudo committed Jan 4, 2023
1 parent bfa4cea commit 5429368
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 37 deletions.
44 changes: 16 additions & 28 deletions whatsapp_addon/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,24 @@
{
"name": "Example devcontainer for add-on repositories",
"image": "ghcr.io/home-assistant/devcontainer:addons",
"appPort": [
"7123:8123",
"7357:4357"
],
"appPort": ["7123:8123", "7357:4357"],
"postStartCommand": "bash devcontainer_bootstrap",
"runArgs": [
"-e",
"GIT_EDITOR=code --wait",
"--privileged"
],
"runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"],
"containerEnv": {
"WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}"
"WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}"
},
"extensions": [
"timonwong.shellcheck",
"esbenp.prettier-vscode"
],
"mounts": [
"type=volume,target=/var/lib/docker"
],
"extensions": ["timonwong.shellcheck", "esbenp.prettier-vscode"],
"mounts": [ "type=volume,target=/var/lib/docker" ],
"settings": {
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
},
"terminal.integrated.defaultProfile.linux": "zsh",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
},
"terminal.integrated.defaultProfile.linux": "zsh",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true
}
}
}
5 changes: 5 additions & 0 deletions whatsapp_addon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.2.4

- Bug fixed.
- Added patch for receive button on iOS (Attention! iOS receive buttons only if app is open (it seems to be a iOS app bug))

## 1.2.2

- Added the ability to always be online or offline. This could lead to not receiving notifications on other devices. (**Restard required**)
Expand Down
2 changes: 1 addition & 1 deletion whatsapp_addon/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Whatsapp
version: "1.2.3"
version: "1.2.4"
slug: whatsapp_addon
description: Whatsapp addon for send message from Home Assistant
url: "https://github.com/giuseppecastaldo/ha-addons/tree/main/whatsapp_addon"
Expand Down
2 changes: 1 addition & 1 deletion whatsapp_addon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "Giuseppe Castaldo",
"license": "ISC",
"dependencies": {
"@giuseppecastaldo/baileys": "git+https://github.com/giuseppecastaldo/Baileys",
"@adiwajshing/baileys": "github:adiwajshing/baileys",
"axios": "^0.27.2",
"body-parser": "^1.20.0",
"cors": "^2.8.5",
Expand Down
73 changes: 73 additions & 0 deletions whatsapp_addon/utils/useFileAuthState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const {
proto,
initAuthCreds,
BufferJSON
} = require("@adiwajshing/baileys");
const fs = require('fs');

const KEY_MAP = {
'pre-key': 'preKeys',
'session': 'sessions',
'sender-key': 'senderKeys',
'app-state-sync-key': 'appStateSyncKeys',
'app-state-sync-version': 'appStateVersions',
'sender-key-memory': 'senderKeyMemory'
};

async function readFileAsync(filename) {
try {
return await fs.promises.readFile(filename, 'utf8');
} catch (error) {
if (error.code === 'ENOENT') {
return null;
}
}
}

async function writeToFileAsync(filename, text) {
await fs.promises.writeFile(filename, text, 'utf8');
}

const useFileAuthState = async (path) => {
const saved_data = await readFileAsync(path)

let creds = saved_data != null ? JSON.parse(saved_data, BufferJSON.reviver).creds : initAuthCreds();
let keys = saved_data != null ? JSON.parse(saved_data, BufferJSON.reviver).keys : {};

const saveState = () => {
writeToFileAsync(path, JSON.stringify({ creds, keys }, BufferJSON.replacer, 2));
};

return {
state: {
creds,
keys: {
get: (type, ids) => {
const key = KEY_MAP[type];
return ids.reduce((dict, id) => {
var _a;
let value = (_a = keys[key]) === null || _a === void 0 ? void 0 : _a[id];
if (value) {
if (type === 'app-state-sync-key') {
value = proto.Message.AppStateSyncKeyData.fromObject(value);
}
dict[id] = value;
}
return dict;
}, {});
},
set: (data) => {
for (const _key in data) {
const key = KEY_MAP[_key];
keys[key] = keys[key] || {};
Object.assign(keys[key], data[_key]);
}
saveState();
}
}
},
saveState
};
};

module.exports = useFileAuthState
35 changes: 28 additions & 7 deletions whatsapp_addon/whatsapp.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const EventEmitter = require("eventemitter2");

const makeWASocket = require("@giuseppecastaldo/baileys").default;
const makeWASocket = require("@adiwajshing/baileys").default;
const {
DisconnectReason,
useSingleFileAuthState
} = require("@giuseppecastaldo/baileys");
} = require("@adiwajshing/baileys");
const useFileAuthState = require("./utils/useFileAuthState");

const MessageType = {
text: "conversation",
Expand Down Expand Up @@ -48,15 +48,37 @@ class WhatsappClient extends EventEmitter {
connect = async () => {
if (this.#status.connected) return

const { state, saveState } = useSingleFileAuthState(this.#path)
const { state, saveState } = await useFileAuthState(this.#path)

this.#conn = makeWASocket({
auth: state,
syncFullHistory: false,
markOnlineOnConnect: !this.#offline,
browser: ['Ubuntu', 'Desktop', '20.0.04'],
logger: require("pino")({ level: "silent" }),
defaultQueryTimeoutMs: undefined
defaultQueryTimeoutMs: undefined,
patchMessageBeforeSending: (message) => {
const requiresPatch = !!(
message.buttonsMessage
|| message.templateMessage
|| message.listMessage
);
if (requiresPatch) {
message = {
viewOnceMessage: {
message: {
messageContextInfo: {
deviceListMetadataVersion: 2,
deviceListMetadata: {},
},
...message,
},
},
};
}

return message;
}
})

this.#conn.ev.on('creds.update', (state) => {
Expand Down Expand Up @@ -121,13 +143,12 @@ class WhatsappClient extends EventEmitter {
this.#refreshInterval = setInterval(() => this.restart(), this.#refreshMs)
if (this.#offline) this.setSendPresenceUpdateInterval('unavailable')

this.#conn.ev.on('messages.upsert', msgs => {
this.#conn.ev.on('messages.upsert', async ({ messages }) => {
const msg = messages[0]

if (msg.hasOwnProperty('message') && !msg.key.fromMe) {
delete msg.message.messageContextInfo;
const messageType = Object.keys(msg.message)[0]

this.emit('msg', { type: messageType, ...msg })
}
})
Expand Down

0 comments on commit 5429368

Please sign in to comment.