示例#1
0
def message_answer_handler(update: Update, context: CallbackContext):
    callback_query = update.callback_query
    callback_data = json.loads(callback_query.data)

    if not callback_data:
        callback_query.answer()

        return

    original_attachment_file_id = callback_data[ATTACHMENT_FILE_ID_KEY]

    message = update.effective_message
    chat_type = update.effective_chat.type
    bot = context.bot

    message_id = message.message_id
    chat_id = message.chat.id

    user = update.effective_user

    create_or_update_user(bot, user)

    analytics.track(AnalyticsType.MESSAGE, user)

    if chat_type == Chat.PRIVATE:
        bot.send_chat_action(chat_id, ChatAction.TYPING)

    input_file = bot.get_file(original_attachment_file_id)
    input_file_url = input_file.file_path

    probe = None

    try:
        probe = ffmpeg.probe(input_file_url)
    except:
        pass

    with io.BytesIO() as output_bytes:
        output_type = OutputType.NONE

        invalid_format = None

        if probe:
            for stream in probe['streams']:
                codec_name = stream.get('codec_name')
                codec_type = stream.get('codec_type')

                if codec_name is not None and codec_type == VIDEO_CODED_TYPE:
                    invalid_format = codec_name

                if codec_name in VIDEO_CODEC_NAMES:
                    output_type = OutputType.VIDEO_NOTE

                    mp4_bytes = convert(output_type,
                                        input_video_url=input_file_url)

                    output_bytes.write(mp4_bytes)

                    break
                else:
                    continue

        if output_type == OutputType.NONE:
            if chat_type == Chat.PRIVATE:
                if invalid_format is None:
                    invalid_format = os.path.splitext(input_file_url)[1][1:]

                bot.send_message(chat_id,
                                 'File type "{}" is not yet supported.'.format(
                                     invalid_format),
                                 reply_to_message_id=message_id)

            callback_query.answer()

            return

        output_bytes.seek(0)

        output_file_size = output_bytes.getbuffer().nbytes

        if output_type == OutputType.VIDEO_NOTE:
            if not ensure_size_under_limit(
                    output_file_size,
                    MAX_FILESIZE_UPLOAD,
                    update,
                    context,
                    file_reference_text='Converted file'):
                callback_query.answer()

                return

            bot.send_chat_action(chat_id, ChatAction.UPLOAD_VIDEO)

            send_video_note(bot, chat_id, message_id, output_bytes)

            callback_query.answer()

            return

    if chat_type == Chat.PRIVATE:
        bot.send_message(chat_id,
                         'File type is not yet supported.',
                         reply_to_message_id=message_id)

    callback_query.answer()
