Skip to content

Commit

Permalink
initial build
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjandrews committed Nov 1, 2019
0 parents commit e710a50
Show file tree
Hide file tree
Showing 11 changed files with 993 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TRANSLATE_IAM_APIKEY=
TRANSLATE_URL=
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env
node_modules
.DS_Store
npm-debug.log
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:11-alpine

EXPOSE 3000
WORKDIR /usr/src/

COPY ./package.json ./package-lock.json ./
RUN npm install

ENV PATH /usr/src/node_modules/.bin:$PATH

COPY . .

CMD ["npm", "start"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Nexmo, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node index.js
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# SMS Text Translator with IBM Watson Language Translation

[![Deploy to Heroku](https://www.herokucdn.com/deploy/button.svg)](https://nexmo.dev/ibm-nexmo-sms-translation-heroku)

This example uses IBM Watson Language Translator to translate SMS messages.

SMS Messages sent through Nexmo will be sent to IBM Language Translator and the translation is returned to the console.

## IBM Tone Analyzer Service

Reference: [https://www.ibm.com/watson/services/language-translator/](https://www.ibm.com/watson/services/language-translator/)
API Docs: [https://cloud.ibm.com/apidocs/language-translator](https://cloud.ibm.com/apidocs/language-translator)
GitHub: [https://github.com/watson-developer-cloud/node-sdk#language-translator](https://github.com/watson-developer-cloud/node-sdk#language-translator)

Register for the [IBM Language Translator service](https://console.bluemix.net/catalog/services/tone-analyzer). Once created, make a note of the API Key and URL for use later. These can be found in the Manage page of the IBM Cloud dashboard after login.


## Running the App

This sample app uses a `.env` file to provide the API key and URL.

Copy the provided `.env.example` file to a new file called `.env`:

```
cp .env.example > .env
```

Then update the values with those from the IBM Tone Analyzer service Manage page, and then save.

```
TRANSLATE_IAM_APIKEY=
TRANSLATE_URL=
```

Also, expose the application to the internet using tools like [ngrok](https://ngrok.com/). To see how, [check out this guide](https://www.nexmo.com/blog/2017/07/04/local-development-nexmo-ngrok-tunnel-dr/).

### Using Docker

To run the app using Docker, run the following command in a terminal:

```
docker-compose up
```

This will create a new image with all the dependencies and run it at http://localhost:3000.

### Using Node

To run the app using node, run the following command in a terminal:

```
npm install && node index.js
```

This will install all the dependencies and run it at http://localhost:3000.

## Linking the app to Nexmo

For this example app a Nexmo number and SMS webhook setup is needed.

This can be achieved with the Nexmo CLI. Install the CLI by following [these instructions](https://github.com/Nexmo/nexmo-cli#installation).

### Rent a New Virtual Number

Renting a number will need to be in place. This can also be achieved using the CLI by running this command:

```
nexmo number:buy --country_code US
```

### Adding the SMS Webhook

Update the number created with the URL of the hosted or local server.

```
nexmo link:sms phone_number https://my-hostname/message
```

## Try it out

With the example Node application running in the terminal, send various SMS messages to the virtual number. The terminal will output the response from IBM Language Translator.


## Extend
This app prints out to the console. For integration with an application, extend the `translateText` function to suit your needs.
14 changes: 14 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "SMS Text Translation with Nexmo & IBM Watson",
"description": "Example using IBM Watson to translate SMS messages.",
"keywords": ["Translation", "sms", "nodejs", "IBM Watson", "nexmo"],
"website": "https://developer.nexmo.com/extend/ibm-watson-speech-to-text",
"repository": "https://github.com/nexmo-community/sms-sentiment-watson",
"env": {
"TRANSLATE_IAM_APIKEY": {
"description": "The API Key from IBM Language Translator Service"
},
"TRANSLATE_URL": {
"description": "The URL from IBM Language Translator Service",
}
}
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.7'
services:
sms-sentiment:
build: .
env_file: .env
ports:
- 5000:5000
volumes:
- ./:/usr/src
- ./node_modules:/usr/src/node_modules
64 changes: 64 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

require('dotenv').config();

const express = require('express');
const bodyParser = require('body-parser');
const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
const { IamAuthenticator } = require('ibm-watson/auth');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

const server = app.listen(process.env.PORT || 3000, () => {
console.log(`Express server listening on port ${server.address().port} in ${app.settings.env} mode`);
});

// Reading the inbound SMS messages
const handleRoute = (req, res) => {

let params = req.body;

if (req.method === "GET") {
params = req.query
}

if (!params.to || !params.msisdn) {
res.status(400).send({'error': 'This is not a valid inbound SMS message!'});
} else {
translateText(params);
res.status(200).end();
}

};

app.route('/message')
.get(handleRoute)
.post(handleRoute)
.all((req, res) => res.status(405).send());

const languageTranslator = new LanguageTranslatorV3({
version: '2017-09-21',
authenticator: new IamAuthenticator({
apikey: process.env.TRANSLATE_IAM_APIKEY,
}),
url: process.env.TRANSLATE_URL,
});

function translateText(params) {
const translateParams = {
text: params.text,
modelId: 'en-es',
};

languageTranslator.translate(translateParams)
.then(translationResult => {
console.log(params.text);
console.dir(translationResult, {depth: null})
})
.catch(err => {
console.log('error:', err);
});
}
Loading

0 comments on commit e710a50

Please sign in to comment.