-
Notifications
You must be signed in to change notification settings - Fork 48
/
server.js
195 lines (166 loc) · 5.02 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var notifier = require('../source/notifier');
// Using mandrill
notifier
.receive('user-registered', function (e, actions, callback) {
actions.create('send-welcome-email', {user: e.user}, callback);
})
.resolve('send-welcome-email', function (a, actions, callback) {
asyncRequestForUser(actions.user, function (err, user) {
if (err) {
return callback(err);
}
actions.resolved(a, {email: user.email, name: user.name}, callback);
});
})
.execute('send-welcome-email', function (a, transport, callback) {
var user = a.data;
var vars = [
{ name: 'USER_NAME', content: user.name },
{ name: 'USER_EMAIL', content: user.email}
];
transport.mandrill('/messages/send-template', {
template_name: 'welcome-email',
template_content: [],
message: {
to: [{email: user.email}],
global_merge_vars: vars
}
}, callback);
});
// Using nodemailer
notifier
.receive('user-registered', function (e, actions, callback) {
actions.create('send-welcome-email', {user: e.user}, callback);
})
.resolve('send-welcome-email', function (a, actions, callback) {
// skip to execute
actions.resolved(a, {email: a.user.email, name: a.user.name}, callback);
})
.execute('send-welcome-email', function (a, transport, callback) {
var user = a.data;
var mailOpts = {
from: '[email protected]',
to: '[email protected]',
subject: 'Test Email',
text: 'An example of a message.',
html: '<h1>Hello</h1> </br> Welcome!'
}
transport.nodemailer.sendMail(mailOpts, (error, info) => {
if(error){
console.log(error);
return callback(error);
}
console.log('Message sent: ' + JSON.stringify(info));
// Preview only available when sending through an Ethereal account
console.log('Preview URL: ' + nodemailer.getTestMessageUrl(info));
callback();
});
});
// Using twilio
notifier
.receive('user-registered', function (e, actions, callback) {
actions.create('send-verify-sms', {user: e.user}, callback);
})
.resolve('send-verify-sms', function (a, actions, callback) {
asyncRequestForUser(actions.user, function (err, user) {
if (err) {
return callback(err);
}
actions.resolved(a, {phone: user.phone}, callback);
});
}).
execute('send-verify-sms', function (a, transport, callback) {
transport.twilio.messages.create({
to: a.data.phone,
from: '+12282201270',
body: 'Verification code: 1111',
}, callback);
});
// Using android push
notifier
.receive('user-completed-action', function (e, actions, callback) {
actions.create('send-android-push', {user: e.user}, callback);
})
.resolve('send-android-push', function (a, actions, callback) {
asyncRequestForUser(actions.user, function (err, user) {
if (err) {
return callback(err);
}
actions.resolved(a, {deviceId: user.deviceId}, callback);
});
})
.execute('send-android-push', function (a, transport, callback) {
var regIds = [];
regIds.push(a.data.regIds);
var message = {
title: 'This is a tite',
message: "Hi there."
};
transport.android.push({ message: message, regIds: regIds, retries: 3}, callback);
});
notifier
.receive('user-completed-action-with-hook', function (e, actions, callback) {
actions.create('send-android-push', {user: e.user}, callback);
})
.resolve('send-android-push', function (a, actions, callback) {
asyncRequestForUser(actions.user, function (err, user) {
if (err) {
return callback(err);
}
actions.resolved(a, {deviceId: user.deviceId}, callback);
});
})
.execute('send-android-push', function (a, transport, callback) {
var regIds = [];
regIds.push(a.data.regIds);
var message = {
title: 'This is a tite',
message: "Hi there."
};
transport.android.push({ message: message, regIds: regIds, retries: 3 }, function (err, result) {
if (result.failure === 1) {
var data = {
message: message,
status: result.success
};
notifier.sendHook('notify.sms', err || result, data);
}
return callback(err, result);
});
});
// Using iOS push
notifier
.receive('user-completed-action', function (e, actions, callback) {
actions.create('send-ios-push', {user: e.user}, callback);
})
.resolve('send-ios-push', function (a, actions, callback) {
asyncRequestForUser(actions.user, function (err, user) {
if (err) {
return callback(err);
}
actions.resolved(a, {deviceId: user.deviceId}, callback);
});
})
.execute('send-ios-push', function (a, transport, callback) {
var tokens = [];
tokens.push(a.data.token);
transport.ios.push({
production: false, // use specific gateway based on 'production' property.
passphrase: 'secretPhrase',
alert: { "body" : "Your turn!", "action-loc-key" : "Play" , "launch-image" : "mysplash.png"},
badge: 1,
tokens: tokens
}, callback);
});
notifier.start(process.env.NODE_PORT || 3031);
function asyncRequestForUser(userId, callback) {
var user = {
email: '[email protected]',
name: 'alexander.beletsky',
phone: '+3805554455',
token: 'regId123'
};
process.nextTick(function () {
callback(null, user);
});
}