コード例 #1
0
def image_random_callback(update: Update, context: CallbackContext) -> int:
    """Handles the image random callback
    Makes yhe user try the generation again
    The conversation remains in the "random" state or is put in the "end" state

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler

    Returns:
        int: new state of the conversation
    """
    info = get_callback_info(update, context)

    operation = info["query_data"][13:]

    if operation == 'finish':
        sender_id = info['sender_id']

        info['bot'].edit_message_reply_markup(chat_id=info['chat_id'], message_id=info['message_id'], reply_markup=None)

        if os.path.exists(build_bg_path(sender_id)):
            os.remove(build_bg_path(sender_id))
        os.remove(build_photo_path(sender_id))

        return STATE['end']

    generate_photo(info=info, user_data=context.user_data, delete_message=True)

    return STATE['random']
コード例 #2
0
def background_msg(update: Update, context: CallbackContext) -> int:
    """Handles the background message
    Saves the photo so it can be used as the background of the image

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler

    Returns:
        int: new state of the conversation
    """
    info = get_message_info(update, context)
    text = read_md("background")
    photo = update.message.photo
    resize_mode = context.user_data['resize_mode']

    if photo:  # if an actual photo was sent
        bg_image = info['bot'].getFile(photo[-1].file_id)
        bg_image.download(build_bg_path(info['sender_id']))

    info['bot'].send_message(chat_id=info['chat_id'],
                             text=text,
                             parse_mode=ParseMode.MARKDOWN_V2)

    generate_photo(info, context.user_data)

    if resize_mode == "crop":
        return STATE['crop']
    elif resize_mode == "random":
        return STATE['random']
    else:
        return STATE['end']
コード例 #3
0
def cancel_cmd(update: Update, context: CallbackContext) -> int:
    """Handles the /cancel command
    Cancels the current cretion of the image
    Puts the conversation in the "end" state

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler

    Returns:
        int: new state of the conversation
    """
    info = get_message_info(update, context)
    text = read_md("cancel")

    # Clear the disk space used by the images, if present
    bg_path = build_bg_path(info['sender_id'])
    photo_path = build_photo_path(info['sender_id'])
    if os.path.exists(bg_path):
        os.remove(bg_path)
    if os.path.exists(photo_path):
        os.remove(photo_path)

    info['bot'].send_message(chat_id=info['chat_id'],
                             text=text,
                             parse_mode=ParseMode.MARKDOWN_V2)
    return STATE['end']
コード例 #4
0
def image_crop_callback(update: Update, context: CallbackContext) -> int:
    """Handles the image crop callback
    Modifies the cropping parameters
    The conversation remains in the "crop" state or is put in the "end" state

    Args:
        update (Update): update event
        context (CallbackContext): context passed by the handler

    Returns:
        int: new state of the conversation
    """
    info = get_callback_info(update, context)

    operation = info["query_data"][11:]

    if operation == 'reset':
        context.user_data['background_offset'] = {'x': 0, 'y': 0}
    elif operation == 'finish':
        sender_id = info['sender_id']

        info['bot'].edit_message_reply_markup(chat_id=info['chat_id'], message_id=info['message_id'], reply_markup=None)

        if os.path.exists(build_bg_path(sender_id)):
            os.remove(build_bg_path(sender_id))
        os.remove(build_photo_path(sender_id))

        return STATE['end']
    else:
        offset_value = OFFSET_VALUES[operation]

        context.user_data['background_offset'] = {
            'x': context.user_data['background_offset']['x'] + offset_value['x'],
            'y': context.user_data['background_offset']['y'] + offset_value['y']
        }

    generate_photo(info=info, user_data=context.user_data, delete_message=True)

    return STATE['crop']