Skip to content

Commit

Permalink
Merge pull request #1 from Nexmo/markl-update-private-sms-tutorial
Browse files Browse the repository at this point in the history
Markl update private sms tutorial
  • Loading branch information
lornajane authored Jan 8, 2019
2 parents 6a7783f + cf73c53 commit 9ddd12f
Show file tree
Hide file tree
Showing 9 changed files with 929 additions and 339 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# SMS Proxy using Node and the Nexmo SMS API

This app used the Nexmo SMS API to demonstrate how to use build an SMS proxy for masked communication between users.
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.

## Prerequisites

You will need:

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

## Installation

Expand All @@ -25,33 +25,34 @@ Rename the config file:
mv example.env .env
```

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

If preferred you can set previously provisioned numbers in the `PROVISIONED_NUMBERS` configuration value.
If you do not have a virtual number, you can purchase one via the [dashboard](https://dashboard.nexmo.com).

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`

### Running the App

```sh
npm start
```

The application should be available on <http://localhost:5000>.
The application should be available on `http://localhost:3000`.

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

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>:
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`
and replacing `USER_A_NUMBER` and `USER_B_NUMBER` with the real numbers of the parties involved:

```
POST /conversation HTTP/1.1
Host: localhost:5000
POST /chat HTTP/1.1
Host: localhost:3000
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
userANumber=USER_A_NUMBER&userBNumber=USER_B_NUMBER
```

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.

You can see a list of registered conversations by accessing <http://localhost:5000/conversations>.
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.
73 changes: 73 additions & 0 deletions SmsProxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const Nexmo = require('nexmo');

class SmsProxy {

constructor() {
this.nexmo = new Nexmo({
apiKey: process.env.NEXMO_API_KEY,
apiSecret: process.env.NEXMO_API_SECRET
}, {
debug: true
});
}

createChat(userANumber, userBNumber) {
this.chat = {
userA: userANumber,
userB: userBNumber
};

this.sendSMS();
}

sendSMS() {
/*
Send a message from userA to the virtual number
*/
this.nexmo.message.sendSms(this.chat.userA,
process.env.VIRTUAL_NUMBER,
'Reply to this SMS to talk to UserA');

/*
Send a message from userB to the virtual number
*/
this.nexmo.message.sendSms(this.chat.userB,
process.env.VIRTUAL_NUMBER,
'Reply to this SMS to talk to UserB');
}


getDestinationRealNumber(from) {
let destinationRealNumber = null;

// Use `from` numbers to work out who is sending to whom
const fromUserA = (from === this.chat.userA);
const fromUserB = (from === this.chat.userB);

if (fromUserA || fromUserB) {
destinationRealNumber = fromUserA ? this.chat.userB : this.chat.userA;
}

return destinationRealNumber;
}

proxySms(from, text) {
// Determine which real number to send the SMS to
const destinationRealNumber = this.getDestinationRealNumber(from);

if (destinationRealNumber === null) {
console.log(`No chat found for this number`);
return;
}

// Send the SMS from the virtual number to the real number
this.nexmo.message.sendSms(process.env.VIRTUAL_NUMBER,
destinationRealNumber,
text);
}

}

module.exports = SmsProxy;
14 changes: 0 additions & 14 deletions config.js

This file was deleted.

4 changes: 1 addition & 3 deletions example.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
NEXMO_API_KEY=
NEXMO_API_SECRET=
NEXMO_DEBUG=true
PROVISIONED_NUMBERS=[{"country":"2_CHAR_CODE","msisdn":"NUMBER"}]
SMS_WEBHOOK_URL=
VIRTUAL_NUMBER=
229 changes: 0 additions & 229 deletions lib/SmsProxy.js

This file was deleted.

Loading

0 comments on commit 9ddd12f

Please sign in to comment.