forked from bahaso/rocket-daily-standup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
211 lines (187 loc) · 7.04 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
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
require('dotenv').config()
const rocket = require('@rocket.chat/sdk')
const respmap = require('./reply')
const moment = require('moment')
const HOST = process.env.ROCKETCHAT_URL
const USER = process.env.ROCKETCHAT_USER
const PASS = process.env.ROCKETCHAT_PASSWORD
const BOTNAME = process.env.BOT_NAME
const SSL = process.env.ROCKETCHAT_USE_SSL
const ROOMS = [process.env.ROCKETCHAT_ROOM]
let time = {
dialog: process.env.BOT_TIME_DIALOG,
publish: process.env.BOT_TIME_PUBLISH
}
let channelId=''
let users = []
let submited = []
let indexCount = 0
let myuserid
const runbot = async () => {
const conn = await rocket.driver.connect( { host: HOST, useSsl: SSL})
myuserid = await rocket.driver.login({username: USER, password: PASS})
const roomsJoined = await rocket.driver.joinRooms(ROOMS)
const subscribed = await rocket.driver.subscribeToMessages()
const msgloop = await rocket.driver.reactToMessages( processMessages )
}
const processMessages = async(err, message, messageOptions) => {
if (!err && moment().format('dddd') != 'Saturday' && moment().format('dddd') != 'Sunday') {
if (message.u._id === myuserid) return
const roomname = await rocket.driver.getDirectMessageRoomId(message.u.username)
if (message.msg.toLowerCase() && message.rid == roomname) {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
console.log('get dm from '+message.u.username)
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
let response
let inpmsg = message.msg.toLowerCase()
if (indexCount== 0) {
submited.push({
user: message.u,
message1: inpmsg,
message2: '',
message3: '',
})
response = respmap.yesterday
indexCount=1
}
else if(indexCount == 1){
let indexSubmit = findInSubmit(message.u.username)
submited[indexSubmit].message2 = inpmsg
response = respmap.today
indexCount=2
}
else if (indexCount == 2){
let indexSubmit = findInSubmit(message.u.username)
submited[indexSubmit].message3 = inpmsg
response = respmap.blocking
if (moment().format('HH:mm:ss') > time.publish) {
let submit = submited[indexSubmit]
rocket.driver.sendMessage({
rid: channelId,
msg: 'Hey @here, @'+submit.user.name+' was submited a stand-up\n'+
'**1. What did you do since yesterday?**\n'+
'> '+submit.message1+'\n'+
'** ** \n'+
'**2. What will you do today?**\n'+
'> '+submit.message2+'\n'+
'** ** \n'+
'**3. Anything is blocking your progress?**\n'+
'> '+submit.message3+'\n'
})
.then(response => {
submited.splice(indexSubmit, 1)
b.respond('Thank you 👋')
})
.catch(error => {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
console.log('Error send message to channel')
console.log(error)
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
})
}
indexCount = 0
}
else{
response = message.u.username +
', how can ' + BOTNAME + ' help you with ' +
message.msg.substr(BOTNAME.length + 1)
}
const sentmsg = await rocket.driver.sendToRoom(response, roomname)
}
}
}
function getChannelMemberList() {
rocket.api.get('channels.members', {
roomId: channelId
})
.then(response => {
users = []
response.members.forEach(user => {
if(!user.username.toLowerCase().includes('bot')) {
users.push(user)
}
})
})
.catch(error => {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
console.log('Error when get channel member list :')
console.log(error)
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
})
}
function findInSubmit(username){
let response = submited.find(submit => {
return submit.user.username == username
})
return submited.indexOf(response)
}
rocket.api.get('channels.list')
.then(response => {
response.channels.forEach(channel => {
if (ROOMS.indexOf(channel.name) >= 0){
channelId = channel._id
}
})
getChannelMemberList()
})
setInterval(() => {
if(moment().format('HH:mm:ss') == time.dialog && moment().format('dddd') != 'Saturday' && moment().format('dddd') != 'Sunday') {
users.forEach(user => {
if (user.username) {
rocket.driver.getDirectMessageRoomId(user.username)
.then(userRoomId => {
rocket.driver.sendMessage({
rid: userRoomId,
msg: 'Hi, '+user.name+'. What did you do since yesterday?'
})
})
.catch(error => {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
console.log('Error when send DM to ' +user.name)
console.log(error)
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
})
}
})
}
if(moment().format('HH:mm:ss') == time.publish && moment().format('dddd') != 'Saturday' && moment().format('dddd') != 'Sunday') {
let message1 = ''
let message2 = ''
let message3 = ''
let unsubmited = ''
users.forEach(user => {
if (findInSubmit(user.username) < 0) {
unsubmited+=' @'+user.username
}
})
if (unsubmited.length > 0) {
unsubmited = "i didn't hear from"+unsubmited
}
submited.forEach(submit => {
message1+='> '+submit.user.name+'\n'+'> '+submit.message1+'\n'+'** ** \n'
message2+='> '+submit.user.name+'\n'+'> '+submit.message2+'\n'+'** ** \n'
message3+='> '+submit.user.name+'\n'+'> '+submit.message3+'\n'+'** ** \n'
})
rocket.driver.sendMessage({
rid: channelId,
msg: 'Hey, @here our daily stand-up team :coffee:\n'+
'**1. What did you do since yesterday?**\n'+
message1+
'**2. What will you do today?**\n'+
message2+
'**3. Anything is blocking your progress?**\n'+
message3+
unsubmited
})
.then(response => {
submited = []
})
.catch(error => {
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
console.log('Error when sending message to channel :')
console.log(error)
console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
})
}
}, 1000)
runbot()