forked from astraldawn/dinnerBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
356 lines (281 loc) · 9.56 KB
/
commands.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
from config import API_URL
import requests
import sys
import datetime
import utils
import errors
def welcome(bot, update):
bot.sendMessage(update.message.chat_id, text='Hi!')
def help(bot, update):
bot.sendMessage(update.message.chat_id, text='Help!')
def info(bot, update):
user_name, user_id, group = utils.get_info(update)
bot.sendMessage(group, text=user_name + ' ' + user_id)
# Register group (should automate this though)
def register_group(bot, update):
user_name, user_id, group = utils.get_info(update)
# Server request
data = {
'group': group
}
r = requests.post(API_URL + 'register_group', json=data)
# Response handling
if r.status_code == 200:
bot.sendMessage(group, text='Group registered')
else:
bot.sendMessage(group, text='Group already registered / registration failed')
# Registration command
def register(bot, update):
user_name, user_id, group = utils.get_info(update)
# Server request
data = {
'id': user_id,
'username': user_name,
'groupID': group
}
r = requests.post(API_URL + 'register', json=data)
print(r.text)
# Response handling
if r.status_code == 200:
bot.sendMessage(group, text=user_name + ': Successful registration')
else:
bot.sendMessage(group, text=user_name + ': Registration failed / already registered')
# Start meal
def start_meal(bot, update, args):
user_name, user_id, group = utils.get_info(update)
# TODO: Check the number of args
if len(args) == 0:
bot.sendMessage(group, text=user_name + ", please indicate meal type, for example: '/start lunch'")
return
else:
meal_type = args[0].strip()
# Request
data = {
'group': group,
'meal_type': meal_type
}
r = requests.post(API_URL + 'add_meal', json=data)
# Response handling
if r.status_code == 200:
data2 = {
'user_id': user_id,
'group': group,
'portions': 1,
'cooked': True
}
r2 = requests.post(API_URL + 'eating', json=data2)
bot.sendMessage(group, text='Thanks for cooking, ' + user_name + '! You are eating 1 portion')
bot.sendMessage(group, text='Who is eating ' + meal_type + '?')
else:
bot.sendMessage(group, text='Another meal is running / unable to start meal')
# End meal
def end_meal(bot, update, args):
user_name, user_id, group = utils.get_info(update)
# Request
data = {
'group': group
}
r = requests.post(API_URL + 'end_meal', json=data)
# Response handling
# Do not end the meal immediately, should trigger some handlers for warnings
# E.g. 15min / 10min
if r.status_code == 200:
bot.sendMessage(group, text='Meal ended')
else:
bot.sendMessage(group, text='Unable to end meal')
def representsInt(s):
try:
int(s)
return True
except ValueError:
return False
def meal_participation(bot, update, args, cooked):
user_name, user_id, group = utils.get_info(update)
# Argument handling
if len(args) == 1:
# TODO: Check arg value
if not representsInt(args[0]):
bot.sendMessage(group, text="Zzz. Please type an integer as the argument, or nothing at all.")
return
elif int(args[0]) < 0:
bot.sendMessage(group, text="Nice try...but nope.")
return
portion = int(args[0])
elif len(args) == 0:
portion = 1
else:
utils.throw_error(bot, group, errors.WRONG_N_ARGS)
return
portion_text = str(portion) + (' portion' if portion == 1 else ' portions')
# Request
data = {
'user_id': user_id,
'group': group,
'portions': portion,
'cooked': cooked
}
r = requests.post(API_URL + 'eating', json=data)
if r.status_code == 200:
if portion == 0:
bot.sendMessage(group, text=user_name + ' removed from meal')
elif cooked:
bot.sendMessage(group, text='Thanks for cooking, ' + user_name + '! You are eating ' + portion_text)
else:
bot.sendMessage(group, text=user_name + ' eating ' + portion_text)
else:
bot.sendMessage(group, text='Unable to update, is a meal started?')
# Eating
def eating(bot, update, args):
meal_participation(bot, update, args, cooked=False)
# Cooking
def cooking(bot, update, args):
meal_participation(bot, update, args, cooked=True)
# Return information for currently running meal
def meal_info(bot, update, args):
user_name, user_id, group = utils.get_info(update)
# Argument handling
meal_id = -1
if len(args) == 1:
meal_id = args[0]
# Request
data = {
'group': group,
'meal_id': meal_id
}
r = requests.post(API_URL + 'meal_info', json=data)
if r.status_code == 200:
meal_participations = r.json()
output = ''
total_portions = 0
if len(meal_participations) == 0:
bot.sendMessage(group, 'No information to display')
return
for mp in meal_participations:
output += str(mp['user_name']) + ' '
output += str(mp['portions']) + (' portion' if mp['portions'] == 1 else ' portions')
output += ' - cooking' if mp['cooked'] else ''
output += '\n'
total_portions += mp['portions']
output += str(total_portions) + (' portion' if total_portions == 1 else ' portions') + ' required'
bot.sendMessage(group, text=output)
else:
bot.sendMessage(group, text='Unable to retrieve meal information')
# Return the last n meals
def get_meals(bot, update, args):
user_name, user_id, group = utils.get_info(update)
# Argument handling
number = 5
if len(args) > 0:
number = args[0]
data = {
'group': group,
'number': number
}
r = requests.post(API_URL + 'meals', json=data)
if r.status_code == 200:
meals = r.json()
output = ''
if len(meals) == 0:
bot.sendMessage(group, 'No information to display')
return
for meal in meals:
output += 'ID: ' + str(meal[0]) + ' '
output += 'Portions: ' + str(meal[1])
output += '\n'
output += str(len(meals)) + ' meals retrieved'
bot.sendMessage(group, text=output)
else:
bot.sendMessage(group, text='Unable to retrieve information')
# Tally the last n users
def tally_group(bot, update, args):
user_name, user_id, group = utils.get_info(update)
data = {
'group': group,
'set_date': False if len(args) == 0 else True
}
r = requests.post(API_URL + 'tally_group', json=data)
if r.status_code == 200:
users = r.json()
output = ''
total = 0
most_social = ''
least_portions = sys.maxsize
if len(users) == 0:
bot.sendMessage(group, 'No information to display')
return
for user, portions in users.items():
output += user + ' - ' + str(portions) + '\n'
total += portions
if portions < least_portions:
# Need to handle the case where multiple people tie
most_social = user
least_portions = portions
output += str(total) + ' portions consumed\n'
output += most_social + ' has the most life'
bot.sendMessage(group, text=output)
else:
bot.sendMessage(group, text='Unable to retrieve information')
# Tally information for the user
def tally_user(bot, update, args):
user_name, user_id, group = utils.get_info(update)
data = {
'group': group,
'user_id': user_id
}
r = requests.post(API_URL + 'tally_user', json=data)
if r.status_code == 200:
output = ''
meals = r.json()
print(meals)
for meal in meals:
output += meal['type'] + ' ' + meal['date'] + '\n'
output += 'Involved in ' + str(len(meals)) + ' meals'
bot.sendMessage(group, text=output)
else:
bot.sendMessage(group, text='Unable to retrieve information')
# Meal modification routes
def modify_cooking(bot, update, args, cooking):
user_name, user_id, group = utils.get_info(update)
meal_id = None
if len(args) == 1:
meal_id = int(args[0])
else:
utils.throw_error(bot, group, errors.WRONG_N_ARGS)
return
data = {
'user_id': user_id,
'meal_id': meal_id
}
if cooking:
r = requests.post(API_URL + 'add_chef', json=data)
else:
r = requests.post(API_URL + 'remove_chef', json=data)
if r.status_code == 200:
bot.sendMessage(group, text='Update successful')
else:
bot.sendMessage(group, text='Update unsuccessful')
def add_chef(bot, update, args):
modify_cooking(bot, update, args, cooking=True)
def remove_chef(bot, update, args):
modify_cooking(bot, update, args, cooking=False)
# Change number of portions
def change_portions(bot, update, args):
user_name, user_id, group = utils.get_info(update)
meal_id = None
portions = None
if len(args) == 2:
meal_id = int(args[0])
portions = int(args[1])
else:
utils.throw_error(bot, group, errors.WRONG_N_ARGS)
return
data = {
'user_id': user_id,
'meal_id': meal_id,
'portions': portions
}
r = requests.post(API_URL + 'change_portions', json=data)
if r.status_code == 200:
bot.sendMessage(group, text='Update successful')
else:
bot.sendMessage(group, text='Update unsuccessful')