How to send a SMS from Total.js?
- create an account on Nexmo SMS API (cheap SMS)
- get
key
and secret
Edit your application's /config
:
nexmo-key : YOUR KEY
nexmo-secret : YOUR SECRET
nexmo-sender : Total.js
Create a definition file e.g. /definitions/sms.js
:
F.sms = function(number, message, callback) {
RESTBuilder.make(function(builder) {
builder.url('https://rest.nexmo.com/sms/json?api_key={0}&api_secret={1}&from={2}&to={3}&text={4}&type=unicode'.format(F.config['nexmo-key'], F.config['nexmo-secret'], encodeURIComponent(F.config['nexmo-sender']), number, encodeURIComponent(message)));
builder.exec(function(err, response) {
LOGGER('sms', 'response:', JSON.stringify(response), 'error:', err);
callback && callback(err, response['message-count'] > 0);
});
});
return F;
};
Sending SMS from anywhere in the code:
F.sms('+421903163302', 'Hello Peter from Total.js');
// or
F.sms('+421903163302', 'Hello Peter from Total.js', (err, sent) => console.log(err, sent));