Node.js: inviare SMS con le API di Clickatell

Node.js: inviare SMS con le API di Clickatell

Con Node.js possiamo inviare SMS con le API di Clickatell.

Quello che ci occorre รจ il nostro auth token e il numero di telefono a cui inviare l'SMS:


const https = require('https');

var options = {
  hostname: 'api.clickatell.com',
  path: '/rest/message',
  method: 'POST',
  headers: {
    'X-Version' : '1',
    'Content-Type' : 'application/json',
    'Authorization' : 'bearer IL_VOSTRO_AUTH_TOKEN',
    'Accept' : 'application/json'
  }
};

var postData = {
    'text' : 'Hello',
    'to' : ['39123456789'] // Non dimentichiamo il prefisso internazionale 39
};

var req = https.request(options, function(res) {
    console.log(res.statusCode);
    console.log(JSON.stringify(res.headers));
    var resp = '';

    res.on('data', function(chunk) {
        resp += chunk;
    });
    res.on('end', function() {
        console.log(resp);
    });
});

req.write(JSON.stringify(postData));
req.end();

Esempio di risposta dalle API:


202
{
  "date":"Sun, 31 Jul 2016 09:56:15 GMT",
  "server":"Apache",
  "content-length":"110",
  "connection":"close",
  "content-type":"application/json"
}
{
"data":
  {
  "message":[
  {
    "accepted":true,
    "to":"39123456789",
    "apiMessageId":"abc1234"
  }
  ]
  }
}

Torna su