Ejemplo n.º 1
0
async def handler_new_message(event):
    """NewMessage event handler.
    """
    # skip if Album
    if hasattr(event, 'grouped_id') and event.grouped_id is not None:
        return
    try:
        logger.debug(f'New message from {event.chat_id}:\n{event.message}')
        targets = CHANNEL_MAPPING.get(event.chat_id)
        if targets is None or len(targets) < 1:
            logger.warning(
                f'NewMessage. No target channel for {event.chat_id}')
            return
        if REMOVE_URLS:
            event.message = remove_url_from_message(event.message)
        sent = 0
        for chat in targets:
            mirror_message = None
            if isinstance(event.message.media, MessageMediaPoll):
                mirror_message = await client.send_message(
                    chat, file=InputMediaPoll(poll=event.message.media.poll))
            else:
                mirror_message = await client.send_message(chat, event.message)

            sent += 1
            if sent > LIMIT_TO_WAIT:
                sent = 0
                time.sleep(TIMEOUT_MIRRORING)

    except Exception as e:
        logger.error(e, exc_info=True)
Ejemplo n.º 2
0
async def handler_album(event):
    """Album event handler.
    """
    try:
        logger.debug(f'New Album from {event.chat_id}:\n{event}')
        targets = CHANNEL_MAPPING.get(event.chat_id)
        if targets is None or len(targets) < 1:
            logger.warning(f'Album. No target channel for {event.chat_id}')
            return
        # media
        files = []
        # captions
        caps = []
        for item in event.messages:
            if REMOVE_URLS:
                item = remove_url_from_message(item)
            files.append(item.media)
            caps.append(item.message)
        sent = 0
        for chat in targets:
            mirror_messages = await client.send_file(chat,
                                                     caption=caps,
                                                     file=files)
            sent += 1
            if sent > LIMIT_TO_WAIT:
                sent = 0
                time.sleep(TIMEOUT_MIRRORING)
    except Exception as e:
        logger.error(e, exc_info=True)
Ejemplo n.º 3
0
async def handler_album(event):
    """Album event handler.
    """
    try:
        logger.debug(f'New Album from {event.chat_id}:\n{event}')
        targets = CHANNEL_MAPPING.get(event.chat_id)
        if targets is None or len(targets) < 1:
            logger.warning(f'Album. No target channel for {event.chat_id}')
            return
        # media
        files = []
        # captions
        caps = []
        # original messages ids
        original_idxs = []
        for item in event.messages:
            files.append(item.media)
            caps.append(item.message)
            original_idxs.append(item.id)
        sent = 0
        for chat in targets:
            mirror_messages = await client.send_file(chat,
                                                     caption=caps,
                                                     file=files)
            if mirror_messages is not None and len(mirror_messages) > 1:
                for idx, m in enumerate(mirror_messages):
                    db.insert(
                        MirrorMessage(original_id=original_idxs[idx],
                                      original_channel=event.chat_id,
                                      mirror_id=m.id,
                                      mirror_channel=chat))
            sent += 1
            if sent > LIMIT_TO_WAIT:
                sent = 0
                time.sleep(TIMEOUT_MIRRORING)
    except Exception as e:
        logger.error(e, exc_info=True)