0.0.2 • Published 6 years ago

pronto-notification-service v0.0.2

Weekly downloads
10
License
-
Repository
github
Last release
6 years ago

Notification Service

Support for notifications across platforms (email, sms, push). Pluggable platform providers. Boilerplate for checking user settings. Standardized templating from platform events.

Usage

You build a Notification Service with a configuration and a set of notification templates.

Configuration

Configure the service and any platforms it will use.

in ./config.coffee

module.exports =
    id_key: 'id'
    platform_slug: 'tryna'
    email:
        # email service config
    (sms):
        # sms service config...
    (push):
        # push service config...

Templates

Set up templates for each notification type for notification providers you are using.

in ./templates.coffee

module.exports =
    'announcements:created': (announcement) ->
        {body} = announcement

        return {
            'email':
                type: "text" #or html
                subject: "New announcement from Platform Name."
                body: "#{body}"
            'sms': "Platform Name announcement: #{body}"
        }

If you want to send an html email, compile the html in your templates.coffee file and pass it into the body of your template.

Run the Service

somata = require 'somata'

templates = require './templates'
config = require './config'

Notifications = require('notification-service')(config, templates)
{notifyUser, notifyUsers, notifyContacts} = Notifications

client = new somata.Client
DataService = client.remote.bind client, 'tryna:data'

# Set up any schema update notifications
# -----------------------------------------------------------------------------

applySubscriptions = (S) ->
    Object.keys(S).map (s_k) ->
        client.subscribe 'tryna:data', s_k, S[s_k]

subscriptions = {

    "announcements:created": (event) ->
        {
            DataService 'find', 'users', {}, (err, all_users) ->
                notifyUsers all_users, "app_invites:created", event.item, ->
        }
}

applySubscriptions(subscriptions)

# Export any utility functions you'd like from the service
# -----------------------------------------------------------------------------

service = new somata.Service 'tryna:notification', {
    notifyUser
    notifyContact
}

API

A user is a person who has signed up / logged into an app.

A contact is a person with a record that has at least an email/phone and maybe more data. Currently the Notification Service can view a User or pieces of the user as a Contact if it needs to. Notification kinds should look as much as possible like "type:event", like "invite:created" or "tryna:updated", keyed off of raw schema notifications. More custom kinds like "my-special-invite" may also be appropriate as long as you think you can keep the templates file scalable.

notifyUser(user, kind, context, cb)
notifyUsers(users, kind, context, cb)
notifyContact(contact, kind, context, cb)
notifyContacts(contacts, kind, context, cb)

You can send an HTML email by setting the type to 'html' and compiling html from the context within your templates.coffee file.

TODO:

Consider being able to specify the type of notification to send (email vs. sms, etc). Though this will be mostly contextual or based off user settings.

Pass in custom checking of user setting and app context to stop or send notifications.

Is it necessary to have sms and push be pluggable like email is (for ses/mailgun/etc)?