I am trying to send image message, I am able to upload the file but it's giving me 401 on final send command. If I can get a hint on this, I can also make a PR to this repo.
const iv = mediaKeyExpanded.slice(0, 16);
const cipherKey = mediaKeyExpanded.slice(16, 48);
const macKey = mediaKeyExpanded.slice(48, 80);
const cipher = crypto.createCipheriv('aes-256-cbc', cipherKey, iv);
let enc = cipher.update(imageData);
enc = Buffer.concat([iv, enc, cipher.final()]);
const sig = crypto.createHmac("sha256", macKey).update(Buffer.concat([iv, enc])).digest();
const mac = sig.slice(0, 10);
let sum = crypto.createHash('sha256');
sum.update(imageData);
const fileSha256 = sum.digest();
let sum2 = crypto.createHash('sha256');
sum2.update(Buffer.concat([enc, mac]));
const fileEncSha256 = sum2.digest();
const fileEncSha256B64 = fileEncSha256.toString('base64');
wa.client.ws.sendCmd('query', 'mediaConn').then(data => {
const auth = data.media_conn.auth;
const options = {
hostname: 'mmg-fna.whatsapp.net',
path: `/mms/image/${(fileEncSha256B64)}?auth=${encodeURI(auth)}&token=${(fileEncSha256B64)}`,
method: 'POST',
port: 443,
headers: {
'Origin': 'https://web.whatsapp.com',
'Referer': 'https://web.whatsapp.com/',
}
};
const req = https.request(options, resp => {
resp.on('data', (d) => {
const jd = JSON.parse(d)
const webMsg = new WebMessageInfo();
const msg = new Message();
const key = new MessageKey();
const msgId = createMessageId();
key.setId(msgId);
key.setRemotejid(widHelper.sanitizer('[email protected]'));
key.setFromme(true);
const imageProto = new ImageMessage();
imageProto.setUrl(jd.url);
imageProto.setMediakey(mediaKey.toString('base64'));
imageProto.setMimetype('image/jpeg');
imageProto.setFileencsha256(fileEncSha256.toString('base64'));
imageProto.setFilesha256(fileSha256.toString('base64'));
imageProto.setDirectpath(jd.direct_path);
imageProto.setMediakeytimestamp(mediaKeyTime);
imageProto.setFilelength(Buffer.byteLength(imageData));
msg.setImagemessage(imageProto);
webMsg.setMessage(msg);
webMsg.setKey(key);
webMsg.setMessagetimestamp(Math.floor(Date.now() / 1000));
webMsg.setStatus(1);
const buf = webMsg.serializeBinary();
const node = wa.client.actionNode("relay", [
["message", undefined, buf]
]);
wa.client.ws.sendNode(node, msgId, binaryOptions(METRIC.MESSAGE));
});
});
req.on('error', (e) => {
console.error('errorerrorerrorerrorerrore', e);
});
req.write(Buffer.concat([enc, mac]));
req.end();
})