Various modules

Description

Modules that contains various helper functions

photo_utils

Generates the image based on the user’s settings

modules.various.photo_utils.build_bg_path(sender_id: int) → str

Builds the path for the background image sent by the user

Parameters

sender_id (int) – id of the userd that sent the image

Returns

path where to save/find the image

Return type

str

modules.various.photo_utils.build_photo_path(sender_id: int) → str

Builds the path for the edited image requested by the user

Parameters

sender_id (int) – id of the userd that sent the image

Returns

path where to save/find the image

Return type

str

modules.various.photo_utils.create_image(data: dict, bg_path: str, photo_path: str)

Creates the image with the data provided

Parameters
  • data (dict) – {‘title’: title of the image, ‘caption’: caption of the image, ‘template’: template to be used, ‘resize_mode’: how to resize the image, ‘background_offset’: offset used to crop the image}

  • bg_path (str) – path where to find the bg_image, if provided

  • photo_path (str) – path that will be used to save the image

modules.various.photo_utils.draw_text(draw_im: PIL.ImageDraw, w: int, text: str, y_text: float, font: any) → int

Draws the text on the image of width w, starting at height y_text

Parameters
  • draw_im (ImageDraw) – image to draw on

  • w (int) – with of the image

  • text (str) – text to write

  • y_text (int) – height of the text

  • font (any) – font of the text

Returns

final height of the text

Return type

int

modules.various.photo_utils.generate_photo(info: dict, user_data: dict, delete_message: bool = False)

Generates the image based on the user’s settings, then sends it The process can be executed on the main thread or on a separate thread, based on the settings

Parameters
  • info (dict) – {‘bot’: bot used to send the image, ‘chat_id’: id of the chat that will receive the image}

  • data (dict) – {‘title’: title of the image, ‘caption’: caption of the image, ‘template’: template to be used, ‘resize_mode’: how to resize the image, ‘background_offset’: offset used to crop the image}

  • message_id (bool, optional) – whther or not the previous message needs to be deleted. Defaults to False.

modules.various.photo_utils.resize_image(im: PIL.Image, fg: PIL.Image, resize_mode: str, offset: dict) → PIL.Image

Resizes the image with the method specified in resize_mode

Parameters
  • im (Image) – image to resize

  • fg (Image) – images wich dimensions will be used to resize the former image

  • resize_mode (str) – how to resize the image

  • offset (dict) – offset used to crop the image

Returns

newly resized image

Return type

Image

modules.various.photo_utils.send_image(info: dict, data: dict)

Creates and sends the requested image

Parameters
  • info (dict) – {‘bot’: bot used to send the image, ‘chat_id’: id of the chat that will receive the image}

  • data (dict) – {‘title’: title of the image, ‘caption’: caption of the image, ‘template’: template to be used, ‘resize_mode’: how to resize the image, ‘background_offset’: offset used to crop the image}

utils

Common operation for each command/callback

modules.various.utils.get_callback_info(update: telegram.update.Update, context: telegram.ext.callbackcontext.CallbackContext) → dict

Get the classic info from the update and context parameters for callbacks

Parameters
  • update (Update) – update event

  • context (CallbackContext) – context passed by the handler

Returns

{bot, chat_id, text, message_id, sender_first_name, sender_id, query_data}

Return type

dict

modules.various.utils.get_keyboard_crop() → telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup

Generates the InlineKeyboardMarkup for the crop callback

Returns

reply markup to apply at the message

Return type

InlineKeyboardMarkup

modules.various.utils.get_keyboard_random() → telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup

Generates the InlineKeyboardMarkup for the random callback

Returns

reply markup to apply at the message

Return type

InlineKeyboardMarkup

modules.various.utils.get_keyboard_setting(setting: str) → telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup

Generates the InlineKeyboardMarkup for the settings command

Returns

reply markup to apply at the message

Return type

InlineKeyboardMarkup

modules.various.utils.get_message_info(update: telegram.update.Update, context: telegram.ext.callbackcontext.CallbackContext) → dict

Get the classic info from the update and context parameters

Parameters
  • update (Update) – update event

  • context (CallbackContext) – context passed by the handler

Returns

{bot, chat_id, text, message_id, sender_first_name, sender_id}

Return type

dict