Skip to content

Instantly share code, notes, and snippets.

@AlexLakatos
Created September 16, 2019 08:56
Show Gist options
  • Save AlexLakatos/a70909203bc7a94524907b1ec9fd0c47 to your computer and use it in GitHub Desktop.
Save AlexLakatos/a70909203bc7a94524907b1ec9fd0c47 to your computer and use it in GitHub Desktop.
How to Send and Receive SMS Messages With Node.js and Express
const app = require('express')()
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.listen(3000)
$ nexmo number:buy --country_code US --confirm
$ touch index.js
$ touch server.js
app.post('/webhooks/inbound-message', (req, res) => {
console.log(req.body);
res.status(200).end();
});
const Nexmo = require('nexmo')
const nexmo = new Nexmo({
apiKey: NEXMO_API_KEY,
apiSecret: NEXMO_API_SECRET,
applicationId: NEXMO_APPLICATION_ID,
privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
})
const Nexmo = require('nexmo')
const nexmo = new Nexmo({
apiKey: NEXMO_API_KEY,
apiSecret: NEXMO_API_SECRET
})
$ npm install express body-parser
$ npm install nexmo@beta
$ npm init
$ npm install nexmo
$ ngrok http 3000
$ node index.js
$ node server.js
let text = "👋Hello from Nexmo";
nexmo.channel.send(
{ "type": "sms", "number": "TO_NUMBER" },
{ "type": "sms", "number": "Nexmo" },
{
"content": {
"type": "text",
"text": text
}
},
(err, responseData) => {
if (err) {
console.log("Message failed with error:", err);
} else {
console.log(`Message ${responseData.message_uuid} sent successfully.`);
}
}
);
let text = "👋Hello from Nexmo";
nexmo.message.sendSms("Nexmo", "TO_NUMBER", text, {
type: "unicode"
}, (err, responseData) => {
if (err) {
console.log(err);
} else {
if (responseData.messages[0]['status'] === "0") {
console.log("Message sent successfully.");
} else {
console.log(`Message failed with error: ${responseData.messages[0]['error-text']}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment