Skip to content

Commit 5429368

Browse files
committed
Release 1.2.4
1 parent bfa4cea commit 5429368

File tree

6 files changed

+124
-37
lines changed

6 files changed

+124
-37
lines changed
Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
11
{
22
"name": "Example devcontainer for add-on repositories",
33
"image": "ghcr.io/home-assistant/devcontainer:addons",
4-
"appPort": [
5-
"7123:8123",
6-
"7357:4357"
7-
],
4+
"appPort": ["7123:8123", "7357:4357"],
85
"postStartCommand": "bash devcontainer_bootstrap",
9-
"runArgs": [
10-
"-e",
11-
"GIT_EDITOR=code --wait",
12-
"--privileged"
13-
],
6+
"runArgs": ["-e", "GIT_EDITOR=code --wait", "--privileged"],
147
"containerEnv": {
15-
"WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}"
8+
"WORKSPACE_DIRECTORY": "${containerWorkspaceFolder}"
169
},
17-
"extensions": [
18-
"timonwong.shellcheck",
19-
"esbenp.prettier-vscode"
20-
],
21-
"mounts": [
22-
"type=volume,target=/var/lib/docker"
23-
],
10+
"extensions": ["timonwong.shellcheck", "esbenp.prettier-vscode"],
11+
"mounts": [ "type=volume,target=/var/lib/docker" ],
2412
"settings": {
25-
"terminal.integrated.profiles.linux": {
26-
"zsh": {
27-
"path": "/usr/bin/zsh"
28-
}
29-
},
30-
"terminal.integrated.defaultProfile.linux": "zsh",
31-
"editor.formatOnPaste": false,
32-
"editor.formatOnSave": true,
33-
"editor.formatOnType": true,
34-
"files.trimTrailingWhitespace": true
13+
"terminal.integrated.profiles.linux": {
14+
"zsh": {
15+
"path": "/usr/bin/zsh"
16+
}
17+
},
18+
"terminal.integrated.defaultProfile.linux": "zsh",
19+
"editor.formatOnPaste": false,
20+
"editor.formatOnSave": true,
21+
"editor.formatOnType": true,
22+
"files.trimTrailingWhitespace": true
3523
}
36-
}
24+
}

whatsapp_addon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.2.4
2+
3+
- Bug fixed.
4+
- Added patch for receive button on iOS (Attention! iOS receive buttons only if app is open (it seems to be a iOS app bug))
5+
16
## 1.2.2
27

38
- Added the ability to always be online or offline. This could lead to not receiving notifications on other devices. (**Restard required**)

whatsapp_addon/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Whatsapp
2-
version: "1.2.3"
2+
version: "1.2.4"
33
slug: whatsapp_addon
44
description: Whatsapp addon for send message from Home Assistant
55
url: "https://github.com/giuseppecastaldo/ha-addons/tree/main/whatsapp_addon"

