-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinal.py
More file actions
453 lines (402 loc) · 16.2 KB
/
final.py
File metadata and controls
453 lines (402 loc) · 16.2 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from telegram.ext import CommandHandler, Updater, CallbackQueryHandler, MessageHandler, ConversationHandler, Filters
from telegram import ChatAction, ParseMode, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, Message
from datetime import datetime
import json
import os
import requests
import logging
trigger_find=False
trigger_lose=False
# Load bot token
with open('token1.ini', 'r') as file:
BOT_TOKEN = file.read()
# Create bot
updater = Updater(token=BOT_TOKEN, use_context=True)
RESPONSE, NO_PIC = range(2)
############################## Conversation handlers ###########################################
def report(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please upload a photo and a brief description of the item you found. (Note: It will not work if no picture is sent.)'
)
return RESPONSE
def response(update, context):
user = update.message.from_user
photo_file = update.message.photo[-1].get_file()
photo_file.download('user_photo.jpg')
update.message.reply_text('Thank you for your report. It will now be filed in our records.')
return ConversationHandler.END
def no_pic(update, context):
user = update.message.from_user
update.message.reply_text('Please submit a picture of the item')
return RESPONSE
def cancel(update, context):
user = update.message.from_user
update.message.reply_text('Thank you for using our bot!')
return ConversationHandler.END
# Function to construct inline keyboard
def build_menu(buttons, n_cols, header_buttons=None, footer_buttons=None):
menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
if header_buttons:
menu.insert(0, [header_buttons])
if footer_buttons:
menu.append([footer_buttons])
return menu
# Add /start handler
def start(update, context):
print('/start has been called')
context.bot.send_message(
chat_id = update.effective_chat.id,
text = f'Hello! Did you /find or /lose item(s)?'
)
def delete(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'This item will be removed from our records. Thank you for using our bot!'
)
def find(update, context):
global trigger_find
trigger_find=True
print('/find has been selected')
button_list = [
InlineKeyboardButton("Personal", callback_data = 'find_1'),
InlineKeyboardButton("Academic", callback_data = 'find_2'),
InlineKeyboardButton("CCA", callback_data = 'find_3'),
InlineKeyboardButton("Others", callback_data = 'find_4'),
InlineKeyboardButton("Main Menu", callback_data= 'find_5')
]
reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=2))
context.bot.send_message(
chat_id = update.effective_chat.id, text = main_menu_message(),
reply_markup=reply_markup
)
def lose(update, context):
global trigger_lose
trigger_lose=True
print('/lose has been selected')
button_list = [
InlineKeyboardButton("Personal", callback_data = 'lose_1'),
InlineKeyboardButton("Academic", callback_data = 'lose_2'),
InlineKeyboardButton("CCA", callback_data = 'lose_3'),
InlineKeyboardButton("Others", callback_data = 'lose_4'),
InlineKeyboardButton("Main Menu", callback_data= 'lose_5')
]
reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=2))
context.bot.send_message(
chat_id = update.effective_chat.id, text = main_menu_message(),
reply_markup=reply_markup
)
def personal(update, context):
print('personal has been selected')
query = update.callback_query
context.bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=sub_menu_message(),
reply_markup=personal_keyboard())
def academic(update, context):
print('academic has been selected')
query = update.callback_query
context.bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=sub_menu_message(),
reply_markup=academic_keyboard())
def cca(update, context):
print('cca has been selected')
query = update.callback_query
context.bot.edit_message_text(chat_id=query.message.chat_id,
message_id=query.message.message_id,
text=sub_menu_message(),
reply_markup=cca_keyboard())
def others(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Others has been selected'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def valuables(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Valuables has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def identification(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Identification has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def electronic(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Electronics has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
trigger_find = not trigger_find
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
trigger_lose = not trigger_lose
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
img_url='https://imgur.com/rIglB1C'
context.bot.send_photo(
chat_id=update.effective_chat.id,
photo=img_url,
caption='Apple laptop with black sleeve found at Cinnamon College dining hall at 4.30pm',
parse_mode=ParseMode.MARKDOWN
)
keyboard = [
InlineKeyboardButton('This is mine!', callback_data= 'delete')
]
reply_markup = InlineKeyboardMarkup(build_menu(keyboard, n_cols=1))
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please verify whether this is your item!',
reply_markup=reply_markup
)
trigger_lose=False
def essentials(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Essentials has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def notes(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Notes has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def textbooks(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Textbooks has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def library_books(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Library books has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def physical_assignments(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Assignments has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def sports_equipment(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Sports equipment has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
def costumes_attires(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Costumes has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost'
)
def instruments(update, context):
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Instruments has been selected.'
)
global trigger_lose
global trigger_find
if trigger_find:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Type /report to submit a report for an item you found.'
)
else:
context.bot.send_message(
chat_id = update.effective_chat.id,
text = 'Please check if the following entries matches the item you lost.'
)
############################ Keyboards #########################################
def personal_keyboard():
keyboard = [[InlineKeyboardButton('Valuables', callback_data='personal_1')],
[InlineKeyboardButton('Identification', callback_data='personal_2')],
[InlineKeyboardButton('Electronic', callback_data='personal_3')],
[InlineKeyboardButton('Essentials', callback_data='personal_4')],
[InlineKeyboardButton('Main Menu', callback_data='personal_5')]
]
return InlineKeyboardMarkup(keyboard)
def academic_keyboard():
keyboard = [[InlineKeyboardButton('Notes', callback_data='academic_1')],
[InlineKeyboardButton('Textbooks', callback_data='academic_2')],
[InlineKeyboardButton('Library Books', callback_data='academic_3')],
[InlineKeyboardButton('Physical Assignments', callback_data='academic_4')],
[InlineKeyboardButton('Main Menu', callback_data='academic_5')]
]
return InlineKeyboardMarkup(keyboard)
def cca_keyboard():
keyboard = [[InlineKeyboardButton('Sports Equipment', callback_data='cca_1')],
[InlineKeyboardButton('Costumes/Attires', callback_data='cca_2')],
[InlineKeyboardButton('Instruments', callback_data='cca_3')],
[InlineKeyboardButton('Main Menu', callback_data='cca_4')]
]
return InlineKeyboardMarkup(keyboard)
############################# Messages #########################################
def main_menu_message():
return 'Please choose a category:'
def sub_menu_message():
return 'Please choose a sub-category:'
############################# Handlers #########################################
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CommandHandler('find', find))
updater.dispatcher.add_handler(CommandHandler('lose', lose))
updater.dispatcher.add_handler(CallbackQueryHandler(personal, pattern='find_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(academic, pattern='find_2'))
updater.dispatcher.add_handler(CallbackQueryHandler(cca, pattern='find_3'))
updater.dispatcher.add_handler(CallbackQueryHandler(others, pattern='find_4'))
updater.dispatcher.add_handler(CallbackQueryHandler(start, pattern='find_5'))
updater.dispatcher.add_handler(CallbackQueryHandler(personal, pattern='lose_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(academic, pattern='lose_2'))
updater.dispatcher.add_handler(CallbackQueryHandler(cca, pattern='lose_3'))
updater.dispatcher.add_handler(CallbackQueryHandler(others, pattern='lose_4'))
updater.dispatcher.add_handler(CallbackQueryHandler(start, pattern='lose_5'))
updater.dispatcher.add_handler(CallbackQueryHandler(valuables, pattern='personal_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(identification, pattern='personal_2'))
updater.dispatcher.add_handler(CallbackQueryHandler(electronic, pattern='personal_3'))
updater.dispatcher.add_handler(CallbackQueryHandler(essentials, pattern='personal_4'))
updater.dispatcher.add_handler(CallbackQueryHandler(start, pattern='personal_5'))
updater.dispatcher.add_handler(CallbackQueryHandler(notes, pattern='academic_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(textbooks, pattern='academic_2'))
updater.dispatcher.add_handler(CallbackQueryHandler(library_books, pattern='academic_3'))
updater.dispatcher.add_handler(CallbackQueryHandler(physical_assignments, pattern='academic_4'))
updater.dispatcher.add_handler(CallbackQueryHandler(start, pattern='academic_5'))
updater.dispatcher.add_handler(CallbackQueryHandler(sports_equipment, pattern='cca_1'))
updater.dispatcher.add_handler(CallbackQueryHandler(costumes_attires, pattern='cca_2'))
updater.dispatcher.add_handler(CallbackQueryHandler(instruments, pattern='cca_3'))
updater.dispatcher.add_handler(CallbackQueryHandler(start, pattern='cca_4'))
conv_handler = ConversationHandler(
entry_points=[CommandHandler('report', report)],
states={
RESPONSE: [MessageHandler(Filters.photo, response)],
NO_PIC: [MessageHandler(Filters.text, no_pic)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
dp = updater.dispatcher
dp.add_handler(conv_handler)
dp.add_handler(CallbackQueryHandler(delete, pattern='delete'))
updater.start_polling()
print('Bot started!')