Files
whatsapp-api-tutorial/index-multiple-account.html
2022-07-05 17:42:13 +08:00

183 lines
5.1 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<title>Whatsapp API by Ngekoding</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- This parts is optional, just for improve the styles -->
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Montserrat', sans-serif;
padding: 20px;
}
.form-container {
margin: 15px 0;
max-width: 500px;
}
.form-container input,
.form-container textarea {
width: 100%;
border: 1px solid #ccc;
border-radius: 2px;
padding: 5px 8px;
font-family: inherit;
}
.add-client-btn {
padding: 6px 15px;
margin-top: 10px;
background: green;
color: white;
border: 1px solid rgb(0, 93, 0);
border-radius: 2px;
}
.client-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
grid-gap: 15px;
margin-top: 30px;
}
.client {
border: 1px solid #ccc;
border-radius: 4px;
padding: 15px;
}
#qrcode {
display: none; /* Showed when qr code received */
width: 100%;
margin: 10px 0;
border: 1px solid #efefef;
border-radius: 4px;
}
ul.logs {
max-height: 150px;
padding: 15px 15px 15px 30px;
margin-top: 5px;
border-radius: 4px;
overflow-y: auto;
background: #efefef;
color: #666;
font-size: 14px;
}
ul.logs li:first-child {
color: green;
}
.hide {
display: none;
}
</style>
</head>
<body>
<div id="app">
<h1>Whatsapp API</h1>
<p>Powered by Ngekoding</p>
<div class="form-container">
<label for="client-id">ID</label><br>
<input type="text" id="client-id" placeholder="Masukkan ID">
<br><br>
<label for="client-description">Deskripsi</label><br>
<textarea rows="3" id="client-description" placeholder="Masukkan deskripsi"></textarea>
<br>
<button class="add-client-btn">Tambah Client</button>
</div>
<div class="client-container">
<div class="client hide">
<h3 class="title"></h3>
<p class="description"></p>
<img src="" alt="QR Code" id="qrcode">
<h3>Logs:</h3>
<ul class="logs"></ul>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<script>
$(document).ready(function() {
var socket = io();
// Ketika button tambah diklik
$('.add-client-btn').click(function() {
var clientId = $('#client-id').val();
var clientDescription = $('#client-description').val();
/**
* Some peoples want to use the phone number as the ID
* But because we use the ID as the html class attribute value: class="<The ID>"
* It won't work. Read more on https://www.w3.org/TR/REC-html40/types.html#type-cdata
*
* So, here we add the prefix to solve that problem
* Each ID will automatically added a 'client-' prefix for the class attribute
*/
var clientClass = 'client-' + clientId;
var template = $('.client').first().clone()
.removeClass('hide')
.addClass(clientClass);
template.find('.title').html(clientId);
template.find('.description').html(clientDescription);
template.find('.logs').append($('<li>').text('Connecting...'));
$('.client-container').append(template);
socket.emit('create-session', {
id: clientId,
description: clientDescription
});
});
socket.on('init', function(data) {
$('.client-container .client').not(':first').remove();
console.log(data);
for (var i = 0; i < data.length; i++) {
var session = data[i];
var clientId = session.id;
var clientDescription = session.description;
var clientClass = 'client-' + clientId;
var template = $('.client').first().clone()
.removeClass('hide')
.addClass(clientClass);
template.find('.title').html(clientId);
template.find('.description').html(clientDescription);
$('.client-container').append(template);
if (session.ready) {
$(`.client.${clientClass} .logs`).prepend($('<li>').text('Whatsapp is ready!'));
} else {
$(`.client.${clientClass} .logs`).prepend($('<li>').text('Connecting...'));
}
}
});
socket.on('remove-session', function(id) {
$(`.client.client-${id}`).remove();
});
socket.on('message', function(data) {
$(`.client.client-${data.id} .logs`).prepend($('<li>').text(data.text));
});
socket.on('qr', function(data) {
$(`.client.client-${data.id} #qrcode`).attr('src', data.src);
$(`.client.client-${data.id} #qrcode`).show();
});
socket.on('ready', function(data) {
$(`.client.client-${data.id} #qrcode`).hide();
});
socket.on('authenticated', function(data) {
$(`.client.client-${data.id} #qrcode`).hide();
});
});
</script>
</body>
</html>