Skip to content

Instantly share code, notes, and snippets.

@vielhuber
Last active March 14, 2025 11:56
Show Gist options
  • Save vielhuber/b84d453876f15bb3d5e21305af26136e to your computer and use it in GitHub Desktop.
Save vielhuber/b84d453876f15bb3d5e21305af26136e to your computer and use it in GitHub Desktop.
whatsapp js web api #js
/node_modules/
/.wwebjs_auth/
/.wwebjs_cache/
/package-lock.json
  • node --version // >= 23
  • npm init -y
  • npm install whatsapp-web.js
  • npm install qrcode-terminal
  • node --no-deprecation script.js
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const fs = require('fs');
const client = new Client({
authStrategy: new LocalAuth({ clientId: 'custom-session' }),
puppeteer: {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
],
defaultViewport: {
width: 1920,
height: 1080
}
}
});
client.on('qr', async qr => {
console.log('QR-Code erhalten!');
qrcode.generate(qr, { small: true }, function (qrcode) {
console.log(qrcode);
console.log('Schreibe Datei...');
fs.writeFileSync(
'qr.json',
JSON.stringify({
success: true,
data: { img: qrcode }
})
);
fs.writeFileSync(
'status.json',
JSON.stringify({
success: true,
message: 'finished'
})
);
});
});
client.on('ready', async () => {
console.log('Client ist bereit!');
// send message
let recipient = '[email protected]';
await client.sendMessage(recipient, '🤖 test 🤖');
let media = MessageMedia.fromFilePath('dummy.pdf');
let response = await client.sendMessage(recipient, media, { caption: '🤖 media file 🤖' });
// fetch messages
let data = [];
let chats = await client.getChats();
for (let chat of chats) {
let messages = await chat.fetchMessages({ limit: 50 });
messages.forEach(messages__value => {
data.push(messages__value);
});
}
fs.writeFileSync('data.json', JSON.stringify({ success: true, data: data }));
fs.writeFileSync(
'status.json',
JSON.stringify({
success: true,
message: 'finished'
})
);
await client.destroy();
process.exit();
});
fs.writeFileSync(
'status.json',
JSON.stringify({
success: true,
message: 'initializing'
})
);
client.initialize();
// limit timeout
setTimeout(async () => {
await client.destroy();
process.exit();
}, 120000);
$whatsapp_folder = $_SERVER['DOCUMENT_ROOT'] . '/path/to/script';
@unlink($whatsapp_folder . '/status.json');
@unlink($whatsapp_folder . '/qr.json');
@unlink($whatsapp_folder . '/data.json');
if(PHP_OS_FAMILY === 'Windows') {
define('NODE_PATH', '"C:\Program Files\nodejs\node"');
file_put_contents($whatsapp_folder . '/run.bat', '@echo off' . PHP_EOL . 'cd /d ' . $whatsapp_folder . '' . PHP_EOL . 'start /B "" ' . NODE_PATH . ' script.js > NUL 2>&1' );
pclose(popen('start /B cmd /c ' . $whatsapp_folder . '/run.bat', 'r'));
}
else {
define('NODE_PATH', '/root/.nvm/versions/node/v23.5.0/bin/node');
shell_exec('cd ' . $whatsapp_folder . ' && ' . NODE_PATH . ' --no-deprecation script.js > /dev/null 2>&1 &');
}
while (
!file_exists($whatsapp_folder . '/status.json') ||
strpos(json_decode(file_get_contents($whatsapp_folder . '/status.json'))->message, 'finished') === false
) {
sleep(1);
}
if (file_exists($whatsapp_folder . '/qr.json')) {
echo 'QR-Code scannen, um Zugriff zu erhalten!' .
'<br/>' ;
echo 'Nach dem Scannen bitte mind. 1 Minute warten, bevor das Script erneut ausgeführt wird!' . '<br/>';
echo '<textarea style="width: 500px;height: 500px;">';
echo json_decode(file_get_contents($whatsapp_folder . '/qr.json'))->data->img;
echo '</textarea>';
echo '<br/>';
@unlink($whatsapp_folder . '/qr.json');
} else {
echo 'Kein QR-Code vorhanden!<br/>';
}
if (file_exists($whatsapp_folder . '/data.json')) {
$data = json_decode(file_get_contents($whatsapp_folder . '/data.json'));
foreach ($data as $data__value) {
print_r($data__value);
}
} else {
echo 'Keine Daten vorhanden!<br/>';
}
@unlink($whatsapp_folder . '/status.json');
@unlink($whatsapp_folder . '/qr.json');
@unlink($whatsapp_folder . '/data.json');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment