Last active
December 21, 2024 13:36
-
-
Save diegofcornejo/bf670d8d0c1bbd2b636fcd278c45d6a3 to your computer and use it in GitHub Desktop.
Node SMPP Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var app = express(); | |
var smpp = require('smpp'); | |
var session = smpp.connect('smpp://10.10.10.10:4331'); | |
session.auto_enquire_link_period = 1000; | |
var didConnect = false; | |
app.use( | |
bodyParser.json({ | |
limit: '1mb', | |
}) | |
); // parse application/vnd.api+json as json | |
app.use( | |
bodyParser.urlencoded({ | |
limit: '1mb', | |
extended: true, | |
parameterLimit: 1000, | |
}) | |
); // parse application/x-www-form-urlencoded | |
app.use(function (req, res, next) { | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); | |
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); | |
next(); | |
}); | |
app.set('port', process.env.PORT || 4000); | |
var server = app.listen(app.get('port'), function () { | |
console.log('Express server listening on port ' + server.address().port); | |
}); | |
session.on('connect', function () { | |
didConnect = true; | |
session.bind_transceiver( | |
{ | |
system_id: 'USER', | |
password: 'PASS', | |
system_type: 'smpp', | |
interface_version: 52, | |
source_addr_ton: 1, | |
source_addr_ton: 1, | |
dest_addr_ton: 1, | |
dest_addr_npi: 1, | |
}, | |
function (pdu) { | |
console.log(pdu); | |
console.log('pdu status', lookupPDUStatusKey(pdu.command_status)); | |
if (pdu.command_status == 0) { | |
console.log('Successfully bound'); | |
} | |
} | |
); | |
}); | |
function lookupPDUStatusKey(pduCommandStatus) { | |
for (var k in smpp.errors) { | |
if (smpp.errors[k] == pduCommandStatus) { | |
return k; | |
} | |
} | |
} | |
function connectSMPP() { | |
console.log('smpp reconnecting'); | |
session.connect(); | |
} | |
session.on('close', function () { | |
console.log('smpp disconnected'); | |
if (didConnect) { | |
connectSMPP(); | |
} | |
}); | |
session.on('error', function (error) { | |
console.log('smpp error', error); | |
didConnect = false; | |
}); | |
//recibir mensajes | |
// https://stackoverflow.com/questions/21927762/smpp-delivery-receipt-response-not-coming-in-node-smpp | |
// session.on('deliver_sm', function(pdu) { | |
// console.log(pdu.short_message); | |
// session.send(pdu.response()); | |
// }); | |
// https://kalapun.com/posts/working-with-sms-via-smpp-in-nodejs/ | |
session.on('pdu', function (pdu) { | |
// incoming SMS from SMSC | |
//console.log('####### PDU ########'); | |
//console.log(pdu) | |
if (pdu.command == 'deliver_sm') { | |
// // no '+' here | |
var fromNumber = pdu.source_addr.toString(); | |
var toNumber = pdu.destination_addr.toString(); | |
var text = ''; | |
if (pdu.short_message && pdu.short_message.message) { | |
text = pdu.short_message.message; | |
} | |
console.log('SMS ' + fromNumber + ' -> ' + toNumber + ': ' + text); | |
// // Reply to SMSC that we received and processed the SMS | |
session.deliver_sm_resp({ | |
sequence_number: pdu.sequence_number, | |
}); | |
} | |
}); | |
session.on('enquire_link', function (pdu) { | |
session.send(pdu.response()); | |
}); | |
function sendSMS(from, to, text) { | |
// in this example, from & to are integers | |
// We need to convert them to String | |
// and add `+` before | |
return new Promise(function (resolve, reject) { | |
//from = '+' + from.toString(); | |
//to = '+' + to.toString(); | |
session.submit_sm( | |
{ | |
source_addr: from, | |
destination_addr: to, | |
short_message: text, | |
source_addr_ton: 1, | |
source_addr_ton: 1, | |
dest_addr_ton: 1, | |
dest_addr_npi: 1, | |
}, | |
function (pdu) { | |
// console.log('sms pdu status', lookupPDUStatusKey(pdu.command_status)); | |
if (pdu.command_status == 0) { | |
// Message successfully sent | |
// console.log(pdu.message_id); | |
resolve({ | |
status: 'Enviado', | |
phone: to, | |
pdu: pdu.command_status + '-' + lookupPDUStatusKey(pdu.command_status), | |
message_id: pdu.message_id, | |
}); | |
} else { | |
reject({ | |
status: 'No enviado', | |
phone: to, | |
pdu: pdu.command_status + '-' + lookupPDUStatusKey(pdu.command_status), | |
}); | |
} | |
} | |
); | |
}); | |
} | |
app.post('/sms', function (req, res) { | |
var sms = req.body; | |
// console.log(sms) | |
sendSMS(sms.from, sms.to, sms.message).then( | |
(result) => { | |
result.message = sms.message; | |
console.log(result); | |
res.send(result); | |
}, | |
(error) => { | |
error.message = sms.message; | |
console.log(error); | |
res.send(error); | |
} | |
); | |
// var query = "select * from user"; | |
// var params = []; | |
// db.execute(query, params, function(data) { | |
// res.send(data); | |
// }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment