diff --git a/README.md b/README.md index 869aed3..3f6b795 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ + + Twilio + + # Server notifications with Twilio, Node.js, and Express [![Build Status](https://travis-ci.org/TwilioDevEd/call-tracking-node.svg?branch=master)](https://travis-ci.org/TwilioDevEd/server-notifications-node) @@ -6,66 +10,70 @@ Use Twilio to send SMS alerts so that you never miss a critical issue. [Read the full tutorial here](https://www.twilio.com/docs/tutorials/walkthrough/server-notifications/node/express)! -## Running the Project on Your Machine +## Local development -To run this project on your computer, download or clone the source. You will -also need to download and install either [Node.js](http://nodejs.org/) -or [io.js](https://iojs.org/en/index.html), both of which should also install -[npm](https://www.npmjs.com/). +To run this project on your computer you will + need to download and install either [Node.js](http://nodejs.org/) +or [io.js](https://iojs.org/en/index.html), both of which should also install +[npm](https://www.npmjs.com/). -You will also need to [sign up for a Twilio account](https://www.twilio.com/try-twilio) +You will also need to [sign up for a Twilio account](https://www.twilio.com/try-twilio) if you don't have one already. -### Install Dependencies +1. First clone this repository and `cd` into it. -Navigate to the project directory in your terminal and run: + ```bash + $ git clone git@github.com:TwilioDevEd/server-notifications-node.git + $ cd server-notifications-node + ``` -```bash -npm install -``` +1. Next, open `.env.example` at the root of the project and update it with + values from your + [Twilio account](https://www.twilio.com/console) + and local configuration. Save the file as `.env`. You'll need to set + `TWILIO_AUTH_TOKEN`, `TWILIO_ACCOUNT_SID`, and `TWILIO_NUMBER`. -This should install all of our project dependencies from npm into a local -`node_modules` folder. + For the `TWILIO_NUMBER` variable you'll need to provision a new number + in the + [Manage Numbers page](https://www.twilio.com/user/account/phone-numbers/incoming) + under your account. The phone number should be in + [E.164 format](https://www.twilio.com/help/faq/phone-numbers/how-do-i-format-phone-numbers-to-work-internationally) -### Configuration + Run `source .env` to export the environment variables. -Next, open `.env.sample` at the root of the project and update it with -values from your -[Twilio account](https://www.twilio.com/user/account/voice-messaging) -and local configuration. Save the file as `.env`. You'll need to set -`TWILIO_AUTH_TOKEN`, `TWILIO_ACCOUNT_SID`, and `TWILIO_NUMBER`. +1. Navigate to the project directory in your terminal and run: -For the `TWILIO_NUMBER` variable you'll need to provision a new number -in the -[Manage Numbers page](https://www.twilio.com/user/account/phone-numbers/incoming) -under your account. The phone number should be in -[E.164 format](https://www.twilio.com/help/faq/phone-numbers/how-do-i-format-phone-numbers-to-work-internationally) + ```bash + $ npm install + ``` -### Running the Project + This should install all of our project dependencies from npm into a local + `node_modules` folder. -To launch the application, you can use `node .` in the project's root directory. -You might also consider using [nodemon](https://github.com/remy/nodemon) for -this. It works just like the node command, but automatically restarts your -application when you change any source code files. +1. Make sure the tests succeed. -Make sure you have customized the `config/administrators.json` file -with your phone number and then open -[http://localhost:3000/error](http://localhost:3000/error). You'll get a text message shortly -informing you of an exception. + ```bash + $ npm test + ``` -```bash -npm install -g nodemon -nodemon . -``` +1. To launch the application, you can use `node .` in the project's root directory. + You might also consider using [nodemon](https://github.com/remy/nodemon) for + this. It works just like the node command, but automatically restarts your + application when you change any source code files. -### Running Tests + Make sure you have customized the `config/administrators.json` file + with your phone number and then open + [http://localhost:3000/error](http://localhost:3000/error). You'll get a text message shortly + informing you of an exception. -Basic functional tests can be run with: + ```bash + $ npm install -g nodemon + $ nodemon . + ``` -```bash -npm test -``` -## License +## Meta -MIT +* No warranty expressed or implied. Software is as is. Diggity. +* [MIT License](http://www.opensource.org/licenses/mit-license.html) +* Lovingly crafted by Twilio Developer Education. diff --git a/package.json b/package.json index 5a25be7..c7cd1bd 100644 --- a/package.json +++ b/package.json @@ -29,17 +29,17 @@ "body-parser": "^1.12.0", "connect-flash": "^0.1.1", "csurf": "^1.8.3", - "dotenv": "^1.2.0", + "dotenv": "^2.0.0", "express": "^4.12.0", "express-session": "^1.10.3", "morgan": "^1.5.1", "sanitize-html": "^1.10.0", - "twilio": "^1.11.1", + "twilio": "3.0.0-rc.13", "underscore": "^1.8.3" }, "devDependencies": { - "chai": "^2.1.0", - "mocha": "^2.3.0", - "supertest": "^1.1.0" + "chai": "^3.5.0", + "mocha": "^3.1.2", + "supertest": "^2.0.1" } } diff --git a/tutorial/index.jade b/tutorial/index.jade index 1fb0fdf..534fad7 100644 --- a/tutorial/index.jade +++ b/tutorial/index.jade @@ -3,12 +3,12 @@ :markdown ## What does this thing do? - This [Express.js](http://expressjs.com/) sample application + This [Express.js](//expressjs.com/) sample application demonstrates how to send SMS alerts to a list of system administrators if something goes wrong on your server. In this tutorial, we'll show you the key bits of code to make this work. Head to the [application's - README.md](https://github.com/TwilioDevEd/server-notifications-node) + README.md](//github.com/TwilioDevEd/server-notifications-node) to see how to run the application locally. Let's get started! @@ -16,15 +16,15 @@ --- **See Also:** - * [Getting started with Express.js](http://expressjs.com/starter/hello-world.html) - * [twilio-node library](http://twilio.github.io/twilio-node/) + * [Getting started with Express.js](//expressjs.com/starter/hello-world.html) + * [twilio-node library](//twilio.github.io/twilio-node/) .step(data-file='config.js', data-highlight='23-45') :markdown ## Configure Twilio client In order to send messages we'll need to create a [Twilio REST - client](http://twilio.github.io/twilio-node/#quickstart), which + client](//twilio.github.io/twilio-node/#quickstart), which requires reading our `TWILIO_ACCOUNT_SID`, `TWILIO_NUMBER` `TWILIO_AUTH_TOKEN` from the enviroment. We also make the variables available through the `config` module. @@ -32,8 +32,8 @@ --- **See Also:** - * [twilio-node Quickstart](http://twilio.github.io/twilio-node/#quickstart) - * [Twilio REST Web Service Interface](https://www.twilio.com/docs/api/rest) + * [twilio-node Quickstart](//twilio.github.io/twilio-node/#quickstart) + * [Twilio REST Web Service Interface](//www.twilio.com/docs/api/rest) .step(data-file='config/administrators.json') :markdown @@ -49,20 +49,20 @@ We will implement error handling and message delivery as a piece of [Express.js - middleware](http://expressjs.com/guide/using-middleware.html). We + middleware](//expressjs.com/guide/using-middleware.html). We will make all the Twilio API calls from there. --- **See Also:** - * [Error handling middleware](http://expressjs.com/guide/using-middleware.html#middleware.error-handling) + * [Error handling middleware](//expressjs.com/guide/using-middleware.html#middleware.error-handling) .step(data-file='middleware/twilioNotifications.js') :markdown ## Trigger notifications for administrator list In our Express middleware module, we read the admins from our JSON file - and send alert messages to each of them with the `sendSms` method in + and send alert messages to each of them with the `sendSms` method in `twilioClient`. We also call `next` with the error object, which will allow other configured error handling middleware to execute. @@ -71,43 +71,41 @@ ## Send a text message There are the three parameters needed to [send an - SMS](http://twilio.github.io/twilio-node/#moreexamples) + SMS](//twilio.github.io/twilio-node/#moreexamples) using the [Twilio REST - API](https://www.twilio.com/docs/api/rest): `From`, `To`, and + API](//www.twilio.com/docs/api/rest): `From`, `To`, and `Body`. [US and Canadian phone - numbers](https://www.twilio.com/help/faq/sms/is-mms-supported-for-all-carriers-in-us-and-canada) + numbers](//www.twilio.com/help/faq/sms/is-mms-supported-for-all-carriers-in-us-and-canada) can also send an image with the message. --- **See Also:** - * [REST API Docs for Message resource](https://www.twilio.com/docs/api/rest/message) - * [Your message logs in the account portal](https://www.twilio.com/user/account/log/messages) + * [REST API Docs for Message resource](//www.twilio.com/docs/api/rest/message) + * [Your message logs in the account portal](//www.twilio.com/user/account/log/messages) .step :markdown ## Where to next? - That's it! We've just implemented an automated server notification system + That's it! We've just implemented an automated server notification system that can send you server alerts if anything goes wrong. If you're a Node.js developer working with Twilio, you might want to check out these other tutorials. - [**Two-Factor Authentication**](https://www.twilio.com/docs/howto/walkthrough/two-factor-authentication/node/express) + [**Two-Factor Authentication**](//www.twilio.com/docs/howto/walkthrough/two-factor-authentication/node/express) Increase the security of your login system by verifying a user's mobile phone in addition to their password. - [**SMS and MMS marketing notifications**](https://www.twilio.com/docs/howto/walkthrough/marketing-notifications/node/express) + [**SMS and MMS marketing notifications**](//www.twilio.com/docs/howto/walkthrough/marketing-notifications/node/express) SMS and MMS messages are a personal way to engage with users, offering a much higher open rate than e-mail and more impact than a tweet. - ### Did this help? - Thanks for checking out this tutorial! If you have any feedback to share - with us, we'd love to hear it. Tweet [@twilio](http://twitter.com/twilio) - to let us know what you think. - + Thanks for checking out this tutorial! If you have any feedback + to share with us, we'd love to hear it. [Contact the Twilio Developer + Education Team](mailto:deved-oss@twilio.com) to let us know what you think. diff --git a/twilioClient.js b/twilioClient.js index 3120292..83efa01 100644 --- a/twilioClient.js +++ b/twilioClient.js @@ -5,14 +5,12 @@ module.exports.sendSms = function(to, message) { client.messages.create({ body: message, to: to, - from: config.sendingNumber - // mediaUrl: 'http://www.yourserver.com/someimage.png' - }, function(err, data) { - if (err) { - console.error('Could not notify administrator'); - console.error(err); - } else { - console.log('Administrator notified'); - } + from: config.sendingNumber, + mediaUrl: imageUrl + }).then(function(data) { + console.log('Administrator notified'); + }).catch(function(err) { + console.error('Could not notify administrator'); + console.error(err); }); };