Add send message validation for app-multiple-account.js

Just a simple example, for more validations example can be founded in app.js
This commit is contained in:
Nur Muhammad
2022-03-22 08:19:17 +08:00
parent 965bc7975e
commit 4f0f6d0933

View File

@@ -164,12 +164,36 @@ io.on('connection', function(socket) {
});
// Send message
app.post('/send-message', (req, res) => {
app.post('/send-message', async (req, res) => {
const sender = req.body.sender;
const number = phoneNumberFormatter(req.body.number);
const message = req.body.message;
const client = sessions.find(sess => sess.id == sender).client;
const client = sessions.find(sess => sess.id == sender)?.client;
// Make sure the sender is exists & ready
if (!client) {
return res.status(422).json({
status: false,
message: `The sender: ${sender} is not found!`
})
}
/**
* Check if the number is already registered
* Copied from app.js
*
* Please check app.js for more validations example
* You can add the same here!
*/
const isRegisteredNumber = await client.isRegisteredUser(number);
if (!isRegisteredNumber) {
return res.status(422).json({
status: false,
message: 'The number is not registered'
});
}
client.sendMessage(number, message).then(response => {
res.status(200).json({