def video_from_telegram(bot: telegram.Bot, update: telegram.Update):
    """
    if update.message:
        message: telegram.Message = update.message
        edited = False
    else:
        message: telegram.Message = update.edited_message
        edited = True
    """

    message: telegram.Message = update.effective_message
    edited = (bool(getattr(update, "edited_message", None))
              or bool(getattr(update, "edited_channel_post", None)))

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    if edited:
        recall_message(forward_index, message)

    # don't forward this message
    if (message.caption and message.caption.startswith('//')) or (
            message.reply_to_message and message.reply_to_message.caption
            and message.reply_to_message.caption.startswith('//')) or (
                message.reply_to_message and message.reply_to_message.text
                and message.reply_to_message.text.startswith('//')):
        logger.debug('Message ignored: matched comment pattern')
        raise DispatcherHandlerStop()

    reply_entity = list()

    reply_entity.append({'type': 'text', 'data': {'text': '[ 视频 ]'}})
    if message.caption:
        reply_entity.append({
            'type': 'text',
            'data': {
                'text': message.caption
            }
        })

    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=update.effective_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message,
                                       edited=edited)
    global_vars.mdb.append_message(qq_message_id, message.message_id,
                                   forward_index, 0)
示例#2
0
def tg_drive_mode(bot: telegram.Bot, update: telegram.Update):
    """
    if update.message:
        message: telegram.Message = update.message
    else:
        message: telegram.Message = update.edited_message
    """
    message: telegram.Message = update.effective_message
    edited = (bool(getattr(update, "edited_message", None))
              or bool(getattr(update, "edited_channel_post", None)))

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=int(tg_group_id))

    if edited:
        recall_message(forward_index, message)

    # don't forward this message
    if (message.caption and message.caption.startswith('//')
        or message.text and message.text.startswith('//')) \
            or (message.reply_to_message
                and message.reply_to_message.caption
                and message.reply_to_message.caption.startswith('//')) \
            or (message.reply_to_message
                and message.reply_to_message.text
                and message.reply_to_message.text.startswith('//')):
        logger.debug('Message ignored: matched comment pattern')
        raise DispatcherHandlerStop()

    # prevent message leak
    if forward_index == -1:
        raise DispatcherHandlerStop()

    if global_vars.DRIVE_MODE[forward_index]:  # normal block
        logger.debug('Telegram message ignored: drive mode is on')
        raise DispatcherHandlerStop()
def recall(tg_group_id: int, tg_user: telegram.User, tg_message_id: int,
           tg_reply_to: telegram.Message):
    forward_index = get_forward_index(tg_group_id=tg_group_id)
    if forward_index == -1:
        return

    result = recall_message(forward_index, tg_reply_to)

    if result == -1:
        text = 'Please refer to a message.'
    elif result == -2:
        text = 'Message not recallable.'
    elif result == -3:
        text = 'Recalling messages from other QQ users is not supported.',
    elif result == -4:
        text = 'Message sent more than two minutes ago. Recalling failed.'
    else:
        text = 'Message recalled.'

    global_vars.tg_bot.sendMessage(chat_id=tg_group_id,
                                   text=text,
                                   reply_to_message_id=tg_message_id)
