-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
163 lines (134 loc) · 4.64 KB
/
index.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
import { TeamChatClient } from '@zoom/rivet/teamchat';
import { ChatbotClient } from '@zoom/rivet/chatbot';
import dotenv from 'dotenv';
dotenv.config()
const CHATBOT_PORT = 4001;
const TEAMCHAT_PORT = 4002;
(async () => {
const chatbotClient = new ChatbotClient({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
webhooksSecretToken: process.env.WEBHOOK_SECRET_TOKEN,
port: CHATBOT_PORT
});
const teamchatClient = new TeamChatClient({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
webhooksSecretToken: process.env.WEBHOOK_SECRET_TOKEN,
installerOptions: {
redirectUri: `http://localhost:${TEAMCHAT_PORT}`,
stateStore: 'secret_here',
},
port: TEAMCHAT_PORT,
})
chatbotClient.webEventConsumer.onSlashCommand('help', async ({ say }) => {
await say({
"head": {
"text": "Standup Bot Help"
},
"body": [
{ "type": "message", "text": "`/standup_bot help`: Gets list of commands"},
{ "type": "divider", "style": { "bold": false, "dotted": false, "color": "#98a0a9"}},
{ "type": "message", "text": "`/standup_bot start`: Sends the standup message for the day"},
]
})
})
chatbotClient.webEventConsumer.onSlashCommand('start', async ({ say, payload }) => {
const channelName = payload.channelName;
const userId = payload.userId;
async function getChannelId (id) {
try {
const getChannels = async () => {
const channels = (await teamchatClient.endpoints.chatChannels.listUsersChannels({
path: {
userId: id
}
})).data?.channels ?? [];
if (!channels) {
throw new Error('Cannot get channels or empty')
}
return channels;
};
const channels = await getChannels()
const channelId = channels.find((channel) => channel.name === channelName)?.id ?? undefined;
if (!channelId) {
await say('You can only use the standup bot in a channel.')
throw new Error('Channel Id is undefined.')
}
return channelId;
} catch (error) {
throw error;
}
};
let channelId = await getChannelId(userId);
async function listChannelMembers (id) {
try {
const channelMembers = (await teamchatClient.endpoints.chatChannelsAccountLevel.listChannelMembers({
path: {
channelId: id,
userId: userId
}
})).data?.members;
if (!channelMembers) {
throw new Error('Channel members are undefined.')
}
return channelMembers.map(({first_name, last_name }) => {
return { firstName: first_name ?? '', lastName: last_name ?? ''}
});
} catch (error) {
console.log('Cannot list channel members.', error);
throw error;
}
}
let channelMembers = await listChannelMembers(channelId);
await say({
"head": {
"text": "Daily Standup",
"sub_head": {
"text": `${new Date().toLocaleDateString('en-us', { weekday:"long", year:"numeric", month:"short", day:"numeric"}) }`
}
},
"body": [
{
"type": "fields",
"items": channelMembers.map((member) => {
return {
"key": `${member.firstName} ${member.lastName}`,
"value": " ",
"editable": true
}
})
}
]
})
});
chatbotClient.webEventConsumer.event('interactive_message_fields_editable', (response) => {
const payload = response.payload;
function createUpdatedAppCard(payload) {
const appCard = payload.original;
const editedFieldItem = payload.fieldEditItem;
const formFieldItems = appCard.body[0].items;
const updatedItemIndex = formFieldItems.indexOf(formFieldItems.find((item) => item.key === editedFieldItem.key))
appCard.body[0].items[updatedItemIndex] = { editable: false, key: editedFieldItem.key, value: editedFieldItem.newValue }
return appCard;
}
try {
chatbotClient.endpoints.messages.editChatbotMessage({
path: {
message_id: payload.messageId
},
body: {
robot_jid: payload.robotJid,
user_jid: payload.userId,
content: createUpdatedAppCard(payload)
}
})
} catch(error) {
console.log('Cannot edit chatbot message.', error);
throw error;
}
});
await teamchatClient.start()
await chatbotClient.start()
console.log(`Zoom Rivet events servers running on ports ${CHATBOT_PORT} and ${TEAMCHAT_PORT}...`)
})();