whatsapp_addon/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"author": "Giuseppe Castaldo",
1111
"license": "ISC",
1212
"dependencies": {
13-
"@giuseppecastaldo/baileys": "git+https://github.com/giuseppecastaldo/Baileys",
13+
"@adiwajshing/baileys": "github:adiwajshing/baileys",
1414
"axios": "^0.27.2",
1515
"body-parser": "^1.20.0",
1616
"cors": "^2.8.5",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const {
2+
proto,
3+
initAuthCreds,
4+
BufferJSON
5+
} = require("@adiwajshing/baileys");
6+
const fs = require('fs');
7+
8+
const KEY_MAP = {
9+
'pre-key': 'preKeys',
10+
'session': 'sessions',
11+
'sender-key': 'senderKeys',
12+
'app-state-sync-key': 'appStateSyncKeys',
13+
'app-state-sync-version': 'appStateVersions',
14+
'sender-key-memory': 'senderKeyMemory'
15+
};
16+
17+
async function readFileAsync(filename) {
18+
try {
19+
return await fs.promises.readFile(filename, 'utf8');
20+
} catch (error) {
21+
if (error.code === 'ENOENT') {
22+
return null;
23+
}
24+
}
25+
}
26+
27+
async function writeToFileAsync(filename, text) {
28+
await fs.promises.writeFile(filename, text, 'utf8');
29+
}
30+
31+
const useFileAuthState = async (path) => {
32+
const saved_data = await readFileAsync(path)
33+
34+
let creds = saved_data != null ? JSON.parse(saved_data, BufferJSON.reviver).creds : initAuthCreds();
35+
let keys = saved_data != null ? JSON.parse(saved_data, BufferJSON.reviver).keys : {};
36+
37+
const saveState = () => {
38+
writeToFileAsync(path, JSON.stringify({ creds, keys }, BufferJSON.replacer, 2));
39+
};
40+
41+
return {
42+
state: {
43+
creds,
44+
keys: {
45+
get: (type, ids) => {
46+
const key = KEY_MAP[type];
47+
return ids.reduce((dict, id) => {
48+
var _a;
49+
let value = (_a = keys[key]) === null || _a === void 0 ? void 0 : _a[id];
50+
if (value) {
51+
if (type === 'app-state-sync-key') {
52+
value = proto.Message.AppStateSyncKeyData.fromObject(value);
53+
}
54+
dict[id] = value;
55+
}
56+
return dict;
57+
}, {});
58+
},
59+
set: (data) => {
60+
for (const _key in data) {
61+
const key = KEY_MAP[_key];
62+
keys[key] = keys[key] || {};
63+
Object.assign(keys[key], data[_key]);
64+
}
65+
saveState();
66+
}
67+
}
68+
},
69+
saveState
70+
};
71+
};
72+
73+
module.exports = useFileAuthState

whatsapp_addon/whatsapp.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
const EventEmitter = require("eventemitter2");
22

3-
const makeWASocket = require("@giuseppecastaldo/baileys").default;
3+
const makeWASocket = require("@adiwajshing/baileys").default;
44
const {
55
DisconnectReason,
6-
useSingleFileAuthState
7-
} = require("@giuseppecastaldo/baileys");
6+
} = require("@adiwajshing/baileys");
7+
const useFileAuthState = require("./utils/useFileAuthState");
88

99
const MessageType = {
1010
text: "conversation",
@@ -48,15 +48,37 @@ class WhatsappClient extends EventEmitter {
4848
connect = async () => {
4949
if (this.#status.connected) return
5050

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

5353
this.#conn = makeWASocket({
5454
auth: state,
5555
syncFullHistory: false,
5656
markOnlineOnConnect: !this.#offline,
5757
browser: ['Ubuntu', 'Desktop', '20.0.04'],
5858
logger: require("pino")({ level: "silent" }),
59-
defaultQueryTimeoutMs: undefined
59+
defaultQueryTimeoutMs: undefined,
60+
patchMessageBeforeSending: (message) => {
61+
const requiresPatch = !!(
62+
message.buttonsMessage
63+
|| message.templateMessage
64+
|| message.listMessage
65+
);
66+
if (requiresPatch) {
67+
message = {
68+
viewOnceMessage: {
69+
message: {
70+
messageContextInfo: {
71+
deviceListMetadataVersion: 2,
72+
deviceListMetadata: {},
73+
},
74+
...message,
75+
},
76+
},
77+
};
78+
}
79+
80+
return message;
81+
}
6082
})
6183

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

124-
this.#conn.ev.on('messages.upsert', msgs => {
146+
this.#conn.ev.on('messages.upsert', async ({ messages }) => {
125147
const msg = messages[0]
126148

127149
if (msg.hasOwnProperty('message') && !msg.key.fromMe) {
128150
delete msg.message.messageContextInfo;
129151
const messageType = Object.keys(msg.message)[0]
130-
131152
this.emit('msg', { type: messageType, ...msg })
132153
}
133154
})

0 commit comments

Comments
 (0)