def document_from_telegram(bot: telegram.Bot, update: telegram.Update):
    """
    if update.message:
        message: telegram.Message = update.message
        edited = False
    else:
        message: telegram.Message = update.edited_message
        edited = True
    """

    message: telegram.Message = update.effective_message
    edited = (bool(getattr(update, "edited_message", None))
              or bool(getattr(update, "edited_channel_post", None)))

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    if edited:
        recall_message(forward_index, message)

    # don't forward this message
    if (message.caption and message.caption.startswith('//')) or (
            message.reply_to_message and message.reply_to_message.caption
            and message.reply_to_message.caption.startswith('//')) or (
                message.reply_to_message and message.reply_to_message.text
                and message.reply_to_message.text.startswith('//')):
        logger.debug('Message ignored: matched comment pattern')
        raise DispatcherHandlerStop()

    reply_entity = list()

    if message.document.mime_type == 'video/mp4':
        file_id = message.document.file_id
        if global_vars.JQ_MODE:
            tg_get_pic_url(file_id, 'gif', False)
            if os.path.getsize(os.path.join(CQ_IMAGE_ROOT,
                                            file_id + '.gif')) > 5242880:
                reply_entity.append({
                    'type': 'text',
                    'data': {
                        'text': '[ 视频:大小超过 5 MB ]'
                    }
                })
            else:
                reply_entity.append({
                    'type': 'image',
                    'data': {
                        'file': file_id + '.gif'
                    }
                })
            if message.caption:
                reply_entity.append({
                    'type': 'text',
                    'data': {
                        'text': message.caption
                    }
                })
        elif IMAGE_LINK_MODE[forward_index]:
            pic_url = tg_get_pic_url(file_id, 'gif', True)
            if message.caption:
                reply_entity.append({
                    'type': 'text',
                    'data': {
                        'text':
                        '[ 视频, 请点击查看' + pic_url + ' ]' + message.caption
                    }
                })
            else:
                reply_entity.append({
                    'type': 'text',
                    'data': {
                        'text': '[ 视频, 请点击查看' + pic_url + ' ]'
                    }
                })
        else:
            reply_entity.append({'type': 'text', 'data': {'text': '[ 视频 ]'}})
    elif message.document.mime_type == 'image/gif':
        file_id = message.document.file_id
        if global_vars.JQ_MODE:
            file = global_vars.tg_bot.getFile(file_id)
            file.download(custom_path=os.path.join(CQ_IMAGE_ROOT, file_id +
                                                   '.gif'))
            reply_entity.append({
                'type': 'image',
                'data': {
                    'file': file_id + '.gif'
                }
            })
            if message.caption:
                reply_entity.append({
                    'type': 'text',
                    'data': {
                        'text': message.caption
                    }
                })
        elif IMAGE_LINK_MODE[forward_index]:
            file = global_vars.tg_bot.getFile(file_id)
            file.download(custom_path=os.path.join(CQ_IMAGE_ROOT, file_id +
                                                   '.gif'))
            pic_url = get_short_url(SERVER_PIC_URL + file_id + '.gif')
            if message.caption:
                reply_entity.append({
                    'type': 'text',
                    'data': {
                        'text':
                        '[ GIF, 请点击查看' + pic_url + ' ]' + message.caption
                    }
                })
            else:
                reply_entity.append({
                    'type': 'text',
                    'data': {
                        'text': '[ GIF, 请点击查看' + pic_url + ' ]'
                    }
                })
        else:
            reply_entity.append({'type': 'text', 'data': {'text': '[ GIF ]'}})
    else:
        reply_entity.append({'type': 'text', 'data': {'text': '[ 文件 ]'}})
    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=update.effective_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message,
                                       edited=edited)
    global_vars.mdb.append_message(qq_message_id, message.message_id,
                                   forward_index, 0)
def photo_from_telegram(bot: telegram.Bot, update: telegram.Update):
    """
    if update.message:
        message: telegram.Message = update.message
        edited = False
    else:
        message: telegram.Message = update.edited_message
        edited = True
    """

    message: telegram.Message = update.effective_message
    edited = (bool(getattr(update, "edited_message", None))
              or bool(getattr(update, "edited_channel_post", None)))

    tg_group_id = message.chat_id  # telegram group id
    forward_index = get_forward_index(tg_group_id=tg_group_id)

    if edited:
        recall_message(forward_index, message)

    # don't forward this message
    if (message.caption and message.caption.startswith('//')) or (
            message.reply_to_message and message.reply_to_message.caption
            and message.reply_to_message.caption.startswith('//')) or (
                message.reply_to_message and message.reply_to_message.text
                and message.reply_to_message.text.startswith('//')):
        logger.debug('Message ignored: matched comment pattern')
        raise DispatcherHandlerStop()

    reply_entity = list()

    file_id = message.photo[-1].file_id
    if global_vars.JQ_MODE:
        tg_get_pic_url(file_id, 'jpg', False)
        reply_entity.append({
            'type': 'image',
            'data': {
                'file': file_id + '.jpg'
            }
        })
        if message.caption:
            reply_entity.append({
                'type': 'text',
                'data': {
                    'text': message.caption
                }
            })
    elif IMAGE_LINK_MODE[forward_index]:
        pic_url = tg_get_pic_url(file_id, 'jpg', True)
        if message.caption:
            reply_entity.append({
                'type': 'text',
                'data': {
                    'text': '[ 图片, 请点击查看' + pic_url + ' ]' + message.caption
                }
            })
        else:
            reply_entity.append({
                'type': 'text',
                'data': {
                    'text': '[ 图片, 请点击查看' + pic_url + ' ]'
                }
            })
    else:
        reply_entity.append({
            'type': 'text',
            'data': {
                'text': '[ 图片 ]' + message.caption
            }
        })
    qq_message_id = send_from_tg_to_qq(forward_index,
                                       message=reply_entity,
                                       tg_group_id=tg_group_id,
                                       tg_user=update.effective_user,
                                       tg_forward_from=message,
                                       tg_reply_to=message.reply_to_message,
                                       edited=edited)
    global_vars.mdb.append_message(qq_message_id, message.message_id,
                                   forward_index, 0)