mirror of
https://github.com/cheveguerra/whatsapp-api-tutorial.git
synced 2026-04-18 03:39:27 +00:00
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:
@@ -164,12 +164,36 @@ io.on('connection', function(socket) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Send message
|
// Send message
|
||||||
app.post('/send-message', (req, res) => {
|
app.post('/send-message', async (req, res) => {
|
||||||
const sender = req.body.sender;
|
const sender = req.body.sender;
|
||||||
const number = phoneNumberFormatter(req.body.number);
|
const number = phoneNumberFormatter(req.body.number);
|
||||||
const message = req.body.message;
|
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 => {
|
client.sendMessage(number, message).then(response => {
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
|
|||||||
Reference in New Issue
Block a user