Skip to content

Commit 9ddd12f

Browse files
authored
Merge pull request #1 from Nexmo/markl-update-private-sms-tutorial
Markl update private sms tutorial
2 parents 6a7783f + cf73c53 commit 9ddd12f

9 files changed

Lines changed: 929 additions & 339 deletions

File tree

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# SMS Proxy using Node and the Nexmo SMS API
22

3-
This app used the Nexmo SMS API to demonstrate how to use build an SMS proxy for masked communication between users.
3+
This app uses the Nexmo SMS API to demonstrate how to build an SMS proxy for private communication between users. Each user sees only the other party's virtual number, not their real one.
44

55
## Prerequisites
66

77
You will need:
88

99
* A [free Nexmo account](https://dashboard.nexmo.com/sign-up)
10-
* Somewhere to host this web app, Heroku or Your Local Machine with ngrok both work well
10+
* Somewhere to host this web app: Heroku or your local machine with ngrok both work well
1111

1212
## Installation
1313

@@ -25,33 +25,34 @@ Rename the config file:
2525
mv example.env .env
2626
```
2727

28-
Fill in the values in `.env` as appropriate.
28+
Fill in the values in `.env` as appropriate; this will be your API key and secret, and the Nexmo number you want to use.
2929

30-
If preferred you can set previously provisioned numbers in the `PROVISIONED_NUMBERS` configuration value.
30+
If you do not have a virtual number, you can purchase one via the [dashboard](https://dashboard.nexmo.com).
31+
32+
Configure the number's SMS webhook URL to point to your application (if you are using [ngrok](https://ngrok.com) then start your tunnel now), e.g. `https://abcd1234.ngrok.io/webhooks/inbound-sms`
3133

3234
### Running the App
3335

3436
```sh
3537
npm start
3638
```
3739

38-
The application should be available on <http://localhost:5000>.
40+
The application should be available on `http://localhost:3000`.
3941

40-
If you have not set up predefined numbers you can access <http://localhost:5000/provision> for the application to provision numbers.
42+
> To change the port, try `PORT=3001 npm start` as an alternative command
4143
4244
### Using the App
4345

44-
Register a conversation with the application so that mappings can be created between real user numbers and Nexmo virtual numbers. This is done by making a `POST` such as the following to <http://localhost:5000/conversation>:
46+
Register a conversation with the application so that mappings can be created between real user numbers and Nexmo virtual numbers. This is done by making a `POST` such as the following to `http://localhost:3000/chat`
47+
and replacing `USER_A_NUMBER` and `USER_B_NUMBER` with the real numbers of the parties involved:
4548

4649
```
47-
POST /conversation HTTP/1.1
48-
Host: localhost:5000
50+
POST /chat HTTP/1.1
51+
Host: localhost:3000
4952
Cache-Control: no-cache
5053
Content-Type: application/x-www-form-urlencoded
5154
5255
userANumber=USER_A_NUMBER&userBNumber=USER_B_NUMBER
5356
```
5457

55-
When you do this each of the users will receive a text. Reply to that text will allow the to communicate anonymously with each other.
56-
57-
You can see a list of registered conversations by accessing <http://localhost:5000/conversations>.
58+
When you do this each of the users will receive a text. Replying to that text will allow the users to communicate anonymously with each other.

SmsProxy.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict'
2+
3+
const Nexmo = require('nexmo');
4+
5+
class SmsProxy {
6+
7+
constructor() {
8+
this.nexmo = new Nexmo({
9+
apiKey: process.env.NEXMO_API_KEY,
10+
apiSecret: process.env.NEXMO_API_SECRET
11+
}, {
12+
debug: true
13+
});
14+
}
15+
16+
createChat(userANumber, userBNumber) {
17+
this.chat = {
18+
userA: userANumber,
19+
userB: userBNumber
20+
};
21+
22+
this.sendSMS();
23+
}
24+
25+
sendSMS() {
26+
/*
27+
Send a message from userA to the virtual number
28+
*/
29+
this.nexmo.message.sendSms(this.chat.userA,
30+
process.env.VIRTUAL_NUMBER,
31+
'Reply to this SMS to talk to UserA');
32+
33+
/*
34+
Send a message from userB to the virtual number
35+
*/
36+
this.nexmo.message.sendSms(this.chat.userB,
37+
process.env.VIRTUAL_NUMBER,
38+
'Reply to this SMS to talk to UserB');
39+
}
40+
41+
42+
getDestinationRealNumber(from) {
43+
let destinationRealNumber = null;
44+
45+
// Use `from` numbers to work out who is sending to whom
46+
const fromUserA = (from === this.chat.userA);
47+
const fromUserB = (from === this.chat.userB);
48+
49+
if (fromUserA || fromUserB) {
50+
destinationRealNumber = fromUserA ? this.chat.userB : this.chat.userA;
51+
}
52+
53+
return destinationRealNumber;
54+
}
55+
56+
proxySms(from, text) {
57+
// Determine which real number to send the SMS to
58+
const destinationRealNumber = this.getDestinationRealNumber(from);
59+
60+
if (destinationRealNumber === null) {
61+
console.log(`No chat found for this number`);
62+
return;
63+
}
64+
65+
// Send the SMS from the virtual number to the real number
66+
this.nexmo.message.sendSms(process.env.VIRTUAL_NUMBER,
67+
destinationRealNumber,
68+
text);
69+
}
70+
71+
}
72+
73+
module.exports = SmsProxy;

config.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

example.env

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
NEXMO_API_KEY=
22
NEXMO_API_SECRET=
3-
NEXMO_DEBUG=true
4-
PROVISIONED_NUMBERS=[{"country":"2_CHAR_CODE","msisdn":"NUMBER"}]
5-
SMS_WEBHOOK_URL=
3+
VIRTUAL_NUMBER=

lib/SmsProxy.js

Lines changed: 0 additions & 229 deletions
This file was deleted.

0 commit comments

Comments
 (0)