Fixed validation

This commit is contained in:
Daniel Gradman-Svendsen 2025-11-17 14:23:50 +01:00
parent 642c2b5841
commit f8f2fce606
2 changed files with 50 additions and 6 deletions

View File

@ -20,13 +20,30 @@ module.exports = function(RED) {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
async function whatsappMessage(numb , inputMessage){
// Validate message
if (!inputMessage) {
node.error('Error Sending Msg: Message payload is empty or undefined');
SetStatus("Message empty", "red");
setTimeout(() => SetStatus('Connected','green'), 3000);
return;
}
if (node.waClient.clientType === "waWebClient"){
try {
numb = await formatChatNumber(numb, node.waClient, node);
if(typeof inputMessage === "object"){
// Ensure message is a string for simple messages
if(typeof inputMessage === "object" && inputMessage.buttons){
inputMessage = new Buttons(inputMessage.text, inputMessage.buttons, "text" ,inputMessage.footer);
}
await node.waClient.sendMessage(numb, inputMessage);
} else if (typeof inputMessage === "object") {
// If it's an object without buttons, convert to string or handle appropriately
const messageText = inputMessage.text || JSON.stringify(inputMessage);
await node.waClient.sendMessage(numb, messageText);
} else {
// Convert to string to ensure valid message
await node.waClient.sendMessage(numb, String(inputMessage));
}
}
catch(e){
node.error(`Error Sending Msg: ${e}`);
@ -36,9 +53,15 @@ module.exports = function(RED) {
try {
let client = await node.waClient;
numb = await formatChatNumberSocket(numb, node.waClient, node);
if (typeof inputMessage ==="string"){
// Format message for socket client
if (typeof inputMessage === "string"){
inputMessage = {text : inputMessage};
} else if (typeof inputMessage === "object" && !inputMessage.text) {
// If object doesn't have text property, convert to string
inputMessage = {text : JSON.stringify(inputMessage)};
}
await client.sendMessage(numb, inputMessage);
}
catch(e) {

View File

@ -20,14 +20,29 @@ module.exports = function(RED) {
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
async function whatsappMessage(gID , inputMessage){
// Validate message
if (!inputMessage) {
node.error('Error Sending Msg: Message payload is empty or undefined');
SetStatus("Message empty", "red");
setTimeout(() => SetStatus('Connected','green'), 3000);
return;
}
if (node.waClient.clientType === "waWebClient"){
try {
gID = await formatGroupId(gID, node.waClient, node);
if(typeof inputMessage === "object"){
// Ensure message is a string for simple messages
if(typeof inputMessage === "object" && inputMessage.buttons){
inputMessage = new Buttons(inputMessage.text, inputMessage.buttons, "text" ,inputMessage.footer);
await node.waClient.sendMessage(gID, inputMessage);
} else if (typeof inputMessage === "object") {
// If it's an object without buttons, convert to string or handle appropriately
const messageText = inputMessage.text || JSON.stringify(inputMessage);
await node.waClient.sendMessage(gID, messageText);
} else {
await node.waClient.sendMessage(gID, inputMessage);
// Convert to string to ensure valid message
await node.waClient.sendMessage(gID, String(inputMessage));
}
}
catch(e){
@ -38,9 +53,15 @@ module.exports = function(RED) {
try {
let client = await node.waClient;
gID = await formatGroupIdSocket(gID, node.waClient, node);
if (typeof inputMessage ==="string"){
// Format message for socket client
if (typeof inputMessage === "string"){
inputMessage = {text : inputMessage};
} else if (typeof inputMessage === "object" && !inputMessage.text) {
// If object doesn't have text property, convert to string
inputMessage = {text : JSON.stringify(inputMessage)};
}
await client.sendMessage(gID, inputMessage);
}
catch(e) {