return if event.message.media: stats.increase("media_events") logging.info("Media event") await client.send_file(config.my_channel_id, file=event.message, caption=event.message.message) return if event.message.text: logging.info("Text event") stats.increase("text_events") await client.send_message(config.my_channel_id, event.message, link_preview=False) return logging.info("Unhandled message type") @client.on(events.Album(chats=config.channels)) async def album_handler(event): stats.increase("album_events") logging.info("Album event") await client.send_message( config.my_channel_id, file=event.messages, message=event.original_update.message.message, ) client.start() client.run_until_disconnected() # async def main(): # # Now you can use all client methods listed below, like for example...
with TelegramClient("forward_bot", api_id, api_hash) as client: @client.on( events.NewMessage(incoming=True, func=lambda e: e.is_channel and e.message and e. message.grouped_id is None)) async def channel_handler(event): logger.info("channel_handler") try: await event.message.forward_to(target_username) except Exception as error: logger.error(event.to_json()) raise error @client.on(events.Album(func=lambda e: e.is_channel)) async def channel_album_handler(event): logger.info("channel_album_handler") try: await event.forward_to(target_username) except Exception as error: logger.error(event.to_json()) raise error @client.on( events.NewMessage(chats=[target_username], incoming=True, func=get_forwarded_channel_id)) async def add_subscription_handler(event): logger.info("add_subscription_handler") channel_id = get_forwarded_channel_id(event)
from database import Database, MirrorMessage from settings import (API_HASH, API_ID, CHANNEL_MAPPING, CHATS, DB_URL, LIMIT_TO_WAIT, LOG_LEVEL, REMOVE_URLS, SESSION_STRING, TIMEOUT_MIRRORING) from utils import remove_urls logging.basicConfig() logger = logging.getLogger(__name__) logger.setLevel(level=LOG_LEVEL) client = TelegramClient(StringSession(SESSION_STRING), API_ID, API_HASH) db = Database(DB_URL) @client.on(events.Album(chats=CHATS)) 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 = []
handlers = [console, debug_file, info_file] [x.setFormatter(formatter) for x in handlers] [logger.addHandler(x) for x in handlers] # OBJECTS SETUP bot = TelegramClient( CONFIG['BOT_SESSION'], CONFIG['TG_API_ID'], CONFIG['TG_API_HASH']).start(bot_token=CONFIG['BOT_TOKEN']) boards = trello.TrelloAPI() # HANDLERS SETUP @bot.on(events.Album(chats=CONFIG['CHATS'])) async def album_process(event): msg_caption = None for message in event.messages: if not util.check_tags(message): continue else: msg_caption = message break if not msg_caption: return response = await boards.new_card(**util.extract_card_info(msg_caption))