-
Notifications
You must be signed in to change notification settings - Fork 0
/
good-morning.py
436 lines (370 loc) · 12.3 KB
/
good-morning.py
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from flask import Flask, request, make_response, Response
import os
import json
import db
import sys
#import upload
from apscheduler.schedulers.background import BackgroundScheduler
from slackclient import SlackClient
reload(sys)
sys.setdefaultencoding('utf-8')
# Your app's Slack bot user token
SLACK_BOT_TOKEN = os.environ["SLACK_BOT_TOKEN"]
#SLACK_VERIFICATION_TOKEN = os.environ["SLACK_VERIFICATION_TOKEN"]
# Slack client for Web API requests
slack_client = SlackClient(SLACK_BOT_TOKEN)
# Flask web server for incoming traffic from Slack
app = Flask(__name__)
# Dictionary to store coffee orders. In the real world, you'd want an actual key-value store
WAKEUP_TIME = {}
# Send a message to the user asking if they would like coffee
#user_id = "U019EJPBFFH"
#test channel
#channel_id = "C019NLH2ULE"
#fame user_id
user_id = "U0123456789"
#GoodMorning channel
channel_id = 'C019ZB2HK38'
# Create a new order for this user in the WAKEUP_TIME dictionary
WAKEUP_TIME = {
"channel": channel_id,
}
## Good morning common functions
def gm_main():
gm_message = slack_client.api_call(
"chat.postMessage",
as_user=True,
channel=channel_id,
text="I am GoodMorning Bot :sunrise:, and I\'m here to help you wake up early :simple_smile:",
attachments=[{
"text": "",
"callback_id": user_id + "set_time_form",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [{
"name": "record_time",
"text": ":sunny: Record wake-up time",
"type": "button",
"value": "record_time"
},
{
"name": "set_time",
"text": ":memo: Set wake-up time",
"type": "button",
"value": "set_time"
},
{
"name": "set_skip",
"text": ":last_quarter_moon_with_face: Skip tomorrow",
"type": "button",
"value": "set_skip"
},
{
"name": "check_top",
"text": ":bookmark: Check my...",
"type": "button",
"value": "check_top"
}
]
}]
)
return make_response("", 200)
def gm_check():
gm_message = slack_client.api_call(
"chat.postMessage",
as_user=True,
channel=channel_id,
text=":bookmark: What do you want to check? ",
attachments=[{
"text": "",
"callback_id": user_id + "set_time_form",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [{
"name": "check_time",
"text": ":bookmark: Check my wake-up time",
"type": "button",
"value": "check_time"
},
{
"name": "check_score",
"text": ":bookmark: Check my score",
"type": "button",
"value": "check_score"
},
{
"name": "check_penalty",
"text": ":date: Weekly Report",
"type": "button",
"value": "check_penalty"
},
{
"name": "check_balance",
"text": ":money_with_wings: Check my balance",
"type": "button",
"value": "check_balance"
}]
}]
)
return make_response("", 200)
#gm_main()
def set_time(user_id, trigger_id, user_name, time=""):
# Add the message_ts to the user's order info
print("set time: " + time)
# Show the ordering dialog to the user
open_dialog = slack_client.api_call(
"dialog.open",
trigger_id=trigger_id,
dialog={
"title": "Set wake-up time",
"submit_label": "Submit",
"callback_id": user_id + "set_time_form",
"elements": [
{
"label": "time",
"type": "text",
"name": "time",
"placeholder": "09:00"
}
]
}
)
# Update the message to show that we're in the process of taking their order
text=":pencil: [" + user_name +"] Taking your request..."
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text=text,
attachments=[]
)
def set_skip(user_id, user_name):
print("set skip: " + user_name)
res = db.record_skip_db(user_id, user_name)
text = ":last_quarter_moon_with_face: *" + user_name + "* requested skip for tomorrow."
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
if res == db.ERROR_SKIPPED_BEFORE:
text = ":no_entry: Sorry! You already skipped once this week."
elif res == db.ERROR_SKIP_LATE:
text = ":no_entry: Sorry! It's too late to skip."
else:
text = ":heavy_check_mark: Okay! You can take a break tomorrow."
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
return make_response("", 200)
def check_time(user_id, user_name):
res = db.get_time_db(user_id)
if (res == ""):
return make_response("", 500)
text = ":sunrise: *" + user_name + "*'s wake-up time: *" + res + "*"
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
return make_response("", 200)
def record_time(user_id, user_name):
res, time = db.record_time_db(user_id, user_name)
text = ""
if (res < 0):
text = text + ":cloud: *" + user_name + "*, you woke up late today :( "
else:
text = text +":sunny: *" + user_name + "*, good morning! "
text = text + " wake-up time: *" + time + "*"
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
return make_response("", 200)
def check_score(user_id, user_name):
res = db.get_record_db(user_id)
text = ":blossom: *" + user_name + "*'s wake-up score: \n"
text = text + res
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
return make_response("", 200)
def check_balance(user_id, user_name):
res = db.get_balance_db(user_id)
text = ":money_with_wings: *" + user_name + "*'s balance: "
text = text + res + "\n"
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
return make_response("", 200)
def slack_user_name(user_id):
try:
# Get user name from slack
response = slack_client.api_call(
"users.info",
user=user_id
)
#print(response)
if response["ok"] == False:
return ""
return response["user"]["real_name"]
except:
return ""
# weekly report (penalty report)
def penalty_report():
res = "*Weekly Report!*\n"
res = res + db.get_weekly_db()
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = res,
attachments=[]
)
return make_response("", 200)
def flush_weekly():
res = db.update_penalty_db()
db.erase_record_db(())
db.erase_holiday_db()
if res is not "":
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = res,
attachments=[]
)
# process user message for mission
def process_user_message(slack_event, user_id, user_name):
file_type = slack_event["event"]["files"][0]["filetype"]
print(file_type)
if (file_type == "jpg" or file_type == "jpeg"):
record_time(user_id, user_name)
## Slack interactive
@app.route("/slack/interactive", methods=["POST"])
def interactive():
message_action = json.loads(request.form["payload"])
user_id= message_action["user"]["id"]
user_name = message_action["user"]["name"]
real_name = slack_user_name(user_id)
print(message_action)
message_type = message_action["type"]
if message_type == "interactive_message":
trigger_id = message_action["trigger_id"]
action_name = message_action["actions"][0]["name"]
if action_name == "set_time":
set_time(user_id, trigger_id, real_name)
elif action_name == "check_time":
check_time(user_id, real_name)
elif action_name == "record_time":
return record_time(user_id, real_name)
elif action_name == "check_top":
return gm_check()
elif action_name == "check_score":
return check_score(user_id, real_name)
elif action_name == "check_penalty":
return penalty_report()
elif action_name == "check_balance":
return check_balance(user_id, real_name)
elif action_name == "set_skip":
return set_skip(user_id, real_name)
elif message_type == "dialog_submission":
time = message_action["submission"]["time"]
if (db.set_time_db(user_id, real_name, time) < 0):
text = ":warning: Error... Contact the admin please :("
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
return make_response("", 500)
#try:
# db.set_user_name_db(user_id, real_name)
#except:
# print("set_user_name error")
text=":white_check_mark: ["+ real_name +"] Wake-up time set!\n"
text = text + ":sunny: *" + real_name + "*"
text = text + "'s wake-up time: *" + time + "*"
# Update the message to show that we're in the process of taking their order
slack_client.api_call(
"chat.postMessage",
channel=WAKEUP_TIME["channel"],
text = text,
attachments=[]
)
return make_response("", 200)
## Slack interactive
@app.route("/slack/event", methods=["POST"])
def event():
slack_event = json.loads(request.data)
print("\n /slack/event")
print(slack_event)
if ("challenge" in slack_event):
return make_response(slack_event["challenge"], 200,
{"content-type": "application/json"})
event_type = slack_event["event"]["type"]
try:
user_id = slack_event["event"]["user"]
user_name = slack_user_name(user_id)
print("user_name: ")
print(user_name)
print("")
except:
return make_response("", 200)
if (event_type == "member_left_channel"):
db.rmv_db_user(user_id)
elif (event_type == "member_joined_channel"):
db.add_db_user(user_id, user_name)
gm_main()
elif (event_type == "message"):
if (slack_event["event"]["text"] == "help"):
gm_main()
elif (slack_event["event"]["text"] == "check"):
gm_check()
if (slack_event["event"]["channel"] == channel_id):
if ("files" in slack_event["event"]):
process_user_message(slack_event, user_id, user_name)
#elif (event_type == "app_mention"):
# text = slack_event["event"]["text"]
# if "Weekly" in text or "weekly" in text:
# penalty_report()
return make_response("", 200)
### Backup
def backup():
os.system('./backup.sh')
#upload.upload("rec_archive.csv")
#upload.upload("balance_archive.csv")
### Schduler
scheduler = BackgroundScheduler()
job = scheduler.add_job(gm_main, 'cron',
day_of_week = "mon,tue,wed,thu,fri",
hour = 8,
id = 'help')
job = scheduler.add_job(penalty_report, 'cron',
day_of_week = "fri",
hour = 12,
id = 'weekly')
# Flush weekly records on every Friday 12:05PM
job = scheduler.add_job(flush_weekly, 'cron',
day_of_week = 'fri',
hour = 22,
minute = 5,
id = 'flush_weekly')
job = scheduler.add_job(backup, 'cron',
day_of_week = 'mon,tue,wed,thu,fri,sat,sun',
hour = 23,
id = 'backup')
scheduler.start()
if __name__ == "__main__":
app.run()