admin管理员组

文章数量:1278976

I'm trying to read the message about the correct or incorrect sending of an sms message by the Yeastar TG200 gateway. In the browser I get this message in html form:

<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">
Response: Success
Message: Commit successfully!
</pre></body></html>

When I try to do this via axios or fetch I always get an error with headers: SMS sending error: Parse Error: Invalid header token Despite error, text message sends correctly.

const smsConfig = {
  ip: "192.168.1.10",
  user: "user",
  pass: "password1q!",
  port: "1", 
};

async function sendDirect(phoneNumber, message, port = "1") {
  if (!phoneNumber || !message) {
    throw new Error("Provide a phone number and the content of the message");
  }
  if (port !== "1" && port !== "2") {
    throw new Error("The port must be 1 or 2");
  }
  //Replace spaces in the body of the message with “+” characters
  const encodedMessage = message.replace(/ /g, '+');

  const url = `http://${smsConfig.ip}/cgi/WebCGI?1500101=account=${smsConfig.user}&password=${smsConfig.pass}&port=${port}&destination=${phoneNumber}&content=${encodedMessage }`;


  console.log(`sending to: ${url}`);

  try {
    const response = await axios.get(url, {
      timeout: 30000,
    });

    console.log("Response from the gate:", response.data);

    if (response.data.toLowerCase().includes("response: success")) {
      console.log(`✅ SMS sent successfully to ${phoneNumber}`);
    } else {
      console.error(`❌ Error when sending SMS: ${response.data}`);
    }
  } catch (error) {
    console.error("SMS sending error:", error.message);
  }
}

async function main() {
  await sendDirect("555555555", "Test Test2");
}

main();

本文标签: nodejsValidation of sending sms through sms gatewayYeastarStack Overflow