forked from likeastore/notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
120 lines (103 loc) · 3.13 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
var notifier = require('../source/notifier');
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);
});
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);
});
notifier
.receive('user-completed-action', function (e, actions, callback) {
actions.create('send-android-push-notification', {user: e.user}, callback);
})
.resolve('send-android-push-notification', 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-notification', function (a, transport, callback) {
var tokens = [];
tokens.push(a.data.token);
transport.android.push({
message: {key1: 'value1', key2: 'value2'},
tokens: tokens,
retries: 3
}, callback);
});
notifier
.receive('user-completed-action', function (e, actions, callback) {
actions.create('send-ios-push-notification', {user: e.user}, callback);
})
.resolve('send-ios-push-notification', 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-notification', 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);
});
}