From 8d1c19d09273f562987927f02711b2bcfa4c3747 Mon Sep 17 00:00:00 2001 From: Jeffrey Linwood Date: Fri, 14 Jul 2017 17:42:12 -0500 Subject: [PATCH] Merged into SDK Starter --- .gitignore | 3 - .travis.yml | 7 -- Dockerfile | 14 ---- LICENSE | 19 ----- Procfile | 2 - README.md | 57 +------------- config.js | 5 -- create_service.js | 16 ---- docker-compose.yml | 6 -- index.js | 140 --------------------------------- notify.js | 18 ----- package.json | 34 -------- public/facebook_messenger.html | 34 -------- public/node.svg | 17 ---- views/index.jade | 46 ----------- 15 files changed, 2 insertions(+), 416 deletions(-) delete mode 100755 .gitignore delete mode 100644 .travis.yml delete mode 100644 Dockerfile delete mode 100755 LICENSE delete mode 100755 Procfile mode change 100755 => 100644 README.md delete mode 100644 config.js delete mode 100755 create_service.js delete mode 100644 docker-compose.yml delete mode 100755 index.js delete mode 100755 notify.js delete mode 100644 package.json delete mode 100644 public/facebook_messenger.html delete mode 100755 public/node.svg delete mode 100644 views/index.jade diff --git a/.gitignore b/.gitignore deleted file mode 100755 index ee4de9e..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -node_modules -config.json -config.js diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index adbf1e7..0000000 --- a/.travis.yml +++ /dev/null @@ -1,7 +0,0 @@ -sudo: required - -services: - - docker - -script: - - docker build . --no-cache diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index ec457d5..0000000 --- a/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM node:latest - -RUN mkdir -p /usr/src -COPY . /usr/src -WORKDIR /usr/src - -RUN npm install - -# a very simple test -RUN NODE_ENV=test npm start - -EXPOSE 3000 - -CMD [ "npm", "start" ] diff --git a/LICENSE b/LICENSE deleted file mode 100755 index 8fd6ad2..0000000 --- a/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2016 Twilio 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. diff --git a/Procfile b/Procfile deleted file mode 100755 index 4237dd2..0000000 --- a/Procfile +++ /dev/null @@ -1,2 +0,0 @@ -web: node index.js - diff --git a/README.md b/README.md old mode 100755 new mode 100644 index 714906f..cf53591 --- a/README.md +++ b/README.md @@ -1,57 +1,4 @@ -# Notifications Quickstart for Node.js -[![Build Status](https://travis-ci.org/TwilioDevEd/notifications-quickstart-node.svg?branch=master)](https://travis-ci.org/TwilioDevEd/notifications-quickstart-node) +# Notify Quickstart for Node -This application should give you a ready-made starting point for integrating notifications into your -own apps with Twilio Notifications. +This Quickstart application has been merged into the [SDK Starter for Node](https://github.com/TwilioDevEd/sdk-starter-node) application. Please check that GitHub repository for the latest sample code to get you up and running quickly! -# Configure Twilio account information -Before we begin, we need to collect the credentials we need to run the application. - -Credential | Description ----------- | ----------- -Twilio Account SID | Your main Twilio account identifier - [find it in the console](https://www.twilio.com/console). -Twilio Notification_Service SID | Use the create_service.js script to generate this. Just run 'node create_service.js' in your terminal, after you add the above configuration values to the `config.js` file. -Twilio Account Auth Token | Your Twilio authentication token - [find it in the console](https://www.twilio.com/console) - -# Configure Notify push credentials -You will need to create a Notify Service through the [Twilio Console](https://www.twilio.com/console/notify/services), and add at least one credential on the [Mobile Push Credential screen](https://www.twilio.com/console/notify/credentials) (such as Apple Push Notification Service or Firebase Cloud Messaging for Android) to send notifications using Notify. - -For help with setting up native mobile push credentials, please see the following guides: -[Set up push credentials for iOS push notifications](https://www.twilio.com/docs/api/chat/guides/push-notifications-ios) -[Set up push credentials for Android push notifications](https://www.twilio.com/docs/api/chat/guides/push-notifications-android) - -# Setting up the Node.js Application - -Edit the `config.js` file with the Twilio account information collected in the first step above, including your Twilio account SID, Notification Service SID, and Twilio auth token. - -Next, we need to install our dependencies from npm: - -```bash -npm install -``` - -Now we should be all set! Run the application using the `npm` command. - -```bash -npm start -``` - -Your application should now be running at [http://localhost:3000](http://localhost:3000). - -# Usage - -When your app receives a 'registration' in the form of a POST request to the /register endpoint from a mobile client, it will create a binding. A binding is the address Twilio gives your app installation. It lets our service know where to send notifications. - -To send a notification to the client run the notify script - -```bash - node notify IDENTITY_HERE -``` - -The mobile client will receive a notification with the hardcoded 'Hello {IDENTITY}' message. - -That's it! Check out our REST API [docs](http://www.local.twilio.com/docs/api/notifications/rest/overview) for more information on Twilio Notifications. - -## License - -MIT diff --git a/config.js b/config.js deleted file mode 100644 index 22b1895..0000000 --- a/config.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = { - "TWILIO_ACCOUNT_SID": "", // Twilio Account SID from https://www.twilio.com/console - "TWILIO_AUTH_TOKEN": "", // Twilio Auth Token from https://www.twilio.com/console - "TWILIO_NOTIFICATION_SERVICE_SID": "", // Service SID from https://www.twilio.com/console/notify/services -} diff --git a/create_service.js b/create_service.js deleted file mode 100755 index 905d7bd..0000000 --- a/create_service.js +++ /dev/null @@ -1,16 +0,0 @@ -var env = require('./config.js'); -var twilio = require('twilio'); - -// Authenticate with Twilio -var client = new twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN); - -// Create a user notification service instance -var serviceData = { - friendlyName: 'My First Notifications App' -} - -client.notify.v1.services.create(serviceData).then(function(response) { - console.log(response); -}).catch(function(error) { - console.log(error); -}); diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 9438763..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,6 +0,0 @@ -web: - build: . - ports: - - "3000:3000" - volumes: - - .:/usr/src/app diff --git a/index.js b/index.js deleted file mode 100755 index bcfc80f..0000000 --- a/index.js +++ /dev/null @@ -1,140 +0,0 @@ -var _ = require('lodash'); -var http = require('http'); -var express = require('express'); -var bodyParser = require('body-parser'); -var env = require('./config.js'); -var twilio = require('twilio'); - -// Create Express Webapp -var app = express(); -app.use(bodyParser.urlencoded({ extended: true })); -app.use(bodyParser.json()); -app.use(express.static('public')); - -// Basic health check - check environment variables have been configured -// correctly -app.get('/', function(request, response) { - - response.render('index.jade',{ - TWILIO_ACCOUNT_SID: env.TWILIO_ACCOUNT_SID, - TWILIO_AUTH_TOKEN: env.TWILIO_AUTH_TOKEN, - TWILIO_NOTIFICATION_SERVICE_SID: env.TWILIO_NOTIFICATION_SERVICE_SID, - - }); - -}); - -function createBinding(opts, response){ - // Authenticate with Twilio - var client = new twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN); - - // Get a reference to the user notification service instance - var service = client.notify.v1.services(env.TWILIO_NOTIFICATION_SERVICE_SID); - - var execCreateBinding = function(){ - - var params = - { - "identity": opts.identity, - "bindingType": opts.BindingType, - "address": opts.Address - }; - - if (!_.isUndefined(opts.endpoint)) { - params.endpoint = opts.endpoint; - } - - service.bindings.create(params).then(function(binding) { - var message = 'Binding created!'; - console.log(binding); - // Send a JSON response indicating success - response.send({ - endpoint: binding.endpoint, - message: message - }); - }).catch(function(error) { - var message = 'Failed to create binding: ' + error; - console.log(message); - - // Send a JSON response indicating an internal server error - response.status(500).send({ - error: error, - message: message - }); - }); - - } - - // For APNS bindings let's check if the app's provisioning profile is aligned - // with that of the configured certificate. - // Typically this check is unnecessary and wasteful in production but very - // useful if you are doing this the first time so we recommend removing it - // before deploying to production. - if (opts.BindingType === "apn"){ - - service.fetch().then(function(serviceObj){ - return client.notify.v1.credentials(serviceObj.apnCredentialSid).fetch(); - }).then(function(credential){ - if (credential.sandbox !== opts.Sandbox){ - var message = "Mismatched APNS certificate. Make sure you are using a production certificate with a production provisioning profile and a development certificate with a development provisioning profile."; - console.log(message); - console.log("Credential's sandbox value: " + credential.sandbox); - console.log("App's sandbox value: " + opts.Sandbox); - response.status(400).send({ - error: "52134", - message: message - }); - } else{ - execCreateBinding(); - } - }).catch(function(error){ - console.log(error); - response.status(500).send({ - error: error, - message: "Failed to create binding: " + error - }); - }); - } else { //if not APNS binding we can just go ahead and create the Binding - execCreateBinding(); - } -} - -//Create a binding using device properties -app.post('/register', function(request, response) { - createBinding(request.body, - response); -}); - -//Create a facebook-messenger binding based on the authentication webhook from Facebook -app.post('/messenger_auth', function(request, response) { - //Extract the request received from Facebook - var message = request.body.entry[0].messaging[0]; - console.log(message); - // Set user identity using their fb messenger user id - var identity = message.sender.id; - //Let's create a new facebook-messenger Binding for our user - createBinding({ - "identity":identity, - "BindingType":'facebook-messenger', - "Address":message.sender.id - }, - response); -}); - -//Verification endpoint for Facebook needed to register a webhook. -app.get('/messenger_auth', function(request, response) { - console.log(request.query["hub.challenge"]); - response.send(request.query["hub.challenge"]); -}); - - - -// Start HTTP server -var server = http.createServer(app); -var port = process.env.PORT || 3000; -server.listen(port, function() { - console.log('Express server running on *:' + port); - if (process.env.NODE_ENV == 'test') { - process.exit(); - } -}); diff --git a/notify.js b/notify.js deleted file mode 100755 index 2e08c5b..0000000 --- a/notify.js +++ /dev/null @@ -1,18 +0,0 @@ -var env = require('./config.js'); -var twilio = require('twilio'); - -// Authenticate with Twilio -var client = new twilio(env.TWILIO_ACCOUNT_SID, env.TWILIO_AUTH_TOKEN); - -// Create a reference to the user notification service -var service = client.notify.v1.services(env.TWILIO_NOTIFICATION_SERVICE_SID); - -// Send a notification -service.notifications.create({ - 'identity': '' + process.argv.slice(2), - 'body': 'Hello, World!' -}).then(function(response) { - console.log(response); -}).catch(function(error) { - console.log(error); -}); diff --git a/package.json b/package.json deleted file mode 100644 index 3fc94b0..0000000 --- a/package.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "name": "notifications-quickstart-node", - "version": "0.3.0", - "description": "A sample Notifications app", - "main": "index.js", - "scripts": { - "start": "node index.js" - }, - "engines": { - "node": "4.0.0" - }, - "dependencies": { - "body-parser": "^1.15.0", - "express": "^4.13.3", - "jade": "^1.11.0", - "twilio": "3.0.0-alpha-1", - "fb": "^1.1.1" - }, - "repository": { - "type": "git", - "url": "https://github.com/TwilioDevEd/notifications-quickstart-node" - }, - "keywords": [ - "node", - "heroku", - "express" - ], - "author": "Siraj Raval", - "contributors": [ - "Kevin Whinnery ", - "Jeff Linwood " - ], - "license": "MIT" -} diff --git a/public/facebook_messenger.html b/public/facebook_messenger.html deleted file mode 100644 index e166a66..0000000 --- a/public/facebook_messenger.html +++ /dev/null @@ -1,34 +0,0 @@ - - -
-
- - diff --git a/public/node.svg b/public/node.svg deleted file mode 100755 index 3c7e161..0000000 --- a/public/node.svg +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - diff --git a/views/index.jade b/views/index.jade deleted file mode 100644 index 3f1836c..0000000 --- a/views/index.jade +++ /dev/null @@ -1,46 +0,0 @@ -doctype html -html - head - title Notifications Quickstart - style. - * { font-family: Helvetica; } - table { border-collapse:collapse; } - th { - margin:0; - border: 1px solid #ccc; - background-color: #eee; - text-align:left; - padding:5px; - } - td { - margin:0; - border: 1px solid #ccc; - padding:5px; - } - .unset { color: red; font-weight: bold; } - .good { color: green; font-weight: bold; } - body - h1 Notifications Environment Setup - p The following system environment variables should be set: - table - - tr - th TWILIO_NOTIFICATION_SERVICE_SID - if TWILIO_NOTIFICATION_SERVICE_SID - td.good= TWILIO_NOTIFICATION_SERVICE_SID - else - td.unset Not Configured - - tr - th TWILIO_ACCOUNT_SID - if TWILIO_ACCOUNT_SID - td.good= TWILIO_ACCOUNT_SID - else - td.unset Not Configured - - tr - th TWILIO_AUTH_TOKEN - if TWILIO_AUTH_TOKEN - td.good Secret token configured - else - td.unset Not Configured