-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtibot.py
687 lines (511 loc) · 22.1 KB
/
tibot.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
import os
import io
from datetime import datetime
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
ConversationHandler,
CallbackContext,
CallbackQueryHandler,
)
from dotenv import load_dotenv
from PIL import Image
from scrapers import wallhaven, bing, unsplash
import image_modify
import local_storage
# Menu enums & load vars
(
state_quote,
state_source,
state_date_menu,
state_image,
state_img_provider,
state_img_searchterm,
state_done_menu,
) = range(7) # All States of application
cbd_quote, cbd_done, cbd_change_image, cbd_new_bg_src = range(4) # Menu options for completion menu
cbd_date_None, cbd_date_Year, cbd_date_MonthYear, cbd_date_DayMonthYear = range(4) # Menu items for date options
cbd_src_wallhaven, cbd_src_unsplash = range(2) # Menu items for image selection menu
load_dotenv()
# Keyboards
FinishMenuKeyboard = [
[
InlineKeyboardButton("Change BG Image", callback_data=str(cbd_change_image)),
InlineKeyboardButton("New BG Source", callback_data=str(cbd_new_bg_src)),
],
[
InlineKeyboardButton("Another Quote", callback_data=str(cbd_quote)),
InlineKeyboardButton("I'm Done", callback_data=str(cbd_done)),
],
]
finish_kb_markup = InlineKeyboardMarkup(FinishMenuKeyboard)
provider_keyboard = [
[
InlineKeyboardButton("Wallhaven", callback_data=str(cbd_src_wallhaven)),
InlineKeyboardButton("Unsplash", callback_data=str(cbd_src_unsplash)),
]
]
provider_markup = InlineKeyboardMarkup(provider_keyboard)
img_search_keyboard = [
[
InlineKeyboardButton("Landscape", callback_data="Landscape"),
InlineKeyboardButton("Abstract", callback_data="Abstract"),
],
[InlineKeyboardButton("Bing Image of the day", callback_data="#BING#")],
]
img_search_markup = InlineKeyboardMarkup(img_search_keyboard)
# Strings
ASKFORIMAGE = "What term should I used to search for a background image?\nYou can send me a (compressed) image or choose an option below"
ASKFORPROVIDER = "Choose a provider below"
ENDCONVO = "Bye bye "
FIRSTIMAGEQUOTE = "Here you go, hot off the presses"
FINISHEDMESSAGE = "What would you like to do now?"
PLZ_WAIT_MSG = "Please wait, we are doing cool image things..."
def tele_img_convert(img: Image):
"""
Converts PIL & telegram images to a Bytes stream which can be used by both PIL & Telegram
Args:
img (PIL Image): The image to be converted
Returns:
Image Stream (BytesIO): Bytes stream of the image
"""
output = io.BytesIO()
img.save(output, format="PNG")
return output.getvalue()
def start(update: Update, context: CallbackContext) -> int:
"""
Initialize the conversation with the user and asks the user for quote
initiated with /start command
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_quote (int): return to conversation handler and wait for a quote before executing the relevant method
"""
update.message.reply_text(
"Hello I am TiBot, the quote wallpapers generation bot. Send me a plaintext quote so I can show you what I can do"
)
return state_quote
def again(update: Update, context: CallbackContext) -> int:
"""
Restart the conversation with the user and asks the user for another quote
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_quote (int): return to conversation handler and wait for a quote before executing the relevant method
"""
query = update.callback_query
query.answer()
context.chat_data.clear()
query.message.edit_text("Great! Please send me a quote?")
return state_quote
def quote(update: Update, context: CallbackContext) -> int:
"""
Stores the quote & asks for the quote master / author
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_source (int): return to conversation handler and wait for a source before executing the relevant method
"""
quote_txt = update.message.text
context.chat_data["Quote"] = quote_txt
# Set the image re-generation flag to false. this will be needed in the image generation methods
context.chat_data["ChangeBG"] = False
update.message.reply_text(f'and who said "{quote_txt}"?')
return state_source
def source(update: Update, context: CallbackContext) -> int:
"""
Stores the quote source (quote master / author) & asks the user for a date
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_date_menu (int): return to conversation handler and wait for a date before executing the relevant method
"""
context.chat_data["Source"] = update.message.text
date_keyboard = [
[
InlineKeyboardButton("None", callback_data=str(cbd_date_None)),
InlineKeyboardButton("Year", callback_data=str(cbd_date_Year)),
],
[
InlineKeyboardButton("Month & Year", callback_data=str(cbd_date_MonthYear)),
InlineKeyboardButton(
"Day, Month & Year", callback_data=str(cbd_date_DayMonthYear)
),
],
]
reply_markup = InlineKeyboardMarkup(date_keyboard)
update.message.reply_text(
"Would you like to add a date?\nYou can choose a preset or type a date if its not from today",
reply_markup=reply_markup,
)
return state_date_menu
def get_date_string(date_callback_data):
"""
Returns the current date in the format specified by the on screen options
Args:
date_callback_data (int): Value provided by the on screen options
Returns:
date (str): Date in the requested format
"""
current_date = datetime.now()
if date_callback_data == "0":
return None
elif date_callback_data == "1":
return current_date.strftime("%Y")
elif date_callback_data == "2":
return current_date.strftime("%B %Y")
elif date_callback_data == "3":
return current_date.strftime("%-m %B %Y")
def date_selection(update: Update, context: CallbackContext) -> int:
"""
Stores the date string (that was generated according to the requested format), then asks the user for an image or search term
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_image (int): return to conversation handler and wait for an image or search term before executing the relevant method
"""
query = update.callback_query
query.answer()
context.chat_data["Date"] = get_date_string(query.data)
query.message.edit_text(ASKFORIMAGE, reply_markup=img_search_markup)
return state_image
def date_text(update: Update, context: CallbackContext) -> int:
"""
Stores the date string (provided by the user), then asks the user for an image or search term
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_image (int): return to conversation handler and wait for an image or search term before executing the relevant method
"""
context.chat_data["Date"] = update.message.text
update.message.reply_text(ASKFORIMAGE, reply_markup=img_search_markup)
return state_image
def img_search_term(update: Update, context: CallbackContext) -> int:
"""
Stores the image search term (user provided), then asks the user which image provider to use
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_img_provider (int): return to conversation handler and wait for an image provider before executing the relevant method
"""
context.chat_data["Image_Seed"] = update.message.text
context.bot.delete_message(
chat_id=update.effective_chat.id, message_id=update.message.message_id - 1
)
update.message.reply_text(ASKFORPROVIDER, reply_markup=provider_markup)
return state_img_provider
def img_search_term_cb(update: Update, context: CallbackContext) -> int:
"""
Stores the image search term (provided by on screen options), then asks the user which image provider to use
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_img_provider (int): return to conversation handler and wait for an image provider before executing the relevant method
"""
query = update.callback_query
query.answer()
context.chat_data["Image_Seed"] = query.data
query.message.edit_text(ASKFORPROVIDER, reply_markup=provider_markup)
return state_img_provider
def change_background(update: Update, context: CallbackContext) -> int:
"""
Calls the specified provider's genereation method to re-generate the image
Sets a variable so the generation method knows not to update the provider
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
status (int): returns the status returned by the generation method
"""
query = update.callback_query
query.answer()
context.chat_data["ChangeBG"] = True
try:
if context.chat_data["Provider"] == str(cbd_src_wallhaven):
return wallhavenimage(update, context)
elif context.chat_data["Provider"] == str(cbd_src_unsplash):
return unsplashimage(update, context)
except KeyError:
query.message.reply_text("This image source will just provide the same image.\nTo change the image you will need to change the provider", reply_markup=InlineKeyboardMarkup(FinishMenuKeyboard))
return state_done_menu
def wallhavenimage(update: Update, context: CallbackContext) -> int:
"""
Uses the search term to get a maching image.
The image, quote, source & optional date is submitted for generation of the final quote image.
Sends the completed image to the user.
Checks if quotes created in the chat should be saved. The image is saved if so.
Finally prompt the user for the next action.
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
On successful generation:
state_done_menu (int): return to conversation handler and wait for a completed menu option before executing the relevant method
If no results are found:
status (int): returns the status returned by the new_img_src method
If a different error occurs:
status (int): returns the status returned by the new_img_src method
"""
query = update.callback_query
query.answer()
# Checks if this is a re-generation of an image, if so it won't update the image provider
# If this isnt here, the provider will be set to whichever provider carries the same value as the "New BG" button
if not context.chat_data["ChangeBG"]:
context.chat_data["Provider"] = query.data
else:
context.chat_data["ChangeBG"] = False
try: # Try to get the image and map the quote
wait_msg = query.message.reply_text(PLZ_WAIT_MSG)
bg = wallhaven.get_random_image(context.chat_data["Image_Seed"])
final_img = image_modify.draw_text_layer(
bg,
context.chat_data["Quote"],
context.chat_data["Source"],
context.chat_data["Date"],
)
query.message.reply_document(
tele_img_convert(final_img), caption=FIRSTIMAGEQUOTE
)
context.bot.deleteMessage(
message_id=wait_msg.message_id, chat_id=query.message.chat_id
)
context.chat_data["LocalStorageFilePath"] = local_storage.do_local_storage(
update, context, final_img
)
query.message.reply_text(FINISHEDMESSAGE, reply_markup=finish_kb_markup)
return state_done_menu
except IndexError: # Catch Index Error (caused by no results)
print(
f"Wallhaven scraper found no results for {context.chat_data['Image_Seed']}"
)
query.message.reply_text("No images with that seed, please send another seed")
return new_img_src(update, context)
except Exception as exc_txt: # Catch anything else that might break
print(exc_txt)
query.message.reply_text("I'm sorry, Something went wrong")
return new_img_src(update, context)
def bingimage(update: Update, context: CallbackContext) -> int:
"""
Fetches the Bing image.
The image, quote, source & optional date is submitted for generation of the final quote image.
Sends the completed image to the user.
Checks if quotes created in the chat should be saved. The image is saved if so.
Finally prompt the user for the next action.
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_done_menu (int): return to conversation handler and wait for a completed menu option before executing the relevant method
"""
query = update.callback_query
query.answer()
wait_msg = query.message.reply_text(PLZ_WAIT_MSG)
bg = bing.get_image()
final_img = image_modify.draw_text_layer(
bg,
context.chat_data["Quote"],
context.chat_data["Source"],
context.chat_data["Date"],
)
query.message.reply_document(tele_img_convert(final_img), caption=FIRSTIMAGEQUOTE)
context.bot.deleteMessage(
message_id=wait_msg.message_id, chat_id=query.message.chat_id
)
context.chat_data["LocalStorageFilePath"] = local_storage.do_local_storage(
update, context, final_img
)
query.message.reply_text(FINISHEDMESSAGE, reply_markup=finish_kb_markup)
return state_done_menu
def unsplashimage(update: Update, context: CallbackContext) -> int:
"""
Uses the search term to get a maching image, artist information is included in the returned object.
The image, quote, source & optional date is submitted for generation of the final quote image.
Sends the completed image to the user, including the attribution info & links that is required by the unsplash API.
Checks if quotes created in the chat should be saved. The image is saved if so.
Finally prompt the user for the next action.
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
On successful generation:
state_done_menu (int): return to conversation handler and wait for a completed menu option before executing the relevant method
If no results are found:
status (int): returns the status returned by the new_img_src method
If a different error occurs:
status (int): returns the status returned by the new_img_src method
"""
query = update.callback_query
query.answer()
# Checks if this is a re-generation of an image, if so it won't update the image provider
# If this isnt here, the provider will be set to whichever provider carries the same value as the "New BG" button
if not context.chat_data["ChangeBG"]:
context.chat_data["Provider"] = query.data
else:
context.chat_data["ChangeBG"] = False
try: # Try to get the image and map the quote
wait_msg = query.message.reply_text(PLZ_WAIT_MSG)
bg_tuple = unsplash.get_image(context.chat_data["Image_Seed"])
final_img = image_modify.draw_text_layer(
bg_tuple[2],
context.chat_data["Quote"],
context.chat_data["Source"],
context.chat_data["Date"],
)
captiontext = "{0}\n\nImage by [{1}]({2}?utm_source={3}&utm_medium=referral) on [Unsplash](https://unsplash.com/?utm_source={3}&utm_medium=referral)".format(
FIRSTIMAGEQUOTE,
bg_tuple[0],
bg_tuple[1],
os.getenv("UnsplashAppName")
)
query.message.reply_document(
tele_img_convert(final_img), caption=captiontext, parse_mode="MarkdownV2"
)
context.bot.deleteMessage(
message_id=wait_msg.message_id, chat_id=query.message.chat_id
)
context.chat_data["LocalStorageFilePath"] = local_storage.do_local_storage(
update, context, final_img
)
query.message.reply_text(FINISHEDMESSAGE, reply_markup=finish_kb_markup)
return state_done_menu
except IndexError: # Catch Index Error (caused by no results)
print( f"Unsplash scraper found no results for {context.chat_data['Image_Seed']}" )
query.message.reply_text("No images with that seed, please send another seed")
return new_img_src(update, context)
except Exception as exc_txt: # Catch anything else that might break
print(exc_txt)
query.message.reply_text("I'm sorry, Something went wrong")
return new_img_src(update, context)
def uploadedimage(update: Update, context: CallbackContext) -> int:
"""
Received the image from the user
The image, quote, source & optional date is submitted for generation of the final quote image.
Sends the completed image to the user.
Checks if quotes created in the chat should be saved. The image is saved if so.
Finally prompt the user for the next action.
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_done_menu (int): return to conversation handler and wait for a completed menu option before executing the relevant method
"""
imagefile = context.bot.get_file(update.message.photo[-1].file_id)
imagebytes = io.BytesIO(imagefile.download_as_bytearray())
bg = Image.open(imagebytes)
final_img = image_modify.draw_text_layer(
bg,
context.chat_data["Quote"],
context.chat_data["Source"],
context.chat_data["Date"],
)
update.message.reply_document(tele_img_convert(final_img), caption=FIRSTIMAGEQUOTE)
context.chat_data["LocalStorageFilePath"] = local_storage.do_local_storage(
update, context, final_img
)
update.message.reply_text(FINISHEDMESSAGE, reply_markup=finish_kb_markup)
return state_done_menu
def new_img_src(update: Update, context: CallbackContext) -> int:
"""
Prompt the user for an image or search term
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
state_image (int): return to conversation handler and wait for an image or search term before executing the relevant method
"""
query = update.callback_query
query.answer()
query.message.reply_text(ASKFORIMAGE, reply_markup=img_search_markup)
return state_image
def callback_cancel(update: Update, context: CallbackContext) -> int:
"""
Fallback method to cancel & end the conversation
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
END (int): status update to end conversation
"""
query = update.callback_query
query.answer()
context.chat_data.clear()
query.message.edit_text(ENDCONVO + query.from_user.first_name)
return ConversationHandler.END
def cancel(update: Update, context: CallbackContext) -> int:
"""
Fallback method to cancel & end the conversation
Args:
update (Update): incomming update object
context (CallbackContext): callback for dispatching
Returns:
END (int): status update to end conversation
"""
context.chat_data.clear()
update.message.reply_text(ENDCONVO + update.message.from_user.username)
return ConversationHandler.END
def main():
"""
Run the main conversation handler
"""
# Create the Updater and pass it your bot's token.
updater = Updater(os.getenv("TOKEN"))
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start), CommandHandler("cancel", cancel)],
states={
state_quote: [MessageHandler(Filters.text, quote)],
state_source: [MessageHandler(Filters.text, source)],
state_date_menu: [
CallbackQueryHandler(date_selection),
MessageHandler(Filters.text, date_text),
],
state_image: [
MessageHandler(Filters.text, img_search_term),
MessageHandler(Filters.photo, uploadedimage),
CallbackQueryHandler(bingimage, pattern="#BING#"),
CallbackQueryHandler(img_search_term_cb),
],
state_img_provider: [
CallbackQueryHandler(
wallhavenimage, pattern="^" + str(cbd_src_wallhaven) + "$"
),
CallbackQueryHandler(
unsplashimage, pattern="^" + str(cbd_src_unsplash) + "$"
),
],
state_done_menu: [
CallbackQueryHandler(again, pattern="^" + str(cbd_quote) + "$"),
CallbackQueryHandler(
change_background, pattern="^" + str(cbd_change_image) + "$"
),
CallbackQueryHandler(
new_img_src, pattern="^" + str(cbd_new_bg_src) + "$"
),
CallbackQueryHandler(
callback_cancel, pattern="^" + str(cbd_done) + "$"
),
],
},
fallbacks=[CommandHandler("cancel", cancel)],
)
dispatcher.add_handler(conv_handler)
# 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()