-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nexmo-community/update_autoresponder
Updates How to Send and Receive SMS Messages With Node.js and Express
- Loading branch information
Showing
8 changed files
with
4,340 additions
and
165 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
# nexmo-sms-autoresponder-node | ||
# vonage-sms-autoresponder-node | ||
|
||
https://www.nexmo.com/blog/2019/09/11/send-receive-sms-messages-node-express-javascript-dr/ | ||
[Blog post explaining this repository](https://learn.vonage.com/blog/2019/09/16/how-to-send-and-receive-sms-messages-with-node-js-and-express-dr/) | ||
|
||
## Welcome to Vonage | ||
|
||
If you're new to Vonage, you can [sign up for a Vonage account](https://dashboard.nexmo.com/sign-up?utm_source=DEV_REL&utm_medium=github&utm_campaign=firebase-functions-sms-example) and get some free credit to get you started. | ||
|
||
## Getting Help | ||
|
||
We love to hear from you so if you have questions, comments or find a bug in the project, let us know! You can either: | ||
|
||
* Open an issue on this repository | ||
* Tweet at us! We're [@VonageDev on Twitter](https://twitter.com/VonageDev) | ||
* Or [join the Vonage Community Slack](https://developer.vonage.com/community/slack) | ||
|
||
## Further Reading | ||
|
||
* Check out the Developer Documentation at <https://developer.vonage.com> | ||
* Details about Vonage SMS Functionality <https://developer.vonage.com/messaging/sms/overview> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,68 @@ | ||
const Nexmo = require('nexmo') | ||
import dotenv from 'dotenv' | ||
import express from 'express' | ||
import request from 'request' | ||
import Vonage from '@vonage/server-sdk' | ||
|
||
const app = require('express')() | ||
const bodyParser = require('body-parser') | ||
dotenv.config() | ||
|
||
const request = require('request') | ||
const { | ||
json, | ||
urlencoded | ||
} = express | ||
|
||
app.use(bodyParser.json()) | ||
app.use(bodyParser.urlencoded({ | ||
extended: true | ||
})) | ||
const app = express() | ||
|
||
app.listen(3000) | ||
const vonage = new Vonage({ | ||
applicationId: process.env.VONAGE_APPLICATION_ID, | ||
privateKey: process.env.VONAGE_APPLICATION_PRIVATE_KEY_PATH | ||
}) | ||
|
||
const nexmo = new Nexmo({ | ||
apiKey: process.env.NEXMO_API_KEY, | ||
apiSecret: process.env.NEXMO_API_SECRET, | ||
applicationId: process.env.NEXMO_APPLICATION_ID, | ||
privateKey: process.env.NEXMO_APPLICATION_PRIVATE_KEY_PATH | ||
}); | ||
app.use(json()) | ||
app.use(urlencoded({ | ||
extended: true | ||
})) | ||
|
||
var text = "👋Hello from Nexmo"; | ||
app.listen(3000, () => { | ||
console.log('Server listening at http://localhost:3000') | ||
}) | ||
|
||
app.post('/webhooks/inbound', (req, res) => { | ||
console.log(req.body) | ||
|
||
var number = parseInt(req.body.text) || 42; | ||
let text = "The Numbers API has thrown an error." | ||
const number = parseInt(req.body.message.content.text) || 42 | ||
|
||
request(`http://numbersapi.com/${number}`, (error, response, body) => { | ||
if (error) { | ||
text = "The Numbers API has thrown an error." | ||
} else { | ||
if (!error) { | ||
text = body | ||
} | ||
|
||
nexmo.channel.send( | ||
{ "type": "sms", "number": req.body.msisdn }, | ||
{ "type": "sms", "number": req.body.to }, | ||
{ | ||
vonage.channel.send({ | ||
"type": "sms", | ||
"number": req.body.from.number | ||
}, { | ||
"type": "sms", | ||
"number": req.body.to.number | ||
}, { | ||
"content": { | ||
"type": "text", | ||
"text": text | ||
} | ||
}, | ||
(err, responseData) => { | ||
if (err) { | ||
console.log("Message failed with error:", err); | ||
console.log("Message failed with error:", err) | ||
} else { | ||
console.log(`Message ${responseData.message_uuid} sent successfully.`); | ||
console.log(`Message ${responseData.message_uuid} sent successfully.`) | ||
} | ||
} | ||
); | ||
) | ||
|
||
res.status(200).end() | ||
}) | ||
|
||
res.status(200).end(); | ||
app.post('/webhooks/status', (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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
NEXMO_API_KEY= | ||
NEXMO_API_SECRET= | ||
NEXMO_APPLICATION_ID= | ||
NEXMO_APPLICATION_PRIVATE_KEY_PATH=./private.key | ||
TO_NUMBER= | ||
VONAGE_API_KEY= | ||
VONAGE_API_SECRET= | ||
VONAGE_APPLICATION_ID= | ||
VONAGE_APPLICATION_PRIVATE_KEY_PATH=./private.key | ||
TO_NUMBER= |
Oops, something went wrong.