Send SMS messages to your Free Mobile account, in JavaScript.
To use this library, you must have enabled SMS Notifications in the Options of your Subscriber Area.
The latest Node.js and npm versions. If you plan to play with the sources, you will also need the latest Gulp.js version.
Installing via npm
From a command prompt, run:
$ npm install --save @cedx/free-mobile
This package has an API based on Observables.
It provides a single class, Client
, which allow to send messages to your mobile phone by using the sendMessage()
method:
const {Client} = require('@cedx/free-mobile');
let client = new Client({
username: 'your Free Mobile user name',
password: 'your Free Mobile identification key'
});
client.sendMessage('Hello World!').subscribe(
() => console.log('The message was sent successfully.'),
error => console.log(`An error occurred: ${error}`)
);
The text of the messages will be automatically truncated to 160 characters: you can't send multipart messages using this library.
There is also a convenient sendMessage()
function:
const {sendMessage} = require('@cedx/free-mobile');
let credentials = {username: 'your user name', password: 'your identification key'};
sendMessage('Hello World!', credentials).subscribe(
() => console.log('The message was sent successfully.')
);
The Client
class triggers some events during its life cycle:
request
: emitted every time a request is made to the remote service.response
: emitted every time a response is received from the remote service.
These events are exposed as Observable
, you can subscribe to them using the on<EventName>
properties:
client.onRequest.subscribe(
request => console.log(`Client request: ${request.url}`)
);
client.onResponse.subscribe(
response => console.log(`Server response: ${response.statusCode}`)
);
If you require it, an Observable
can be converted to a Promise by using the toPromise()
method:
let promise = client.sendMessage('Hello World!').toPromise();
promise.then(() => console.log('The message was sent successfully.'));
In order to run the tests, you must set two environment variables:
$ export FREEMOBILE_USERNAME="<your Free Mobile user name>"
$ export FREEMOBILE_PASSWORD="<your Free Mobile identification key>"
Then, you can run the test
script from the command prompt:
$ npm test
Free Mobile for JS is distributed under the Apache License, version 2.0.