-
Notifications
You must be signed in to change notification settings - Fork 16
/
state_handlers.js
251 lines (239 loc) · 7.45 KB
/
state_handlers.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
import * as amizone from "amizone_api";
import { states } from "./states.js";
import {
renderAmizoneMenu,
renderAttendance,
renderSemester,
renderSchedule,
renderWelcomeMessage,
renderUsernamePrompt,
renderPasswordPrompt,
renderOptionButtons,
} from "./render-messages.js";
import { firstNonEmpty } from "../utils.js";
/**
* @param {string} username
* @param {string} password
* @returns {Promise<boolean>}
*/
const validateAmizoneCredentials = async (username, password) => {
const amizoneClient = new amizone.AmizoneServiceApi(
new amizone.Configuration({
username,
password,
})
);
try {
await amizoneClient.amizoneServiceGetSemesters();
return true;
} catch (e) {
return false;
}
};
/**
* @param {Context} ctx
* @returns {Promise<User>}
*/
export const handleNewUser = async (ctx) => {
const { payload } = ctx;
const updatedUser = structuredClone(ctx.user);
const message = payload.textBody;
if (message?.toLowerCase() === "start") {
await ctx.bot.sendMessage(payload.sender, renderWelcomeMessage());
await ctx.bot.sendMessage(payload.sender, renderUsernamePrompt());
updatedUser.state = states.EXPECT_USERNAME;
return updatedUser;
}
// TODO: fix this message
await ctx.bot.sendMessage(payload.sender, "Start the ctx.bot using *start*");
return updatedUser;
};
/**
* @param {Context} ctx
* @returns {Promise<User>}
*/
export const handleUsername = async (ctx) => {
const { payload } = ctx;
const updatedUser = structuredClone(ctx.user);
updatedUser.state = states.EXPECT_PASSWORD;
updatedUser.amizoneCredentials.username = payload.textBody;
await ctx.bot.sendMessage(payload.sender, renderPasswordPrompt());
return updatedUser;
};
/**
* @param {Context} ctx
* @returns {Promise<User>}
*/
export const handlePassword = async (ctx) => {
const { payload } = ctx;
const updatedUser = structuredClone(ctx.user);
const password = payload.textBody;
const credentialsAreValid = await validateAmizoneCredentials(
updatedUser.amizoneCredentials.username,
password
);
if (credentialsAreValid) {
updatedUser.amizoneCredentials.password = password;
updatedUser.state = states.LOGGED_IN;
await ctx.bot.sendInteractiveMessage(payload.sender, renderOptionButtons());
return updatedUser;
}
await ctx.bot.sendMessage(
payload.sender,
"could not validate credentials. please re-enter"
);
updatedUser.amizoneCredentials.username = null;
updatedUser.state = states.EXPECT_USERNAME;
await ctx.bot.sendMessage(payload.sender, renderUsernamePrompt());
return updatedUser;
};
/**
* @param {BotHandlerContext} ctx
* @returns AmizoneServiceApi
*/
const newAmizoneClient = (ctx) => {
const { username, password } = ctx.user.amizoneCredentials;
return new amizone.AmizoneServiceApi(
new amizone.Configuration({
username,
password,
})
);
};
const loggedInOptions = {
GET_ATTENDANCE: "attendance",
GET_SCHEDULE: "class schedule",
GET_COURSES: "courses",
GET_SEMESTERS: "semesters",
GET_MENU: "menu",
};
/**
* Map enumerated menu options to handlers that return a boolean to indicate success and a string
* that is rendered to the client.
* @type {Map<string, (BotHandlerContext) => Promise<[boolean, string]>>}
*/
const optionsMap = new Map([
[
loggedInOptions.GET_ATTENDANCE,
async (ctx) => {
try {
const attendance = await newAmizoneClient(
ctx
).amizoneServiceGetAttendance();
return [true, renderAttendance(attendance.data)];
} catch (err) {
// catch invalid credential?
return [false, ""];
}
},
],
[
loggedInOptions.GET_SCHEDULE,
async (ctx) =>
// const schedule = await newAmizoneClient(
// ctx
// ).amizoneServiceGetClassSchedule(2022,7,19); //@todo add date feature
// console.log(schedule.data);
// return [true, renderSchedule(schedule.data)];
// updatedUser.state = states.USE_DATE;
// @todo do things the right way: factor out the "list" response.
[true, "list"],
],
[
loggedInOptions.GET_SEMESTERS,
async (ctx) => {
try {
const semesters = await newAmizoneClient(
ctx
).amizoneServiceGetSemesters();
console.log(semesters.data);
return [true, renderSemester(semesters.data)];
} catch (err) {
// catch invalid credential?
return [false, ""];
}
},
],
[loggedInOptions.GET_MENU, async (ctx) => [true, ""]],
]);
/**
* @param {BotHandlerContext} ctx
* TODO: MAJOR: catch errors on sending messages.
* @returns {Promise<User>}
*/
export const handleLoggedIn = async (ctx) => {
const { payload } = ctx;
const message = firstNonEmpty(payload.button.text, payload.textBody, payload.interactive.title).toLowerCase();
const updatedUser = structuredClone(ctx.user);
switch (message.toLowerCase()) {
case "options":
await ctx.bot.sendInteractiveMessage(payload.sender, renderAmizoneMenu());
return updatedUser;
case "logout":
updatedUser.amizoneCredentials = { username: "", password: "" };
updatedUser.state = states.NEW_USER;
await ctx.bot.sendMessage(payload.sender, "Logged Out!");
return updatedUser;
default:
if (optionsMap.has(message)) {
const [success, text] = await optionsMap.get(message)(ctx);
if (success) {
if (text === "list") {
await ctx.bot.sendDateList(payload.sender);
updatedUser.state = states.USE_DATE;
} else {
await ctx.bot.sendMessage(payload.sender, text);
await ctx.bot.sendInteractiveMessage(ctx.payload.sender, renderOptionButtons());
}
return updatedUser;
}
await ctx.bot.sendMessage(
payload.sender,
"unsuccessful. maybe try logging in again?"
);
await ctx.bot.sendMessage(payload.sender, renderUsernamePrompt());
updatedUser.state = states.EXPECT_USERNAME;
return updatedUser;
}
// TODO: remove console log
console.log("invalid opt");
// TODO: send a more helpful message...
await ctx.bot.sendMessage(payload.sender, "invalid opt...");
return updatedUser;
}
};
export const handleUseDate = async (ctx) => {
const { payload } = ctx;
const message = firstNonEmpty(payload.interactive.title, payload.textBody);
const updatedUser = structuredClone(ctx.user);
if (Date.parse(message)) {
try {
const date = message.split("-");
console.log(date[0]);
const schedule = await newAmizoneClient(
ctx
).amizoneServiceGetClassSchedule(date[0], date[1], date[2]); // @todo add date feature
// TODO: remove console log
console.log("fetched scheduled: ", schedule.data);
if (schedule.data.classes.length > 0) {
await ctx.bot.sendMessage(
payload.sender,
renderSchedule(schedule.data)
);
} else {
// ? could we make this message more useful
await ctx.bot.sendMessage(payload.sender, "no schedule available.");
}
await ctx.bot.sendInteractiveMessage(ctx.payload.sender, renderOptionButtons());
updatedUser.state = states.LOGGED_IN;
} catch (err) {
// ? catch invalid credential
console.error(`error while processing req: ${err}`);
}
return updatedUser;
}
await ctx.bot
.sendMessage(ctx.payload.sender, "invalid date!")
.catch((err) => console.error("failed to send message to WA: ", err));
return updatedUser;
};