WaJs
WaJs copied to clipboard
Trying to send image message
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 imageData = fs.readFileSync('/Users/ali/Downloads/1.png'); const mediaKey = crypto.randomBytes(32); const mediaKeyTime = Math.floor(Date.now() / 1000); const mediaKeyExpanded = hkdf(mediaKey, 112, {info: 'WhatsApp Image Keys', hash: 'SHA-256'});
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();
})
`
May be you need to using Chrome dev tools and look into web.whatsapp.com logs when sending image. Also you can modify some JS code or place breakpoint to debug it. Set chrome override to this directory
try search "msgCreateRecord" in app2.js
Your encryption method is wrong @aliarshad9691 , check out mine #7
Your encryption method is wrong @aliarshad9691 , check out mine #7
Are you able to send media with your implementation?
Your encryption method is wrong @aliarshad9691 , check out mine #7
Are you able to send media with your implementation?
I don't know, i didn't test it for sendingImageMessage.
I will test it out when im free.
Your encryption method is wrong @aliarshad9691 , check out mine #7
So i just tested it out, but still got 401.
I thought that the unauthorized response caused by incorrect encryption method, but when i used my encryption method the result still the same (401). And the fact that your encryption method was wrong is true.
Maybe we missed something that caused an authorization error.