-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
297 lines (248 loc) · 6.98 KB
/
app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
require("dotenv").config();
const { Elarian } = require("elarian");
const {
ELARIAN_ORG_ID,
ELARIAN_APP_ID,
ELARIAN_API_KEY,
ELARIAN_SMS_SHORTCODE,
} = process.env;
const plans = [
{ id: 1, name: "Maxi", package: "25Mbps", price: 4000 },
{ id: 2, name: "Midi", package: "10Mbps", price: 3000 },
{ id: 3, name: "Mini", package: "5Mbps", price: 2000 },
];
// getPlan returns the plan with the current id
const getPlan = (id) => {
return plans.find((plan) => plan.id === id);
};
const plansMenuText = () => {
let text = `Please select a plan`;
text += plans.map(
(plan) => `\n${plan.id}. ${plan.name} ${plan.package} @ KES ${plan.price}`
);
return text;
};
const SCREEN_HOME = "SCREEN_HOME";
const SCREEN_CREATE_ACCOUNT = "SCREEN_CREATE_ACCOUNT";
const SCREEN_CONTACT_US = "SCREEN_CONTACT_US";
const SCREEN_MY_ACCOUNT = "SCREEN_MY_ACCOUNT";
const SCREEN_VIEW_PLANS = "SCREEN_VIEW_PLANS";
const SCREEN_PREVIEW_PLAN = "SCREEN_PREVIEW_PLAN";
const SCREEN_CONFIRM_PLAN = "SCREEN_CONFIRM_PLAN";
const SCREEN_UNSUBSCRIBE = "SCREEN_UNSUBSCRIBE";
const SCREEN_CONFIRM_UNSUBSCRIPTION = "SCREEN_CONFIRM_UNSUBSCRIPTION";
const REMINDER_PAYMENT_REMINDER = "REMINDER_PAYMENT_REMINDER";
// sendSMSToCustomer
const sendSMSToCustomer = async (customer, message) => {
const response = await customer.sendMessage(
{
number: ELARIAN_SMS_SHORTCODE,
channel: "sms",
},
{
body: {
text: message,
},
}
);
console.log(response);
return response;
};
const ussdSessionEventHandler = async (
notification,
customer,
appData,
callback
) => {
const menu = {
text: null,
isTerminal: false,
};
let screen = SCREEN_HOME;
const { input } = notification;
const { text } = input;
if (appData) {
screen = appData.screen;
}
const customerData = await customer.getMetadata();
let { name, plan } = customerData;
if (screen === SCREEN_HOME && text !== "") {
switch (text) {
case "1": {
screen = name ? SCREEN_MY_ACCOUNT : SCREEN_CREATE_ACCOUNT;
break;
}
case "2": {
screen = SCREEN_VIEW_PLANS;
break;
}
case "3": {
screen = name ? SCREEN_UNSUBSCRIBE : SCREEN_CONTACT_US;
break;
}
case "4": {
screen = SCREEN_CONTACT_US;
}
}
}
switch (screen) {
case SCREEN_CREATE_ACCOUNT: {
menu.text = `What's your name?`;
menu.isTerminal = false;
callback(menu, {
screen: SCREEN_VIEW_PLANS,
});
break;
}
case SCREEN_VIEW_PLANS: {
if (!name && text !== "") {
name = text;
await sendSMSToCustomer(
customer,
`Welcome to connectify. Your account has been created.`
);
}
menu.text = plansMenuText();
menu.isTerminal = false;
callback(menu, {
screen: SCREEN_PREVIEW_PLAN,
});
break;
}
case SCREEN_PREVIEW_PLAN: {
if (text !== "") {
plan = getPlan(parseInt(text));
}
menu.text = `You have selected ${plan.name}.\n Press 1 to confirm subscription.`;
menu.isTerminal = false;
callback(menu, {
screen: SCREEN_CONFIRM_PLAN,
});
break;
}
case SCREEN_CONFIRM_PLAN: {
menu.text = `You have subscribed to ${plan.name}.`;
menu.isTerminal = true;
await sendSMSToCustomer(
customer,
`You have successfully subscribed to ${plan.name}.`
);
await customer.addReminder({
key: REMINDER_PAYMENT_REMINDER,
remindAt: (Date.now() + 60000) / 1000,
payload: "",
interval: 60,
});
callback(menu, {
screen: SCREEN_HOME,
});
break;
}
case SCREEN_CONTACT_US: {
menu.text = `Please contact us on 07xxxxxxxx. We will be happy to help.`;
menu.isTerminal = true;
callback(menu, {
screen: SCREEN_HOME,
});
break;
}
case SCREEN_UNSUBSCRIBE: {
if (!plan) {
menu.text = `You currently don't have an active plan.`;
menu.isTerminal = true;
callback(menu, {
screen: SCREEN_HOME,
});
break;
}
menu.text = `You will be unsubscribed from ${plan.name}.\n Press 1 to confirm.`;
menu.isTerminal = false;
callback(menu, {
screen: SCREEN_CONFIRM_UNSUBSCRIPTION,
});
break;
}
case SCREEN_CONFIRM_UNSUBSCRIPTION: {
if (text !== "" && text === "1") {
switch (text) {
case "1": {
let message = `You have successfully unsubscribed from ${plan.name}.`;
menu.text = message;
menu.isTerminal = true;
await sendSMSToCustomer(customer, message);
plan = null;
screen = SCREEN_HOME;
break;
}
default: {
menu.text = `Invalid option provided.\n Press 1 to confirm unsubscription from ${plan.name}.`;
menu.isTerminal = false;
screen = SCREEN_CONFIRM_UNSUBSCRIPTION;
}
}
}
callback(menu, {
screen,
});
break;
}
case SCREEN_MY_ACCOUNT: {
menu.text = !plan
? `You don't have an active subscription.\n1. View plans`
: `Your current plan is ${plan.name}.\n Thank you for choosing us.`;
menu.isTerminal = plan;
screen = !plan ? SCREEN_VIEW_PLANS : SCREEN_HOME;
callback(menu, { screen });
break;
}
default: {
const guestMenu = `Welcome to connectify.\n1. Create account\n2. View plans\n3. Contact us`;
menu.text = !name
? guestMenu
: `Welcome back ${name}.\n1. My account\n2. Upgrade plan\n3. Unsubscribe \n4. Contact us`;
menu.isTerminal = false;
callback(menu, {
screen,
});
}
}
await customer.updateMetadata({ name, plan });
console.log(customerData);
};
const receivedPaymentEventHandler = async (payment, customer) => {
// TODO only cancel reminder if payment is complete
const { value } = payment;
const { currencyCode, amount } = value;
await sendSMSToCustomer(
customer,
`Payment of ${currencyCode} ${amount} has been successfully received.`
);
await customer.cancelReminder(REMINDER_PAYMENT_REMINDER);
};
const reminderEventHandler = async (reminder, customer) => {
console.log(reminder);
const { name, plan } = await customer.getMetadata();
await sendSMSToCustomer(
customer,
`Hello ${name}, please remember to make your payment of KES ${plan.price}`
);
};
const connectToElarian = () => {
const client = new Elarian({
orgId: ELARIAN_ORG_ID,
appId: ELARIAN_APP_ID,
apiKey: ELARIAN_API_KEY,
});
client
.on("error", (error) => {
console.log(
`elarian: connection error - ${error}. Attempting to reconnect..`
);
})
.on("connected", () => console.log(`elarian: connected successfully`))
.on("ussdSession", ussdSessionEventHandler)
.on("receivedPayment", receivedPaymentEventHandler)
.on("reminder", reminderEventHandler)
.connect();
};
connectToElarian();