thehelp-messaging
A lightweight package for sending SMS via Twilio, and email via Sendgrid. Also makes it easy to receive SMS and email from these services in express
-based apps.
Setup
First, install the project as a dependency:
npm install thehelp-messaging --save
Then you'll need to provide your credentials for these services. You can do it in code, but I prefer to set it up with environment variables...
Email via Sendgrid
Create a Sendgrid sub-user account and use its credentials:
SMS via Twilio
Grab your Twilio API keys from your account settings page:
We'll save the configuration for receiving SMS and email for a bit later.
Sending messages
With that all set up, it's as easy as this:
Send email
var Sendgrid = Sendgrid;var sendgrid = ; var email = from: 'someone@somewhere' to: 'recipient@somewhere' subject: 'subject!' // this or html is required text: 'Plaintext message body!!1' // optional fromname: 'User Name'; sendgrid;
Detailed Sendgrid API documentation.
Send SMS
var Twilio = Twilio;var twilio = ; var sms = From: '+15551000000' To: '+15551000000' Body: 'my first text message!'; twilio;
Yep, those key names are all capitalized. :0( Detailed Twilio API documentation.
Receiving messages
This project includes some express
middleware helpers for receiving SMS from Twilio and Email from Sendgrid.
Receive mail
You'll need one new environment variable:
And you'll need to download and install the busboy
node module and (1.2.9 recommended) supply it to the Sendgrid
class on construction:
var express = ;var Busboy = ;var Sendgrid = Sendgrid; var app = ;var sendgrid = Busboy: Busboy; app;
validate
will ensure that the message is really from Sendgrid (via the 'verify' querystrying parameter and your environment variable. parse
will use busboy
to parse out all the non-file components sent by Sendgrid.
Now you just need to set up the Sengrid Parse dashboard to point a given email subdomain of your site to your server. This is kind of a pain to test by deploying to your server all the time, so check out ngrok for exposing a port on your machine to the outside world.
Receive SMS
Twilio messages are easier to deal with, because they're more easily parsed. However, you still have the problem of ensuring that the message is really from Twilio. That's where twilio.validate()
comes in - note the new node modules required:
var express = ;var bodyPraser = ;var twilioSdk = ;var Twilio = Twilio; var app = ;var twilio = twilio: twilioSdk; app;
Now you need to buy a phone number on Twilio and have it forward SMS sent to it to your application. Again, check out ngrok for exposing a port on your machine to the outside world. It makes iterating on your SMS setup that much faster.
There are a few additional troublshooting tips in the Twilio.validate
detailed documentation..
Detailed Documentation
Detailed docs be found at this project's GitHub Pages, thanks to groc
: http://thehelp.github.io/messaging
Contributing changes
It's a pretty involved project. You'll need Sendgrid and Twilio accounts, and all the environment variables mentioned above.
Running tests
The unit tests are quick and easy, but the manual tests (not part of the grunt
'default' task) in this project are pretty involved. They:
- send SMS and email to a phone number and email address for manual verification, and
- send SMS and email and then receive those messages programmatically
You'll need some additional environment variables:
Those last two environment variables are where things get really interesting. You'll need to set up Sendgrid and Twilio to forward messages to your machine at port 3000
. See the 'Receiving messages' section above.
Pull requests
When you have some changes ready, please include:
- Justification - why is this change worthwhile? Link to issues, use code samples, etc.
- Documentation changes for your code updates. Be sure to check the groc-generated HTML with
grunt doc
- A description of how you tested the change. Don't forget about the very-useful
npm link
command :0)
I may ask you to use a git rebase
to ensure that your commits are not interleaved with commits already in the history. And of course, make sure grunt
completes successfully (take a look at the requirements for thehelp-project
). :0)
License
(The MIT License)
Copyright (c) 2013 Scott Nonnenberg <scott@nonnenberg.com>
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.