-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages-connector.js
148 lines (139 loc) · 5.35 KB
/
messages-connector.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
/*
* messages-connector.js
*
* This module handles sending messages about webhooks on behalf
* of any users who have authorized the integration
*
* JP Shipherd 6/14/2018
*/
class MessageStuff {
constructor(sdk) {
this.webex_sdk = sdk;
}
/**
* Sends a message on behalf of the Authenticating user back to the
* Webex Teams space where the authentication link was first displayed
*
* @function sendAuthorizationCompleteMessage
* @param {Object} authInfo - Details about the user who just authorized us
*/
async sendAuthorizationCompleteMessage(authInfo, lock) {
let message = {
'roomId': authInfo.roomId,
text: authInfo.person.displayName +
' has authorized me to post calls webhook data to this space.\n\n' +
'Make a call and see what happens...'
};
let self = this;
try {
await lock.wait();
// Set the token for the user who we are sending this message for (just in case)
await self.webex_sdk.setToken(authInfo.access_token);
// And post a message in the space on behalf of the authentication user
await self.webex_sdk.messageSend(message);
lock.signal();
} catch(e) {
lock.signal();
throw(e);
}
}
/**
* Post a message, on behalf of the authorizing user about the
* calls resource webhook that was recieved
*
* @function postCallsWebhookMessage
* @param {Object} authInfo - details about the authorizing user and the room we are in
* @param {Object} webhook - webhook data that was just received
*/
async postCallsWebhookMessage(authInfo, webhook, lock) {
let message = {};
let actorName = '';
let personName = '';
let room = {};
let self = this;
try {
await lock.wait();
// Set the token for the user who we are sending this message on behalf of
await self.webex_sdk.setToken(authInfo.access_token);
// get info for the players in this webhook
let person = await self.webex_sdk.personGet(webhook.createdBy); // our authorized user
personName = person.displayName;
person = await self.webex_sdk.personGet(webhook.actorId); // The calls actor
actorName = person.displayName;
if (webhook.data.roomId) {
room = await self.webex_sdk.roomGet(webhook.data.roomId);
}
// Format the message and send the message
message = {
'roomId': authInfo.roomId
};
if (webhook.event === 'created') {
message.markdown = personName + ' (webhook.createdBy) got a calls:created event\n\n' +
actorName + ' (webhoook.actorId) started a call';
if (room) {
message.markdown += ' in the "' + room.title + '" space';
}
message.markdown += '.\n\nStatus: '+ webhook.data.status;
} else if (webhook.event == 'updated') {
message.markdown = personName + ' (webhook.createdBy) got a calls:updated event\n\n' +
actorName +' (webhook.actorId) updated a call';
if (room) {
message.markdown += ' in the "' + room.title + '" space';
}
message.markdown += '.\n\nStatus: '+ webhook.data.status;
} else {
throw new Error('Got unexpected calls resource webhook with event type: ' + webhook.event);
}
if (!authInfo.terseMode) {
message.markdown += '\n```\n' + JSON.stringify(webhook, null, 2); // make it pretty
}
await self.webex_sdk.messageSend(message);
lock.signal();
} catch(e) {
lock.signal();
console.error('Error sending webhook info for ' + authInfo.person.displayName +
'to space: ' +e.message);
}
}
/**
* Post a message, on behalf of the authorizing user about the
* callMemberships resoruce webhook that was recieved
*
* @function postCallMembershipsWebhookMessage
* @param {Object} authInfo - details about the authorizing user and the room we are in
* @param {Object} webhook - webhook data that was just received
*/
async postCallMembershipsWebhookMessage(authInfo, webhook, lock) {
let message = {};
let participantName = '';
let personName = '';
let self = this;
try {
await lock.wait();
// Set the token for the user who we are sending this message for (just in case)
await self.webex_sdk.setToken(authInfo.access_token);
let person = await self.webex_sdk.personGet(webhook.createdBy); // our authorized user
personName = person.displayName;
person = await self.webex_sdk.personGet(webhook.data.personId); // The actor
participantName = person.displayName;
// Format the message and send the message
message = {
'roomId': authInfo.roomId,
'markdown': personName + ' (webhook.createdBy) got a ' + webhook.resource +
':' + webhook.event + ' event.\n\nNew Status for ' +
participantName + ' (webhook.data.personId): '+
webhook.data.status
};
if (!authInfo.terseMode) {
message.markdown += '\n```\n' + JSON.stringify(webhook, null, 2); // make it pretty
}
await self.webex_sdk.messageSend(message);
lock.signal();
} catch(e) {
lock.signal();
console.error('Error sending webhook info for ' + authInfo.person.displayName +
'to space: ' + e.message);
}
}
} // end of module definition
module.exports = MessageStuff;