vernu/textbee

Failed to create SMS batch

scaraliu opened this issue · 5 comments

I set a aws lambada, and it just fails...

Test Event Name
send_sms

Response
{
"statusCode": 200,
"body": "{"success":false,"error":"Failed to create SMS batch","additionalInfo":{}}"
}

Function Logs
2024-05-13T21:22:52.362Z undefined INFO Before importing https
START RequestId: 21a3b6fb-08ab-47f0-b6d5-27eb4ad765d3 Version: $LATEST
2024-05-13T21:22:53.576Z 21a3b6fb-08ab-47f0-b6d5-27eb4ad765d3 INFO {
success: false,
error: 'Failed to create SMS batch',
additionalInfo: {}
}
END RequestId: 21a3b6fb-08ab-47f0-b6d5-27eb4ad765d3
REPORT RequestId: 21a3b6fb-08ab-47f0-b6d5-27eb4ad765d3 Duration: 1285.39 ms Billed Duration: 1286 ms Memory Size: 128 MB Max Memory Used: 72 MB Init Duration: 157.25 ms

import https from 'https';
console.log('Before importing https');

// Constants
const BASE_URL = 'https://api.textbee.dev/api/v1';
const API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Replace with your actual API key
const DEVICE_ID = 'xxxxxxxxxxxxxxxxxxxxxxx'; // Replace with your actual Device ID

export async function handler(event) {
return new Promise((resolve, reject) => {

const options = {
  hostname: 'api.textbee.dev',
  path: `/api/v1/gateway/devices/${DEVICE_ID}/sendSMS`,
  method: 'POST',
  headers: {
    'x-api-key': API_KEY,
  },
};
const req = https.request(options, (res) => {
  let responseBody = '';

  res.on('data', (chunk) => {
    responseBody += chunk;
  });

  res.on('end', () => {
    try {
      const parsedData = JSON.parse(responseBody);
      console.log(parsedData)
      resolve({
        statusCode: 200,
        body: JSON.stringify(parsedData),
      });
    } catch (e) {
      reject({
        statusCode: 500,
        body: JSON.stringify({ message: 'Failed to parse response' }),
      });
    }
  });
});

req.on('error', (error) => {
  console.error('Request error:', error); // Log the error
  reject({
    statusCode: 500,
    body: JSON.stringify({
      message: 'Failed to retrieve data',
      error: error.message,
    }),
  });
});


req.end();

});
}

@scaraliu could you provide the payload you are sending?

it was an issue probably from the load format, as in the object of the post, that was sent (the exact post object i dont have it anymore) that probably was processed badly and also sent badly.
now works.
here is the right post object.
{
"body": "{"recipients": ["+00000000000"], "message": "Hello World!"}"
}
The current version of the aws lambada script
import https from 'https';

// Constants
const BASE_URL = 'https://api.textbee.dev/api/v1';
const API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Replace with your actual API key
const DEVICE_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // Replace with your actual Device ID
const SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxx'; // the request needs this xxxxxxxxxxxxxxxxxx header

export async function handler(event) {

try {
// Check if the request contains the custom authentication header
const authHeader = event.headers["xxxxxxxxxxxxxxxxxxxxxxxx"];
if (!authHeader || authHeader !== SECRET_KEY) {
return {
statusCode: 401,
body: JSON.stringify({ message: "You are not authorized." })
};
}
} catch (e) {
return {
statusCode: 401,
body: JSON.stringify({ message: e })
};
}

// Parse the input event to get recipients and message
const { recipients, message } = JSON.parse(event.body);

return new Promise((resolve, reject) => {

const data = JSON.stringify({
  recipients: recipients,
  message: message
});

const options = {
  hostname: 'api.textbee.dev',
  path: `/api/v1/gateway/devices/${DEVICE_ID}/sendSMS`,
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'x-api-key': API_KEY,
    'Content-Length': Buffer.byteLength(data)
  }
};

const req = https.request(options, (res) => {
  let responseBody = '';

  res.on('data', (chunk) => {
    responseBody += chunk;
  });

  res.on('end', () => {
    try {
      const parsedData = JSON.parse(responseBody);
      console.log(parsedData);
      resolve({
        statusCode: 200,
        body: JSON.stringify(parsedData),
      });
    } catch (e) {
      reject({
        statusCode: 500,
        body: JSON.stringify({ message: 'Failed to parse response' }),
      });
    }
  });
});

req.on('error', (error) => {
  console.error('Request error:', error);
  reject({
    statusCode: 500,
    body: JSON.stringify({
      message: 'Failed to retrieve data',
      error: error.message,
    }),
  });
});

// Write data to request body
req.write(data);
req.end();

});
}

so is it working now?

Yes, my mistake was at the object that was sent, that badly sent to the aws lambada, badly probably was forwarded the api.