-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-graph-client.js
139 lines (116 loc) · 4.02 KB
/
simple-graph-client.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { Client } = require('@microsoft/microsoft-graph-client');
/**
* This class is a wrapper for the Microsoft Graph API.
* See: https://developer.microsoft.com/en-us/graph for more information.
*/
class SimpleGraphClient {
constructor(token) {
if (!token || !token.trim()) {
throw new Error('SimpleGraphClient: Invalid token received.');
}
this._token = token;
// Get an Authenticated Microsoft Graph client using the token issued to the user.
this.graphClient = Client.init({
authProvider: (done) => {
done(null, this._token); // First parameter takes an error if you can't get an access token.
}
});
}
/**
* Sends an email on the user's behalf.
* @param {string} toAddress Email address of the email's recipient.
* @param {string} subject Subject of the email to be sent to the recipient.
* @param {string} content Email message to be sent to the recipient.
*/
async sendMail(toAddress, subject, content) {
if (!toAddress || !toAddress.trim()) {
throw new Error('SimpleGraphClient.sendMail(): Invalid `toAddress` parameter received.');
}
if (!subject || !subject.trim()) {
throw new Error('SimpleGraphClient.sendMail(): Invalid `subject` parameter received.');
}
if (!content || !content.trim()) {
throw new Error('SimpleGraphClient.sendMail(): Invalid `content` parameter received.');
}
// Create the email.
const mail = {
body: {
content: content, // `Hi there! I had this message sent from a bot. - Your friend, ${ graphData.displayName }!`,
contentType: 'Text'
},
subject: subject, // `Message from a bot!`,
toRecipients: [{
emailAddress: {
address: toAddress
}
}]
};
// Send the message.
return await this.graphClient
.api('/me/sendMail')
.post({ message: mail }, (error, res) => {
if (error) {
throw error;
} else {
return res;
}
});
}
/**
* Gets recent mail the user has received within the last hour and displays up to 5 of the emails in the bot.
*/
async getRecentMail() {
return await this.graphClient
.api('/me/messages')
.version('beta')
.get().then((res) => {
return res;
});
}
/**
* Collects information about the user in the bot.
*/
async getMe() {
return await this.graphClient
.api('/me')
.get().then((res) => {
return res;
});
}
/**
* Collects the user's manager in the bot.
*/
async getManager() {
return await this.graphClient
.api('/me/manager')
.version('beta')
.select('displayName')
.get().then((res) => {
return res;
});
}
async getDocuments(queryParameter) {
//Searchparameter "Order" is hardcoded
const searchResponse = { requests: [{ entityTypes: ["microsoft.graph.message"], query: { query_string: { query: "order" } }, from: 0, size: 5 }] };
let result = null;
try {
console.log("entering try block");
result = await this.graphClient
.api('/search/query')
.version('beta')
.post(searchResponse);
}
catch (e) {
console.log("entering catch block");
console.log(e);
console.log("leaving catch block");
}
finally {
console.log("entering and leaving the finally block");
}
return result;
}
}
exports.SimpleGraphClient = SimpleGraphClient;