Introduction
In this tutorial we will send an SMS in Node.js via SMS Exchange using SMPP Protocol.
SMPP (Short Message Peer-to-Peer) is a protocol used by the telecommunications industry. It exchanges SMS messages between (SMSC) and ESME. SMSC acts as middleman to store the message and route it. ESME is the system that delivers SMS to SMSC.
This tutorial will help you to send SMS messages using TelecomsXChange SMPP Gateway.
Step 1: Create SMPP Session
In beginning, we need to create a new smpp
session with IP address and port.
const smpp = require('smpp');
const session = new smpp.Session({host: 'smpp01.telecomsxchange.com', port: 2775});
Step 2: Bind Transceiver
As soon as it connects we will bind it on connect
event:
let isConnected = false
session.on('connect', () => {
isConnected = true;
session.bind_transceiver({
system_id: 'USER_NAME',
password: 'USER_PASSWORD',
interface_version: 1,
system_type: '380666000600',
address_range: '+380666000600',
addr_ton: 1,
addr_npi: 1,
}, (pdu) => {
if (pdu.command_status == 0) {
console.log('Successfully bound')
}
})
})
session.on('close', () => {
console.log('smpp is now disconnected')
if (isConnected) {
session.connect(); //reconnect again
}
})
session.on('error', error => {
console.log('smpp error', error)
isConnected = false;
});
Step 3: Send SMS
So now we are connected, let’s send the SMS:
function sendSMS(from, to, text) {
from = `+${from}`
// this is very important so make sure you have included + sign before ISD code to send sms
to = `${to}`
session.submit_sm({
source_addr: from,
destination_addr: to,
short_message: text
}, function(pdu) {
if (pdu.command_status == 0) {
// Message successfully sent
console.log(pdu.message_id);
}
});
}
Now after sending the SMS, SMSC will send the delivery report that the message has been delivered.
We will attach a pdu
event on session
. After receiving the delivery report we need to acknowledge to SMSC that we have received the delivery report. Otherwise, SMSC will send that report again.
session.on(‘pdu’, function (pdu) {
if (pdu.command == ‘deliver_sm
‘) {
const sms = {
from: null,
to: null,
message: null
}
sms.from = pdu.source_addr.toString();
sms.to = pdu.destination_addr.toString();
if (pdu.message_payload) {
sms.message = pdu.message_payload.message;
}
console.log(sms);
session.deliver_sm_resp({
sequence_number: pdu.sequence_number
});
}
});