Skip to content

Commit

Permalink
added json ld + txt version
Browse files Browse the repository at this point in the history
  • Loading branch information
posabsolute committed Jun 2, 2015
1 parent 0968150 commit b07af91
Show file tree
Hide file tree
Showing 20 changed files with 491 additions and 38 deletions.
29 changes: 27 additions & 2 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function(grunt) {
nunjucks: {
options:{
paths : "src",

langs : ["en_US", "fr_CA"],
configureEnvironment : function(env){
var self = this;
Expand Down Expand Up @@ -180,6 +181,30 @@ module.exports = function(grunt) {
grunt.task.run('nunjucks');
});

grunt.registerTask("build-templates-text", "Build emails text", function(test) {
var nunjucks = grunt.config.get('nunjucks') || {};
var langs = nunjucks.options.langs;
langs.forEach(function(lang){
nunjucks[lang] = {
options:{
lang:lang,
paths : "src/templates"
},
files: [
{
expand: true,
cwd: "src/templates",
src: ["**/*.txt","!**/_*.txt"],
dest: "dist/output/"+lang+"/",
ext: ".txt"
}
]
};
});

grunt.config.set('nunjucks', nunjucks);
grunt.task.run('nunjucks');
});


grunt.loadNpmTasks('grunt-sass');
Expand All @@ -194,8 +219,8 @@ module.exports = function(grunt) {
grunt.registerTask('default',['watch']);
grunt.registerTask('test',['mochaTest']);
grunt.registerTask('css',['sass']);
grunt.registerTask('html',['build-templates','premailer:inline']);
grunt.registerTask('build',['sass','build-templates','premailer:inline']);
grunt.registerTask('html',['build-templates', 'build-templates-text','premailer:inline']);
grunt.registerTask('build',['sass','build-templates', 'build-templates-text','premailer:inline']);
grunt.registerTask('email',['nodemailer']);

};
9 changes: 8 additions & 1 deletion locales/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ alert_text_3: Thanks for choosing Acme Inc.


action_text_1: Please confirm your email address by clicking the link below.
action_text_2: We may need to send you critical information about our service and it is important that we have an accurate email address.
action_text_2: We may need to send you critical information about our service and it is important that we have an accurate email address.



invoice_total: Total
invoice_paid: Paid
invoice_welcome: Thank you for using Acme Inc
invoice_invoice: Invoice
16 changes: 15 additions & 1 deletion locales/fr_CA.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@

test_string: Vous utilisez fr_CA
test_string: Vous utilisez fr_CA

alert_text_1: Vous avez <strong>1 rapport gratuit</strong> restant.
alert_text_2: Ajouter votre carte de credit, ne manquer pas vos rapports
alert_text_3: Merci de choisir Acme Inc.


action_text_1: SVP confirmer vogter addresse email ci-dessous
action_text_2: Il est possible que nous vous envoyons des informations critiques sur nos services dans le futur.


invoice_total: Total
invoice_total: Payer
invoice_welcome: Merci d'avoir utilisé Acme Inc.
invoice_invoice: Facture
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"directories": {
"example": "examples"
},
},
"scripts": {
"test": "grunt test --verbose"
},
Expand Down Expand Up @@ -46,8 +46,10 @@
"dependencies": {
"body-parser": "^1.11.0",
"domain": "0.0.1",
"html-to-text": "^1.3.0",
"le_node": "^0.2.1",
"node-slack": "0.0.7",
"nodemailer-html-to-text": "^1.0.1",
"nodemailer-sendgrid-transport": "^0.1.0",
"nodemailer-wellknown": "^0.1.5",
"pushbullet": "^1.4.3"
Expand Down
157 changes: 155 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,130 @@ nunjucks: {
Follow the same conventions while creating your localization files, in this case, *locales/en_US.yml*.
## JSON LD
By adding schema.org markup to the emails you send your users, you can make that information available across their Google experience, and make it easy for users to take quick action. Inbox, Gmail, Google Calendar, Google Search, and Google Now all already use this structured data.
Inker implement 2 types of the for available on gmail, being actions & promotions.
JSON LD components are dynamic data, generally linked directly to the customer you are sending the email to. JSON-LD complete implementation is available using the Inker API or you can customize the components to use your own templating engine.
### Example:
JSON-LD content is a script tag embedded in the email body, this is already taken care of by Inker.
Usage in the template :
```javascript
{% block jsonLD %} jsonldGoto(); {% endblock %}
```
Then when sending your email using Inker API you would add the customer data like this:
```javascript
"POST /email"
// Post data
{
"data" : {
"collection": "transactional", // required, folder(s) in output
"template": "alert", // required, template filename
"locale" : "en_US", // optional, default to en_US
"variables": {
"jsonLD" : {
"gotoType" : "ViewAction",
"gotoTarget" : "https://watch-movies.com/watch?movieId=abc123",
"gotoName" : "Watch the 'Avengers' movie online"
}
}
},

}
```
It's important to remember to put your JSON-LD content in variables -> jsonLD. As said before you can easily also modify the json-ld components to use your own templating engine. The components are in : *src/templates/components/jsonld*
### Go to Action
Go-To Actions take the user to your website where the action can be completed. Unlike One Click Actions, go-to actions can be interacted with multiple times.
https://developers.google.com/gmail/markup/reference/go-to-action
!(https://developers.google.com/gmail/markup/images/actions-go-to-action.png)
Usage in the template :
```javascript
{% block jsonLD %} jsonldGoto(); {% endblock %}
```
*Options :*
* gotoType {string} ViewAction || TrackAction
* gotoTarget {string} https://watch-movies.com/watch?movieId=abc123
* gotoName {string} Watch the 'Avengers' movie online
### One Click Action
You may add a one-click confirm button to emails requiring users to approve, confirm and acknowledge something. Once the user clicks on the button, an http request will be issued from Google to your service, recording the confirmation. ConfirmAction can only be interacted with once.
https://developers.google.com/gmail/markup/reference/one-click-action
Usage in the template :
```javascript
{% block jsonLD %} jsonldOneClick(); {% endblock %}
```
*Options :*
* action.type {string} ConfirmAction || SaveAction
* action.name {string} textshown in inbox
* action.url {string} Url action
* description {string} Call description
### Review Action
Use to declare a review action. Gmail may show a review button next to the email, which will prompt the user for a numeric review value and / or a user comment.
https://developers.google.com/gmail/markup/reference/review-action
Usage in the template :
```javascript
{% block jsonLD %} jsonldReview(); {% endblock %}
```
*Options :*
* reviewType {string} FoodEstablishment || Movie || Product
* reviewName {string} Joe's Diner
* reviewUrl {string} Action Url
* reviewDescription {string} Hope you enjoyed your meal with us, please review your experience
### RSVP Action
You may add a one-click confirm button to emails requiring users to approve, confirm and acknowledge something. Once the user clicks on the button, an http request will be issued from Google to your service, recording the confirmation. ConfirmAction can only be interacted with once.
https://developers.google.com/gmail/markup/reference/rsvp-action
Usage in the template :
```javascript
{% block jsonLD %} jsonldRSVP(); {% endblock %}
```
*Options :*
* startDate {date} 2015-04-18T15:30:00Z
* endDate {date} 2015-04-18T16:30:00Z
* EventName {string} Google Party
* addressStreet {string} 24 Willie Mays Plaza
* addressLocality {string} San Francisco
* addressRegion {string} CA
* postalCode {string} 94107
* addressCountry {string} USA
* action.attendance http://schema.org/RsvpAttendance/Yes || http://schema.org/RsvpAttendance/No || http://mysite.com/rsvp?eventId=123&value=maybe
## Sending a test email to your inbox
Inker uses [grunt-nodemailer](https://github.com/dwightjack/grunt-nodemailer) to send tests. By default, it sends a test for all files that are in the output folders, you can easily change that in **gruntfile.js**.
Expand Down Expand Up @@ -513,15 +637,17 @@ Example :
// Post data
{
"data" : {
"collection": "data_example", // required, folder(s) in output
"template": "index", // required, template filename
"collection": "transactional", // required, folder(s) in output
"template": "alert", // required, template filename
"locale" : "en_US", // optional, default to en_US
"variables": {
"name":"Cedric",
"loop": ["1","2","3"]
}
},
"options" : {
// enable text version using a custom txt template.
"textVersion" : true,
"from": "sender@address",
"to": "[email protected]",
"subject": "hello",
Expand All @@ -534,6 +660,33 @@ Example :
}
```
### Sending a custom text version
When enabling the text version option, the api will automatically fetch the .txt file at the same folder level as the html version.
If the html template is in: *output.en_US/transactional/alert.html*
The txt version should be here: *output.en_US/transactional/alert.txt*
### Generate text version
You can also automatically the text version from the html version on the fly,
Example :
```javascript
"options" : {
// Enable text version using a custom txt template.
"textVersion" : true,
// Generate text version from html template
"textVersionFromHTML" : true,
"from": "sender@address",
"to": "[email protected]",
"subject": "hello",
"text": "hello world!"
},
```
### Email delivery service Authentication
You must add your credentials in */src/server/configs.providers.auth.js*
Expand Down
22 changes: 19 additions & 3 deletions src/server/controllers/controller.emails.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var express = require('express'),
bodyParser = require('body-parser'),
nodemailer = require('nodemailer'),
wellknown = require('nodemailer-wellknown'),
htmlToText = require('html-to-text'),
logs_service = require('../services/service.logs'),
configs = require('../configs/configs'),
serviceAuthConfigs = require('../configs/configs.providers.auth'),
Expand All @@ -29,9 +30,11 @@ var emails_controller = {
locale = req.body.data.locale || "en_US",
// render template with nunjucks
tplURL = templates_controller.getTemplate(locale, req.body.data.collection, req.body.data.template),
templateHtml = templates_controller.renderTemplate(tplURL, data, res),
tplURLText = templates_controller.getTemplate(locale, req.body.data.collection, req.body.data.template, "txt"),
//templateHtml = templates_controller.renderTemplate(tplURL, data, res),
templateTxt = "",
// get service from post data
service = req.body.service.name || configs.service || 'smtp',
service = req.body.service && req.body.service.name || configs.service || 'smtp',
// get email service provider
transporter = emails_controller.getTransporter(service),
// setup e-mail data
Expand All @@ -43,11 +46,24 @@ var emails_controller = {
return;
}

// generate text version
// using custom txt template
if(req.body.options.textVersion && !req.body.options.textVersionFromHTML){
templateTxt = templates_controller.renderTemplate(tplURLText, data, res);
// generating text version from html template
}else if(req.body.options.textVersion && req.body.options.textVersionFromHTML){
console.log("fuck");
templateTxt = htmlToText.fromString(templateHtml);
}
console.log(templateTxt);

mailOptions.sync = req.body.options.sync || configs.sync || false;
mailOptions.html = templateHtml;
mailOptions.failOver = req.body.service.failOver || configs.failOver || undefined;
mailOptions.text = templateTxt;
mailOptions.failOver = req.body.service && req.body.service.failOver || configs.failOver || undefined;
mailOptions.service = service;
mailOptions.template = tplURL;

// send mail with defined transport object
emails_controller.sendEmail(mailOptions, transporter, undefined, res);

Expand Down
9 changes: 6 additions & 3 deletions src/server/controllers/controller.templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ var templates_controller = {
if(req.headers["x-authorization-token"] !== configs.authToken){
return res.sendStatus(401);
}

// get post data
var data = req.query || {},
locale = req.params.locale || "en_US",
tplURL = templates_controller.getTemplate(locale, req.params.folder, req.params.name),
type = req.query.type || "html",
tplURL = templates_controller.getTemplate(locale, req.params.folder, req.params.name, type),
templateHtml = templates_controller.renderTemplate(tplURL, data, res);

// when there is an error we return an object
Expand All @@ -39,8 +41,9 @@ var templates_controller = {
* Find template path
* @return {string} return template url
*/
getTemplate : function(locale, folder, name){
return locale + '/' + folder + '/' + name + '.html';
getTemplate : function(locale, folder, name, type){
type = type || "html";
return locale + '/' + folder + '/' + name + '.' + type;
},
/**
* Render template with custom data using nunjucks
Expand Down
8 changes: 8 additions & 0 deletions src/templates/components/_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@
{% from "components/_component.video.html" import video %}
{% from "components/_component.panel.html" import panel %}
{% from "components/_component.alertHeader.html" import alertHeader %}

<!-- JSONLD -->
{% from "components/jsonld/actions/_component.jsonld.goto.html" import jsonldGoto %}
{% from "components/jsonld/actions/_component.jsonld.one-click-action.html" import jsonldOneClick %}
{% from "components/jsonld/actions/_component.jsonld.review.html" import jsonldReview %}
{% from "components/jsonld/actions/_component.jsonld.rsvp.html" import jsonldRSVP %}
{% from "components/jsonld/promotions/_component.jsonld.offer.html" import jsonldOffer %}
</head>
<body>
{% block jsonLD %}{% endblock %}
<table class="body">
<tr>
<td class="center" align="center" valign="top">
Expand Down
Loading

0 comments on commit b07af91

Please sign in to comment.