This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
load_user_data.js
61 lines (47 loc) · 2.01 KB
/
load_user_data.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
/*
This module provides a middleware function that pre-loads information
about the user for each message that comes in. This information is used
by the `note_taking.js` sample skill.
Note that this skill translates the user ID into a more universal
`upn` before loading the data - this allows data stored this way to
be accessible by both bot skills and tab applications.
*/
module.exports = function(controller) {
controller.middleware.receive.use(function(bot, message, next) {
// loading user info requires a conversation be present
if (message.channel) {
// use the teams API to load the user's profile
// which gives us their userPrincipalName which can be used to identify them
// within teams, but also in teams tab applications
bot.api.getUserById(message.channel, message.user, function(err, user_profile) {
if (err) {
console.error('Error loading user profile info: ', err);
next();
} else {
// add the user profile information to the mssage object. this can be handy!
message.user_profile = user_profile;
// use the storage system to load the user's record out of the database
controller.storage.users.get(message.user_profile.userPrincipalName, function(err, user_data) {
// now add the user data to the message object
// if one does not exist, add in an empty object ready to use
message.user_data = user_data || {
id: message.user_profile.userPrincipalName,
notes: []
};
// remove the conversation indicator so we get the
var channel_id = message.channel.replace(/\;.*/, '');
controller.storage.channels.get(channel_id, function(err, channel_data) {
message.channel_data = channel_data || {
id: channel_id,
notes: []
};
next();
});
})
}
});
} else {
next();
}
});
}