async def handler(event): if event.is_private: message = event.message.message argv = message.split(" ") if len(argv) != 1: return chat = None hash = None if "/joinchat/" in argv[0]: hash = argv[0].split("/")[-1] try: updates = await client(ImportChatInviteRequest(hash)) chat = updates.chats[0] chat_id = chat.id except Exception as e: updates = await client(CheckChatInviteRequest(hash)) chat_id = updates.chat.id chat = await get_dialog_by_id(chat_id) if "@" in argv[0]: chat_name = argv[0][1:] try: await client( functions.channels.JoinChannelRequest(channel=chat_name)) except Exception as e: pass chat = await get_dialog_by_name(chat_name) res = await get_members(chat) for mes in res: await event.reply(mes) time.sleep(1) if hash is not None: await client(functions.channels.LeaveChannelRequest(chat)) else: await client(functions.channels.LeaveChannelRequest(chat))
async def main(): for chat in chats: time.sleep(5) try: # try to join await client(ImportChatInviteRequest(chat)) time.sleep(5) except: print(chat + ' channel/group does not exist or other error. Skipping to next channel...') else: # if join successful, retrieve chat title chatinvite = await client(CheckChatInviteRequest(hash = chat)) try: chattitle = chatinvite.channel.title except: chattitle = chatinvite.chat.title print(chattitle + ' seems to be a chat...') time.sleep(10) print('Successfully joined ' + chattitle + '. Starting scraping now...') list = [] async for message in client.iter_messages(chattitle, reverse=True): list.append([chattitle, message.chat_id, message.is_private, message.is_group, message.is_channel, is_invite, wave, message.id, message.sender_id, message.date, message.text, message.action]) df = pd.DataFrame(list) df.columns = ['chat_title', 'chat_id', 'is_private', 'is_group', 'is_channel', 'is_invite', 'wave', 'message_id', 'message_senderid', 'message_date', 'message_text', 'message_action'] df['message_action'] = df['message_action'].astype(str) time.sleep(sleep)
async def get_group_from_link(self, link): link_id = link.split("/")[-1] chat_grp = None if "joinchat" in link: chat_grp = await self.client(CheckChatInviteRequest(hash=link_id)) else: chat_grp = await self.client( ResolveUsernameRequest(username=link_id)) chat_grp = chat_grp.chats[0] return chat_grp
async def find_chat_by_invite(request): link = request.json['link'] hash = link.replace('https://t.me/joinchat/', '') client = request.ctx.tg_client try: info = await client(ImportChatInviteRequest(hash=hash)) chat = info.chats[0] except Exception: info = await client(CheckChatInviteRequest(hash=hash)) chat = info.chat await leave_chat(chat.id, client) return json({"info": ttjson(chat)})
async def _join(evt: CommandEvent, arg: str) -> Tuple[Optional[TypeUpdates], Optional[EventID]]: if arg.startswith("joinchat/"): invite_hash = arg[len("joinchat/"):] try: await evt.sender.client(CheckChatInviteRequest(invite_hash)) except InviteHashInvalidError: return None, await evt.reply("Invalid invite link.") except InviteHashExpiredError: return None, await evt.reply("Invite link expired.") try: return (await evt.sender.client(ImportChatInviteRequest(invite_hash))), None except UserAlreadyParticipantError: return None, await evt.reply("You are already in that chat.") else: channel = await evt.sender.client.get_entity(arg) if not channel: return None, await evt.reply("Channel/supergroup not found.") return await evt.sender.client(JoinChannelRequest(channel)), None
async def _join(evt: CommandEvent, identifier: str, link_type: str ) -> Tuple[Optional[TypeUpdates], Optional[EventID]]: if link_type == "joinchat": try: await evt.sender.client(CheckChatInviteRequest(identifier)) except InviteHashInvalidError: return None, await evt.reply("Invalid invite link.") except InviteHashExpiredError: return None, await evt.reply("Invite link expired.") try: return (await evt.sender.client(ImportChatInviteRequest(identifier))), None except UserAlreadyParticipantError: return None, await evt.reply("You are already in that chat.") else: channel = await evt.sender.client.get_entity(identifier) if not channel: return None, await evt.reply("Channel/supergroup not found.") return await evt.sender.client(JoinChannelRequest(channel)), None
async def get_link_stat(request): link = request.json['link'] hash = link.replace('https://t.me/joinchat/', '') client = request.ctx.tg_client info = await client(CheckChatInviteRequest(hash=hash)) chat = info.chat tomorrow = datetime.date.today() + datetime.timedelta(days=1) try: info = await client( GetChatInviteImportersRequest(peer=chat, link=link, offset_date=tomorrow, offset_user=InputUserEmpty(), limit=100)) except Exception: info = False return json({"info": ttjson(info) if info else False})
async def resolve_invit_hash(invit_hash, expired_secends=60 * 5): ''' 解析邀请链接 https://t.me/+G-w4Ovfzp9U4YTFl 默认缓存5min Args: invite_hash (str): e.g. G-w4Ovfzp9U4YTFl expired_secends (int): None: not cache , 60: 1min Returns: Tuple | None: (marked_id,chat_title) ''' if not invit_hash: return None marked_id = '' chat_title = '' cache_key = f'01211resolve_invit_hash{invit_hash}' find = await cache_get(cache_key) if find: logger.info(f'resolve_invit_hash HIT CACHE: {invit_hash}') return find logger.info(f'resolve_invit_hash MISS: {invit_hash}') chatinvite = await client(CheckChatInviteRequest(invit_hash)) if chatinvite and hasattr(chatinvite, 'chat'): # 已加入 # chatinvite.chat.id # 1695903641 # chatinvite.chat.title # '测试' marked_id = telethon_utils.get_peer_id(PeerChannel( chatinvite.chat.id)) # 转换为marked_id chat_title = chatinvite.chat.title channel_entity = chatinvite.chat rel = (marked_id, chat_title, channel_entity) await cache_set(cache_key, rel, expired_secends) # cache.set(cache_key,rel,expired_secends) return rel return None
def check_and_join(client, group_link): try: hash = group_link[22:] check = client(CheckChatInviteRequest(hash)) if isinstance(check, ChatInvite): join = client(ImportChatInviteRequest(hash=hash)) print(client.get_me().phone, "not join", join.chats[0].id) if len(str(join.chats[0].id)) == 10: group_title = join.chats[0].title group_id = "-100" + str(join.chats[0].id) return {"group_id": group_id, "group_title": group_title} else: group_title = join.chats[0].title group_id = "-" + str(join.chats[0].id) return {"group_id": group_id, "group_title": group_title} elif isinstance(check, ChatInviteAlready): print(client.get_me().phone, "joined", check.chat.id) if len(str(check.chat.id)) == 10: group_title = check.chat.title group_id = "-100" + str(check.chat.id) return {"group_id": group_id, "group_title": group_title} else: group_title = check.chat.title group_id = "-" + str(check.chat.id) return {"group_id": group_id, "group_title": group_title} except: check = client(JoinChannelRequest(channel=group_link)) print(client.get_me().phone, "join channel", check.chats[0].id) if len(str(check.chats[0].id)) == 10: group_title = check.chats[0].title group_id = "-100" + str(check.chats[0].id) return {"group_id": group_id, "group_title": group_title} else: group_title = check.chats[0].title group_id = "-" + str(check.chats[0].id) return {"group_id": group_id, "group_title": group_title}
async def main(loop): """ The main telegram-export program. Goes through the configured dialogs and dumps them into the database. """ args = parse_args() config = load_config(args.config_file) dumper = Dumper(config['Dumper']) if args.contexts: dumper.config['Whitelist'] = args.contexts if args.format: formatter = NAME_TO_FORMATTER[args.format](dumper.conn) fmt_contexts = args.format_contexts or formatter.iter_context_ids() for cid in fmt_contexts: formatter.format(cid, config['Dumper']['OutputDirectory']) return proxy = args.proxy_string or dumper.config.get('Proxy') if proxy: proxy = parse_proxy_str(proxy) absolute_session_name = os.path.join(config['Dumper']['OutputDirectory'], config['TelegramAPI']['SessionName']) if config.has_option('TelegramAPI', 'SecondFactorPassword'): client = await (TelegramClient( absolute_session_name, config['TelegramAPI']['ApiId'], config['TelegramAPI']['ApiHash'], loop=loop, proxy=proxy).start( config['TelegramAPI']['PhoneNumber'], password=config['TelegramAPI']['SecondFactorPassword'])) else: client = await (TelegramClient( absolute_session_name, config['TelegramAPI']['ApiId'], config['TelegramAPI']['ApiHash'], loop=loop, proxy=proxy).start(config['TelegramAPI']['PhoneNumber'])) if args.list_dialogs or args.search_string: return await list_or_search_dialogs(args, client) if args.hash: dumper.config['Whitelist'] = str( (await client(CheckChatInviteRequest(args.hash))).chat.id) exporter = Exporter(client, config, dumper, loop) try: if args.download_past_media: await exporter.download_past_media() else: await exporter.start() except asyncio.CancelledError: # This should be triggered on KeyboardInterrupt's to prevent ugly # traceback from reaching the user. Important code that always # must run (such as the Downloader saving resume info) should go # in their respective `finally:` blocks to ensure it gets called. pass finally: await exporter.close() exporter.logger.info("Finished!")
spawn_read_thread=False) client.connect() client.start(phone) forward_ids = [] download_ids = [] for channel_ids, section in ((forward_ids, 'forwarder'), (download_ids, 'downloader')): for username in config.get(section, 'channels', fallback='').split(','): if username: channel, is_private = utils.parse_username(username) if is_private: try: invite = client(CheckChatInviteRequest(channel)) if isinstance(invite, ChatInviteAlready): channel_ids.append(invite.chat.id) elif isinstance(invite, ChatInvite): updates = client(ImportChatInviteRequest(channel)) channel_ids.append(updates.chats[0].id) except InviteHashInvalidError: txt = 'Invite link "{}" is not valid.' logging.warning(txt.format(username.strip())) else: try: channel = client.get_input_entity(channel)
async def join_link(url, client): channel_id = db_stats.get_channel(url, None) invite_code_typle = get_invite_string_from_url(url) if invite_code_typle is None: return None invite_code = invite_code_typle[0] is_public = invite_code_typle[1] logging.info('Joining with invite code %s', invite_code) try: if channel_id is not None: upserted_channel = db_stats.upsert_channel(channel_id[0], url, None) logging.info('Updated channel (id: %s) with code %s', channel_id[0], invite_code) if upserted_channel is not None and upserted_channel[7] == 1: logging.info('Skip joining, already analyzed channel, id: %s', channel_id[0]) return None if is_public: result = await client(JoinChannelRequest(invite_code)) else: result = await client(CheckChatInviteRequest(hash=invite_code)) chat = None if hasattr(result, 'chat'): chat = result.chat elif hasattr(result, 'chats'): chat = result.chats[0] if channel_id is not None: if not hasattr(result, 'title'): result = chat if result is not None: logging.info('Already in channel %s', result.title) got_history = db_stats.is_history_loaded( channel_id[0], url, result.title) if got_history: logging.info( 'History has already been loaded for channel %s', result.title) return None if chat is None: try: chat = await client(ImportChatInviteRequest(invite_code)) except errors.FloodWaitError as ex_flood_wait: await asyncio.sleep(ex_flood_wait.seconds + 10) chat = await client(ImportChatInviteRequest(invite_code)) chat = chat.chats[0] logging.info('Just added to channel %s', chat.title) upsert_res = db_stats.upsert_channel(chat.id, url, chat.title) is_new = upsert_res is None or upsert_res[4] is None if is_new: return chat return None except Exception: logging.info('Cannot add via %s:', url) return None