-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·208 lines (169 loc) · 7.25 KB
/
Copy pathmain.py
File metadata and controls
executable file
·208 lines (169 loc) · 7.25 KB
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
#!/usr/bin/env python3
from flask import Flask, jsonify, request
from ciscosparkapi import CiscoSparkAPI
import datetime
import threading
import pickle
import time
import os
file = open("./key.txt","r")
key = file.read()
botapi = CiscoSparkAPI(access_token=key)
bot_id = 'Y2lzY29zcGFyazovL3VzL0FQUExJQ0FUSU9OL2I4NGE2NWQxLWFhZDktNDIyMC05ODFmLWYwYzNmMGEzZTIwOQ'
bot_name = 'EggTimer'
app = Flask(__name__)
recent_message = [{'From' : 'Cameron', 'message': 'this is the message'}]
def load_reminder_db():
if os.path.getsize("reminder_list.pickle") > 0:
reminder_db = pickle.load(open("reminder_list.pickle", "rb"))
else:
reminder_db = []
return reminder_db
def save_reminder_db(database_list):
pickle.dump(database_list, open("reminder_list.pickle", "wb"))
return
def save_reminder(datetime, roomId, message, person_id, person_nickname):
if message == '':
reminder_message = "Hey <@personId:" + person_id + '|' + person_nickname + '>, apparently you just wanted me to ping you... weirdo.'
else:
reminder_message = "Hey <@personId:" + person_id + '|' + person_nickname + '>, ' + message
db = load_reminder_db()
new_reminder = [datetime, roomId, reminder_message]
# need insert reminder into list alphabetticallly
db.append(new_reminder)
save_reminder_db(db)
# print(reminder_db)
return
def error_notification():
pass
def send_reminder(roomid, reminder_message):
botapi.messages.create(roomId=roomid, markdown=reminder_message)
def check_reminders(sleep_time):
while True:
db = load_reminder_db()
new_db = []
for reminder in db:
if reminder[0] <= datetime.datetime.now():
send_reminder(reminder[1], reminder[2])
else:
new_db.append(reminder)
save_reminder_db(new_db)
print('Sleeping {}'.format(sleep_time))
time.sleep(sleep_time)
def determine_datetime(reminder_time_raw):
# ToDo needs to convert the user input into an actual date and time. send and error response if its not legit
minutes_var = 0
hours_var = 0
days_var = 0
now = datetime.datetime.now()
output = 0
# If time ends in am, pm, a or p the following 2 ifs catch it'
if reminder_time_raw[-2:] == 'am' or reminder_time_raw[-2:] == 'pm':
print('am or pm')
am_pm = reminder_time_raw[-2:]
raw_time = reminder_time_raw[:-2]
if len(raw_time) == 4:
hours_var = int(raw_time[:2])
minutes_var = int(raw_time[2:4])
elif len(raw_time) == 3:
hours_var = int(raw_time[:1])
minutes_var = int(raw_time[1:3])
elif len(raw_time) <= 2:
hours_var = int(raw_time)
if am_pm == 'pm':
print(hours_var)
if hours_var <= 11:
hours_var += 12
output = datetime.datetime.now().replace(hour=hours_var, minute=minutes_var)
if reminder_time_raw[-1:] == 'a' or reminder_time_raw[-1:] == 'p':
print('a or p')
am_pm = reminder_time_raw[-1:]
raw_time = reminder_time_raw[:-1]
if len(raw_time) == 4:
hours_var = int(raw_time[:2])
minutes_var = int(raw_time[2:4])
elif len(raw_time) == 3:
hours_var = int(raw_time[:1])
minutes_var = int(raw_time[1:3])
elif len(raw_time) <= 2:
hours_var = int(raw_time)
if am_pm == 'p':
if hours_var <= 11:
hours_var += 12
output = datetime.datetime.now().replace(hour=hours_var, minute=minutes_var)
# if time ends in h, m (but is not am or pm) , or d. this will catch it
if output == 0:
modifier = reminder_time_raw[-1:]
raw_time = reminder_time_raw[:-1]
if modifier == 'm':
minutes_var = int(raw_time)
output = datetime.datetime.now() + datetime.timedelta(hours=hours_var, minutes=minutes_var, days=days_var)
elif modifier =='d':
days_var = int(raw_time)
output = datetime.datetime.now() + datetime.timedelta(hours=hours_var, minutes=minutes_var, days=days_var)
elif modifier == 'h':
hours_var = int(raw_time)
output = datetime.datetime.now() + datetime.timedelta(hours=hours_var, minutes=minutes_var, days=days_var)
print('Now: ', now)
print('Output: ', output)
if now > output:
output = output + datetime.timedelta(days=1)
output_string = output.strftime('%m/%d/%Y at %I:%M%p')
output_time_string = output.strftime('%m/%d/%Y')
output_date_string = output.strftime('%I:%M%p')
return output, output_time_string, output_date_string
def send_confirmation(roomId, reminder_time_string, reminder_date_string, person_id, person_nickname):
message = 'Okay, I\'ll remind you on {} at {} <@personId:{}|{}>!'.format(reminder_time_string, reminder_date_string, person_id, person_nickname)
botapi.messages.create(roomId=roomId, markdown=message)
@app.route('/')
def Homepage():
return "Hello Livid!"
@app.route('/sparkbot/messages')
def get_messages():
return 'string'
@app.route('/sparkbot/messages', methods=['POST'])
def receive_message():
incoming_webhook=request.get_json()
incoming_data = incoming_webhook.get("data")
print(incoming_data)
person_id = incoming_data.get("personId")
person_dict= botapi.people.get(person_id)
person_nickname = person_dict.nickName
# print('checking who sent message')
if person_id != bot_id:
# print('personID was not botID')
room_id = incoming_data.get("roomId")
print('Room ID: ', room_id)
message_id = incoming_data.get("id")
print('Message ID: ', message_id)
message_dict = botapi.messages.get(message_id)
print('Message Dict: ', message_dict)
message_text = message_dict.text
print('Message Text:', message_text)
bot_found = False
# remindme_found = False
message_split = message_text.split(" ")
if message_split[0].find(bot_name) > -1:
# print('bot name found')
bot_found = True
# commented out the remind me requirements
# if message_split[1].find("remindme") > -1:
# remindme_found = True
if bot_found: # and remindme_found:
# ToDo should accept time and date
reminder_time, reminder_time_string, reminder_date_string = determine_datetime(message_split[1])
if len(message_split) > 2:
message = ' '.join(message_split[2:])
else:
message = ''
print('Message: ', message)
print('Attempting to send confirmation')
send_confirmation(room_id, reminder_time_string, reminder_date_string, person_id, person_nickname)
save_reminder(reminder_time, room_id, message, person_id, person_nickname)
return '', 204
if __name__ == '__main__':
sleep_time = 15
database_watcher = threading.Thread(target=check_reminders, args=(sleep_time, ))
database_watcher.start()
# app.run(debug=True, use_reloader=False)
app.run('0.0.0.0')