Created
September 16, 2019 08:56
-
-
Save AlexLakatos/a70909203bc7a94524907b1ec9fd0c47 to your computer and use it in GitHub Desktop.
How to Send and Receive SMS Messages With Node.js and Express
This file contains 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
const app = require('express')() | |
const bodyParser = require('body-parser') | |
app.use(bodyParser.json()) | |
app.use(bodyParser.urlencoded({ extended: true })) | |
app.listen(3000) |
This file contains 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
$ nexmo number:buy --country_code US --confirm |
This file contains 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
$ touch index.js |
This file contains 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
$ touch server.js |
This file contains 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
app.post('/webhooks/inbound-message', (req, res) => { | |
console.log(req.body); | |
res.status(200).end(); | |
}); |
This file contains 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
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 | |
}) |
This file contains 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
const Nexmo = require('nexmo') | |
const nexmo = new Nexmo({ | |
apiKey: NEXMO_API_KEY, | |
apiSecret: NEXMO_API_SECRET | |
}) |
This file contains 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
$ npm install express body-parser |
This file contains 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
$ npm install nexmo@beta |
This file contains 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
$ npm init | |
$ npm install nexmo |
This file contains 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
$ ngrok http 3000 |
This file contains 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
$ node index.js |
This file contains 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
$ node server.js |
This file contains 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
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.`); | |
} | |
} | |
); |
This file contains 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
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