def start(client: TelegramClient): # client.start() #bot_token=config.get('telegram', 'bot_token') client.connect() # me = client.get_me() # print(me.stringify()) for dialog in client.iter_dialogs(): if not dialog.is_channel: continue if isinstance(dialog.entity.photo, types.ChatPhotoEmpty): print(dialog.entity.id, dialog.entity.title) else: fn = os.path.join( config.get('paths', 'avatars'), 'channel-' + str(dialog.entity.id) + '.jpg' ) # if not os.path.exists(fn): # fn = client.download_profile_photo(dialog.entity, fn, download_big=False) print(dialog.entity.id, dialog.entity.title, '🖕', fn) msg = dialog.message.message if msg: print(' ', msg[:75] + '...' if len(msg) > 78 else msg) # channel = client.get_entity('https://t.me/huytest') # print(channel.stringify()) # print(client.get_messages(channel, 5)) client.add_event_handler(print_message, events.NewMessage(chats=1332837514, incoming=True)) client.catch_up() print('event handlers:', client.list_event_handlers())
class BaseTelegramClient(AioThing): def __init__( self, app_id: Union[int, str], app_hash: str, database: dict, bot_token: Optional[str] = None, mtproxy: Optional[dict] = None, flood_sleep_threshold: int = 60, ): super().__init__() if not app_id or not app_hash: raise ValueError( 'Your API ID or Hash cannot be empty or None. Set up telegram.app_id and/or telegram.app_hash' ) self._telegram_client = TelegramClient( self._get_session(database), app_id, app_hash, flood_sleep_threshold=flood_sleep_threshold, **self._get_proxy(mtproxy=mtproxy), ) self.bot_token = bot_token def _get_session(self, database): if database.get('drivername') == 'postgresql': self.container = AlchemySessionContainer( f"{database['drivername']}://" f"{database['username']}:" f"{database['password']}@" f"{database['host']}:" f"{database['port']}/" f"{database['database']}", session=False, manage_tables=False, ) return self.container.new_session(database['session_id']) else: return sessions.SQLiteSession(session_id=database['session_id']) def _get_proxy(self, mtproxy=None): if mtproxy and mtproxy.get('enabled', True): proxy_config = mtproxy return { 'connection': connection.tcpmtproxy. ConnectionTcpMTProxyRandomizedIntermediate, 'proxy': (proxy_config['url'], proxy_config['port'], proxy_config['secret']) } return {} @retry(retry=retry_if_exception_type(ConnectionError), wait=wait_fixed(5)) async def start(self): await self._telegram_client.start(bot_token=self.bot_token) async def stop(self): return await self.disconnect() def add_event_handler(self, *args, **kwargs): return self._telegram_client.add_event_handler(*args, **kwargs) def catch_up(self): return self._telegram_client.catch_up() def delete_messages(self, *args, **kwargs): return self._telegram_client.delete_messages(*args, **kwargs) def disconnect(self): return self._telegram_client.disconnect() @property def disconnected(self): return self._telegram_client.disconnected def download_document(self, *args, **kwargs): return self._telegram_client._download_document( *args, date=datetime.datetime.now(), thumb=None, progress_callback=None, msg_data=None, **kwargs, ) def edit_message(self, *args, **kwargs): return self._telegram_client.edit_message(*args, **kwargs) def edit_permissions(self, *args, **kwargs): return self._telegram_client.edit_permissions(*args, **kwargs) def forward_messages(self, *args, **kwargs): return self._telegram_client.forward_messages(*args, **kwargs) def get_entity(self, *args, **kwargs): return self._telegram_client.get_entity(*args, **kwargs) def get_input_entity(self, *args, **kwargs): return self._telegram_client.get_input_entity(*args, **kwargs) def iter_admin_log(self, *args, **kwargs): return self._telegram_client.iter_admin_log(*args, **kwargs) def iter_messages(self, *args, **kwargs): return self._telegram_client.iter_messages(*args, **kwargs) def list_event_handlers(self): return self._telegram_client.list_event_handlers() def remove_event_handlers(self): for handler in reversed(self.list_event_handlers()): self._telegram_client.remove_event_handler(*handler) def run_until_disconnected(self): return self._telegram_client.run_until_disconnected() def send_message(self, *args, **kwargs): return self._telegram_client.send_message(*args, **kwargs) def send_file(self, *args, **kwargs): return self._telegram_client.send_file(*args, **kwargs) def __call__(self, *args, **kwargs): return self._telegram_client(*args, **kwargs)