Kirim Beberapa Pesan Text Sekaligus

OneSender support kirim beberapa pesan ke beberapa tujuan dalam sekali request.

Misalnya, anda membuat notifikasi OTP. Ketika user login, sistem akan mengirimkan dua pesan. Ke user berupa kode OTP. Dan kepada admin yang isinya informasi hendak masuk ke member area.

Anda dapat mengirimkan dua notifikasi yang berbeda dengan request seperti berikut.

Request #

[
    {
        "phone": "6289508618321",
        "message": "Hello admin, user #1 baru saja login"
    },
    {
        "phone": "6289500000001",
        "message": "Hello user #1, kode OTP anda: 123456"
    }
]

Response #

Output request jika berhasil seperti berikut:

{
    "code": 200,
    "data": [
        {
            "id": 59,
            "phone": "6289508618321",
            "message": "Hello admin, user #1 baru saja login",
            "date": "2021-10-30T13:22:25+07:00"
        },
        {
            "id": 60,
            "phone": "6289500000001",
            "message": "Hello user #1, kode OTP anda: 123456",
            "date": "2021-10-30T13:22:25+07:00"
        }
    ],
}

Contoh Kode Terapan #

Berikut ini contoh aplikasinya di source code.

PHP #

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://localhost:3000/api/send/texts',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'[
    {
        "phone": "6289508618321",
        "message": "Hello admin, user #1 baru saja login"
    },
    {
        "phone": "6289500000001",
        "message": "Hello user #1, kode OTP anda: 123456"
    }
]',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer 8006652091be85c721ec61810cbacdf5',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Javascript #

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer 8006652091be85c721ec61810cbacdf5");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify([{"phone":"6289508618321","message":"Hello admin, user #1 baru saja login"},{"phone":"6289500000001","message":"Hello user #1, kode OTP anda: 123456"}]);

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("http://localhost:3000/api/send/texts", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Javascript (JQuery) #

var settings = {
  "url": "http://localhost:3000/api/send/texts",
  "method": "POST",
  "timeout": 0,
  "headers": {
    "Authorization": "Bearer 8006652091be85c721ec61810cbacdf5",
    "Content-Type": "application/json"
  },
  "data": JSON.stringify([{"phone":"6289508618321","message":"Hello admin, user #1 baru saja login"},{"phone":"6289500000001","message":"Hello user #1, kode OTP anda: 123456"}]),
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

Powered by BetterDocs