示例#2
0
def message_answer_handler(update: telegram.Update,
                           context: telegram.ext.CallbackContext) -> None:
    callback_query = update.callback_query

    if callback_query is None:
        return

    raw_callback_data = callback_query.data

    if raw_callback_data is None:
        callback_query.answer()

        return

    callback_data = json.loads(raw_callback_data)

    if callback_data is None:
        callback_query.answer()

        return

    message = update.effective_message

    if message is None:
        return

    chat = update.effective_chat

    if chat is None:
        return

    chat_type = chat.type
    bot = context.bot

    attachment = message.effective_attachment

    if attachment is None:
        return

    if not isinstance(attachment, telegram.Video):
        return

    file_size = attachment.file_size

    if file_size is not None and not utils.ensure_size_under_limit(
            file_size, telegram.constants.MAX_FILESIZE_DOWNLOAD, update,
            context):
        return

    attachment_file_id = attachment.file_id

    message_id = message.message_id
    chat_id = message.chat.id

    user = update.effective_user

    if user is not None:
        create_or_update_user(bot, user)

        analytics_handler.track(context, analytics.AnalyticsType.MESSAGE, user)

    if chat_type == telegram.Chat.PRIVATE:
        bot.send_chat_action(chat_id, telegram.ChatAction.TYPING)

    input_file = bot.get_file(attachment_file_id)
    input_file_url = input_file.file_path

    probe = None

    try:
        probe = ffmpeg.probe(input_file_url)
    except ffmpeg.Error:
        pass

    with io.BytesIO() as output_bytes:
        output_type = constants.OutputType.NONE

        invalid_format = None

        if probe:
            for stream in probe['streams']:
                codec_name = stream.get('codec_name')

                if codec_name is not None:
                    invalid_format = codec_name

                if codec_name in constants.VIDEO_CODEC_NAMES:
                    output_type = constants.OutputType.VIDEO_NOTE

                    mp4_bytes = utils.convert(output_type,
                                              input_video_url=input_file_url)

                    if not utils.ensure_valid_converted_file(
                            file_bytes=mp4_bytes, update=update,
                            context=context):
                        callback_query.answer()

                        return

                    if mp4_bytes is not None:
                        output_bytes.write(mp4_bytes)

                    break

                continue

        if output_type == constants.OutputType.NONE:
            if chat_type == telegram.Chat.PRIVATE:
                if invalid_format is None and input_file_url is not None:
                    parts = os.path.splitext(input_file_url)

                    if parts is not None and len(parts) >= 2:
                        extension = parts[1]

                        if extension is not None:
                            invalid_format = extension[1:]

                bot.send_message(
                    chat_id=chat_id,
                    text=f'File type "{invalid_format}" is not yet supported.',
                    reply_to_message_id=message_id)

            callback_query.answer()

            return

        output_bytes.seek(0)

        output_file_size = output_bytes.getbuffer().nbytes

        if output_type == constants.OutputType.VIDEO_NOTE:
            if not utils.ensure_size_under_limit(
                    output_file_size,
                    telegram.constants.MAX_FILESIZE_UPLOAD,
                    update,
                    context,
                    file_reference_text='Converted file'):
                callback_query.answer()

                return

            bot.send_chat_action(chat_id, telegram.ChatAction.UPLOAD_VIDEO)

            utils.send_video_note(bot, chat_id, message_id, output_bytes)

            callback_query.answer()

            return

    if chat_type == telegram.Chat.PRIVATE:
        bot.send_message(chat_id,
                         'File type is not yet supported.',
                         reply_to_message_id=message_id)

    callback_query.answer()
示例#3
0
def message_video_handler(update: Update, context: CallbackContext):
    message = update.effective_message
    chat_type = update.effective_chat.type
    bot = context.bot

    if chat_type != Chat.PRIVATE:
        return

    if cli_args.debug and not check_admin(bot, message, analytics,
                                          ADMIN_USER_ID):
        return

    message_id = message.message_id
    chat_id = message.chat.id
    attachment = message.video

    if not ensure_size_under_limit(attachment.file_size, MAX_FILESIZE_DOWNLOAD,
                                   update, context):
        return

    user = update.effective_user

    input_file_id = attachment.file_id

    create_or_update_user(bot, user)

    analytics.track(AnalyticsType.MESSAGE, user)

    bot.send_chat_action(chat_id, ChatAction.TYPING)

    input_file = bot.get_file(input_file_id)
    input_file_url = input_file.file_path

    probe = None

    try:
        probe = ffmpeg.probe(input_file_url)
    except:
        pass

    with io.BytesIO() as output_bytes:
        output_type = OutputType.NONE

        invalid_format = None

        if probe:
            for stream in probe['streams']:
                codec_name = stream.get('codec_name')
                codec_type = stream.get('codec_type')

                if codec_name is not None and codec_type == VIDEO_CODED_TYPE:
                    invalid_format = codec_name

                if codec_name in VIDEO_CODEC_NAMES:
                    output_type = OutputType.VIDEO_NOTE

                    mp4_bytes = convert(output_type,
                                        input_video_url=input_file_url)

                    output_bytes.write(mp4_bytes)

                    break
                else:
                    continue

        if output_type == OutputType.NONE:
            if invalid_format is None:
                invalid_format = os.path.splitext(input_file_url)[1][1:]

            bot.send_message(
                chat_id,
                'File type "{}" is not yet supported.'.format(invalid_format),
                reply_to_message_id=message_id)

        output_bytes.seek(0)

        output_file_size = output_bytes.getbuffer().nbytes

        if output_type == OutputType.VIDEO_NOTE:
            if not ensure_size_under_limit(
                    output_file_size,
                    MAX_FILESIZE_UPLOAD,
                    update,
                    context,
                    file_reference_text='Converted file'):
                return

            bot.send_chat_action(chat_id, ChatAction.UPLOAD_VIDEO)

            send_video_note(bot, chat_id, message_id, output_bytes)

            return

    bot.send_message(chat_id,
                     'File type is not yet supported.',
                     reply_to_message_id=message_id)