How can I modify my existing Green-Api WhatsApp code to include options for sending both text and media messages?

This topic was automatically generated from Slack. You can find the original thread here.

this is code you helped me to write using Green-Api to send whatsapp:
for (let i = 0; i < students.length; i++) {
const chatId = ${students[i][**this**.whatsapp]}@c.us;
// const message = ${this.content};
const message = this.content.replace(/\/g, ‘’);
console.log(“מספר:”, students[i][this.whatsapp]);

      **try** {
    **await** [axios.post](http://axios.post)(apiUrl, {
      chatId,
      message
    });

now, this one sends only text.
I want to add media messages.
I have the var:
" message_type"
that can be SendMessage for text-only messages, or “SendFileByUrl” for media.

This is example code for sending media:
{
“chatId”: “11001234567@c.us”,
“urlFile”: “https://my.site.com/img/horse.png”,
“fileName”: “horse.png”,
“caption”: “Little horse”
}
now I want to add to my code “if” that checks the var “message_type” and gives the right code for each optin.

Sure, you can add a conditional statement to check the value of “message_type” and then send the appropriate type of message. Here’s how you can modify your code: for (let i = 0; i < students.length; i++) { const chatId = `${students[i][this.whatsapp]}@c.us`; const message = this.content.replace(/\//g, ''); console.log("מספר:", students[i][this.whatsapp]); try { if (this.message_type === "SendMessage") { await [axios.post](http://axios.post)(apiUrl, { chatId, message }); } else if (this.message_type === "SendFileByUrl") { await [axios.post](http://axios.post)(apiUrl, { chatId, urlFile: "https://my.site.com/img/horse.png", // replace with your URL fileName: "horse.png", // replace with your file name caption: "Little horse" // replace with your caption }); } } catch (error) { console.error(error); } } Please replace the urlFile, fileName, and caption with your own values. This code will check the message_type and send a text message if it’s “SendMessage”, or a media message if it’s “SendFileByUrl”.

if I have props “file_name” and “file_url” what is the problem in this code? and please fix it:
for (let i = 0; i < students.length; i++) {
const chatId = ${students[i][**this**.whatsapp]}@c.us;
const message = this.content.replace(///g, ‘’);
console.log(“מספר:”, students[i][this.whatsapp]);

try {
if (this.message_type === “SendMessage”) {
await axios.post(apiUrl, {
chatId,
message
});
} else if (this.message_type === “SendFileByUrl”) {
await axios.post(apiUrl, {
chatId,
urlFile: this.file_url
fileName: this.file_name
caption: message
});
}
} catch (error) {
console.error(error);
}
}

The problem with your code is that you’re missing commas to separate the properties inside the object you’re passing to axios.post when the message_type is “SendFileByUrl”. Here’s the corrected code: ```
for (let i = 0; i < students.length; i++) { const chatId = ${students[i][this.whatsapp]}@c.us; const message = this.content.replace(///g, ‘’); console.log(“מספר:”, students[i][this.whatsapp]); try { if (this.message_type === “SendMessage”) { await axios.post(apiUrl, { chatId, message }); } else if (this.message_type === “SendFileByUrl”) { await axios.post(apiUrl, { chatId, urlFile: this.file_url, fileName: this.file_name, caption: message }); } } catch (error) { console.error(error); } }

I’m sorry, but your question seems to be missing. Could you please provide more details or clarify what you need help with?

still there’s error Unexpected token

I’m sorry to hear that you’re still encountering an error. The “Unexpected token” error usually occurs when there’s a syntax issue in the code. Could you please provide the exact error message or share the code snippet where the error is occurring? This will help me identify the issue and provide a more accurate solution.

this it the code:

for (let i = 0; i < students.length; i++) {
  const chatId = `${students[i][this.whatsapp]}@c.us`;
  const message = this.content.replace(/\//g, '');
  console.log("מספר:", students[i][this.whatsapp]);

  try {
    if (this.message_type === "SendMessage") {
      await [axios.post](http://axios.post)(apiUrl, {
        chatId,
        message
      });
    } else if (this.message_type === "SendFileByUrl") {
      await [axios.post](http://axios.post)(apiUrl, {
        chatId,
        urlFile: this.file_url,
        fileName: this.file_name,
        caption: message
      });
    }
  } catch (error) {
    console.error(error);
  }
}

this is the error:

SyntaxError: Unexpected token (95:8)
at pp$4.raise (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2927:15)
at pp.unexpected (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:698:10)
at pp.expect (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:692:28)
at pp$3.parseObj (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2563:14)
at pp$3.parseExprAtom (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2302:19)
at anonymous.parseExprAtom (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn-node/lib/import-meta/index.js:27:102)
at pp$3.parseExprSubscripts (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2129:21)
at pp$3.parseMaybeUnary (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2106:19)
at anonymous.parseMaybeUnary (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn-node/lib/private-class-elements/index.js:151:54)
at pp$3.parseExprOps (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2041:21)