-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.py
More file actions
359 lines (281 loc) · 12.7 KB
/
dev.py
File metadata and controls
359 lines (281 loc) · 12.7 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
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
import logging
import datetime
from ocr import *
from fridge import *
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, KeyboardButton, ReplyKeyboardMarkup, PhotoSize
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters, RegexHandler,ConversationHandler, InlineQueryHandler, JobQueue, RegexHandler
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
knownUser = {}
commands = {
"start" : 'Initialise User',
"help" : 'Gives you information about the available commands',
"add" : "Adds food item into your fridge",
"used" : "Removes food item from fridge",
"display" : "See what's in your Fridge",
"clear" : "Resets your fridge",
"scan" : "Send a photo of your receipt",
"addexpiry" : "Add expiry date for items"
}
#/start
def start(bot, update):
cid = update.message.chat_id
if cid not in knownUser:
fridge = Fridge()
knownUser[cid]=fridge
bot.send_message(chat_id=cid, text="Hello! This bot helps you to reduce food wastage by managing your fridge.")
#create a instance of fridge
else:
bot.send_message(chat_id=cid, text="User Data already stored!")
help(bot, update)
#/Help
def help(bot, update):
cid = update.message.chat_id
help_text = "The following commands are available: \n"
for key in commands.keys():
help_text += "/" + key + ": "
help_text += commands[key] + "\n"
bot.send_message(chat_id=cid, text=help_text)
#USED FOR /Add
cat = ["FRUITS", "VEGETABLES", "MEAT", "DAIRY PRODUCTS", "OTHERS"]
CAT, FOOD, OTHERS, EXPIRE= range(4)
def add(bot, update):
keyboard = [[InlineKeyboardButton(text= k, callback_data = k)] for k in cat]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('What would you like to add? \n Please choose:', reply_markup=reply_markup)
return CAT
def button(bot, update, user_data):
query = update.callback_query
cid = query.message.chat_id
cat = knownUser[cid].get_category(query.data)
user_data["Cat"]= query.data
keyboard = [[InlineKeyboardButton(text= k, callback_data = k)] for k in cat]
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text(text="{} Selected".format(query.data),
chat_id=query.message.chat_id,
message_id=query.message.message_id, reply_markup=reply_markup)
return FOOD
def button1(bot, update, user_data):
query = update.callback_query
if query.data== "Others":
bot.edit_message_text(text="What is the item?",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
return OTHERS
else:
user_data["fruit"]= query.data
keyboard = [["1","2","3"],["4","5","6"], ["7", "8", "9"],["0",]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard= True, one_time_keyboard= True)
bot.edit_message_text(text="Selected option: {}".format(query.data),
chat_id=query.message.chat_id,
message_id=query.message.message_id)
bot.send_message(chat_id=query.message.chat_id, text= "How many days to expiry?", reply_markup = reply_markup)
return EXPIRE
def newItem(bot, update, user_data):
text = update.message.text.upper()
user_data['fruit'] = text
cid=update.message.chat_id
knownUser[cid].add_entry_to_cat(text, user_data["Cat"])
keyboard = [["1","2","3"],["4","5","6"], ["7", "8", "9"],["0",]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard= True, one_time_keyboard= True)
bot.send_message(chat_id=cid, text= "When will it expire?", reply_markup= reply_markup)
return EXPIRE
def expire(bot, update, user_data):
text = update.message.text
user_data['expire'] = text
new= Food(user_data["fruit"], int(user_data['expire']), user_data["Cat"]) #food inital
cid=update.message.chat_id
knownUser[cid].add_food(new) #add food to fridge
bot.send_message(chat_id=cid, text= "Item Successfully Added")
return ConversationHandler.END
def cancel(bot, update):
update.message.reply_text('Bye!')
return ConversationHandler.END
#/remove
USED , REMOVED = range(2)
cat = ["FRUITS", "VEGETABLES", "MEAT", "DAIRY PRODUCTS", "OTHERS"]
def used(bot, update):
cid = update.message.chat_id
keyboard = [[InlineKeyboardButton(text= k, callback_data = k)] for k in cat]
reply_markup = InlineKeyboardMarkup(keyboard)
update.message.reply_text('Please choose what you want to remove:', reply_markup=reply_markup)
return USED
def button2(bot, update, user_data):
query = update.callback_query
cid = query.message.chat_id
cat = knownUser[cid].print_by_category(query.data) #get whole inventory for fride
keyboard = [[InlineKeyboardButton(text= k, callback_data = k)] for k in cat]
reply_markup = InlineKeyboardMarkup(keyboard)
bot.edit_message_text('Please choose what you would like to remove:',
chat_id=query.message.chat_id,
message_id=query.message.message_id, reply_markup=reply_markup)
return REMOVED
def remove(bot, update, user_data):
query = update.callback_query
cid = query.message.chat_id
knownUser[cid].remove_food(query.data) #remove food
bot.edit_message_text(text="{} has been removed".format(query.data),
chat_id=query.message.chat_id,
message_id=query.message.message_id)
return ConversationHandler.END
#/dísplay
def display(bot, update):
cid = update.message.chat_id
res = knownUser[cid].print_full_fridge() #print food name list
bot.send_message(chat_id=cid, text=res)
#/time
def alert(bot,job):
for k,v in knownUser.items():
res = v.daily_update() #Get list of expired
try:
bot.send_message(chat_id=k, text = res)
except:
continue
#/clear
CLEAR = 0
def clear(bot, update):
cid = update.message.chat_id
keyboard = [["Yes"],["No"]]
reply_markup = ReplyKeyboardMarkup(keyboard,resize_keyboard = True, one_time_keyboard = True)
update.message.reply_text('Are you sure you want to clear your fridge?', reply_markup = reply_markup)
return CLEAR
def cfm_Clear(bot, update):
text = update.message.text
cid=update.message.chat_id
if text == "Yes":
knownUser[cid].clear()
keyboard = [[InlineKeyboardButton(text= k, callback_data = k)] for k in cat]
reply_markup = InlineKeyboardMarkup(keyboard)
bot.send_message(chat_id=cid, text= "Fridge has been cleared")
return ConversationHandler.END
else:
return ConversationHandler.END
#/scan
PICTURE=0
def scan(bot, update):
cid = update.message.chat_id
update.message.reply_text('Please send me a picture!')
return PICTURE
scan_res = []
def image_handler(bot, update):
#file = bot.getFile(update.message.photo.file_id)
#file_id = update.message.photo.file_id
cid = update.message.chat_id
file = bot.getFile(update.message.photo[-1].file_id)
file.download('image.jpg')
text = convert_image("C:/Users/justu/Desktop/H&R/image.jpg")
text= knownUser[cid].add_bulk(text)
global scan_res
scan_res.extend(text)
res= ""
for i in range(len(text)):
res += str(i+1) + ". " + text[i][0] + " \n"
bot.send_message(chat_id=cid, text= "These items are temporary added please add a expiry date to confirm your item! \n" + res)
return ConversationHandler.END
#/addexpiry
CHOOSE, ADDITEM, CHOOSE2 = range(3)
def make():
list2= list(map(lambda x: x[0], scan_res))
keyboard2 = [[InlineKeyboardButton(text= k, callback_data = k)] for k in list2]
keyboard2.append([InlineKeyboardButton(text= "Done", callback_data = "Done")])
reply_markup2 = InlineKeyboardMarkup(keyboard2)
return reply_markup2
def addexpiry(bot, update):
cid=update.message.chat_id
bot.send_message(chat_id=cid, text= "Please add an expiry duration!",reply_markup= make())
return CHOOSE
def choose(bot,update):
cid=update.message.chat_id
bot.send_message(chat_id=cid, text= "Please add an expiry duration!",reply_markup= make())
return ADDITEM
def exp(bot, update, user_data):
query = update.callback_query
if query.data != "Done":
user_data["fruit"]= query.data
keyboard = [["1","2","3"],["4","5","6"], ["7", "8", "9"],["0",]]
reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard= True, one_time_keyboard= True)
bot.edit_message_text(text="Selected option: {}".format(query.data),
chat_id=query.message.chat_id,
message_id=query.message.message_id)
bot.send_message(chat_id=query.message.chat_id, text= "How many days to expiry?", reply_markup = reply_markup)
return ADDITEM
else:
bot.send_message(chat_id=query.message.chat_id, text= "Items have been successfully added")
return ConversationHandler.END
def newItem2(bot, update, user_data):
text = update.message.text
user_data['expire'] = text
global scan_res
for i in scan_res:
if i[0] == user_data["fruit"]:
res=i[1]
scan_res.remove(i)
new= Food(user_data["fruit"], int(user_data['expire']), res) #food inital
cid=update.message.chat_id
knownUser[cid].add_food(new) #add food to fridge
bot.send_message(chat_id=cid, text= "Item Successfully Added")
return CHOOSE2
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
"""Start the bot."""
# Create the EventHandler and pass it your bot's token.
updater = Updater("YOUR TOKEN HERE")
j = updater.job_queue
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("display", display))
#dp.add_handler(CommandHandler("add", add))
#dp.add_handler(CallbackQueryHandler(button))
conv_handler_add = ConversationHandler(
entry_points=[CommandHandler('add', add)],
states={CAT: [CallbackQueryHandler(button ,pass_user_data = True)],
FOOD : [CallbackQueryHandler(button1 ,pass_user_data = True)],
OTHERS: [MessageHandler(Filters.text, newItem, pass_user_data = True)],
EXPIRE: [MessageHandler(Filters.text, expire, pass_user_data = True)]
},
fallbacks=[CommandHandler('cancel', cancel)])
dp.add_handler(conv_handler_add)
conv_handler_used = ConversationHandler(
entry_points=[CommandHandler('used', used)],
states={USED: [CallbackQueryHandler(button2 ,pass_user_data = True)],
REMOVED: [CallbackQueryHandler(remove ,pass_user_data = True)]
},
fallbacks=[CommandHandler('cancel', cancel)])
dp.add_handler(conv_handler_used)
conv_handler_clear = ConversationHandler(
entry_points=[CommandHandler('clear', clear)],
states={CLEAR: [MessageHandler(Filters.text, cfm_Clear)]
},
fallbacks=[CommandHandler('cancel', cancel)])
dp.add_handler(conv_handler_clear)
conv_handler_scan = ConversationHandler(
entry_points=[CommandHandler('scan', scan)],
states={PICTURE: [MessageHandler(Filters.photo, image_handler)],
},
fallbacks=[CommandHandler('cancel', cancel)])
dp.add_handler(conv_handler_scan)
conv_handler_addexpiry = ConversationHandler(
entry_points=[CommandHandler('addexpiry', addexpiry)],
states={CHOOSE: [CallbackQueryHandler(exp ,pass_user_data = True)],
ADDITEM:[MessageHandler(Filters.text, newItem2, pass_user_data = True)],
CHOOSE2: [CallbackQueryHandler(choose ,pass_user_data = True)]
},
fallbacks=[CommandHandler('cancel', cancel)])
dp.add_handler(conv_handler_addexpiry)
# log all errors
dp.add_error_handler(error)
#Schedule Jobs
j.run_daily(alert, datetime.time(8,0,0))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()