async def get_peer_id(self: 'TelegramClient', peer: 'hints.EntityLike', add_mark: bool = True) -> int: """ Gets the ID for the given entity. This method needs to be ``async`` because `peer` supports usernames, invite-links, phone numbers (from people in your contact list), etc. If ``add_mark is False``, then a positive ID will be returned instead. By default, bot-API style IDs (signed) are returned. Example .. code-block:: python print(await client.get_peer_id('me')) """ if isinstance(peer, int): return utils.get_peer_id(peer, add_mark=add_mark) try: if peer.SUBCLASS_OF_ID not in (0x2d45687, 0xc91c90b6): # 0x2d45687, 0xc91c90b6 == crc32(b'Peer') and b'InputPeer' peer = await self.get_input_entity(peer) except AttributeError: peer = await self.get_input_entity(peer) if isinstance(peer, types.InputPeerSelf): peer = await self.get_me(input_peer=True) return utils.get_peer_id(peer, add_mark=add_mark)
def get_entity_rows_by_id(self, entity_id, exact=True): if exact: key = "{}:entities:{}".format(self.sess_prefix, entity_id) s = self.redis_connection.get(key) if not s: return None try: s = self._unpack(s) return entity_id, s["hash"] except Exception as ex: __log__.exception(ex.args) return None else: ids = ( utils.get_peer_id(PeerUser(entity_id)), utils.get_peer_id(PeerChat(entity_id)), utils.get_peer_id(PeerChannel(entity_id)) ) try: for key in self._get_entities(): entity = self._unpack(self.redis_connection.get(key)) if "id" in entity and entity["id"] in ids: return entity["id"], entity["hash"] except Exception as ex: __log__.exception(ex.args)
def _dump_messages(self, messages, target): """ Helper method to iterate the messages from a GetMessageHistoryRequest and dump them into the Dumper, mostly to avoid excessive nesting. Also enqueues any media to be downloaded later by a different coroutine. """ for m in messages: if isinstance(m, types.Message): media_id = self.dumper.dump_media(m.media) if media_id and self._check_media(m.media): self.enqueue_media(media_id, utils.get_peer_id(target), m.from_id, m.date) self.dumper.dump_message(message=m, context_id=utils.get_peer_id(target), forward_id=self.dumper.dump_forward( m.fwd_from), media_id=media_id) elif isinstance(m, types.MessageService): if isinstance(m.action, types.MessageActionChatEditPhoto): media_id = self.dumper.dump_media(m.action.photo) self.enqueue_photo(m.action.photo, media_id, target, peer_id=m.from_id, date=m.date) else: media_id = None self.dumper.dump_message_service( message=m, context_id=utils.get_peer_id(target), media_id=media_id)
async def setup_bot(): """ To set up bot for userbot """ try: await catub.connect() config = await catub(functions.help.GetConfigRequest()) for option in config.dc_options: if option.ip_address == catub.session.server_address: if catub.session.dc_id != option.id: LOGS.warning( f"Fixed DC ID in session from {catub.session.dc_id}" f" to {option.id}") catub.session.set_dc(option.id, option.ip_address, option.port) catub.session.save() break bot_details = await catub.tgbot.get_me() Config.TG_BOT_USERNAME = f"@{bot_details.username}" # await catub.start(bot_token=Config.TG_BOT_USERNAME) catub.me = await catub.get_me() catub.uid = catub.tgbot.uid = utils.get_peer_id(catub.me) if Config.OWNER_ID == 0: Config.OWNER_ID = utils.get_peer_id(catub.me) except Exception as e: LOGS.error(f"STRING_SESSION - {str(e)}") sys.exit()
def get_entity_rows_by_id(self, id, exact=True): with switch_db(Entity, self.database) as _Entity: if exact: return _Entity.objects(id=id) else: ids = (utils.get_peer_id(PeerUser(id)), utils.get_peer_id(PeerChat(id)), utils.get_peer_id(PeerChannel(id))) return _Entity.objects(id__in=ids)
def download_profile_photo(self, photo, target, known_id=None): """ Similar to Downloader.download_media() but for profile photos. Has no effect if there is no photo format (thus it is "disabled"). """ if not self.photo_fmt: return date = datetime.datetime.now() if isinstance(photo, (types.UserProfilePhoto, types.ChatPhoto)): if isinstance(photo.photo_big, types.FileLocation): location = photo.photo_big elif isinstance(photo.photo_small, types.FileLocation): location = photo.photo_small else: return elif isinstance(photo, types.Photo): for size in photo.sizes: if isinstance(size, types.PhotoSize): if isinstance(size.location, types.FileLocation): location = size.location break else: return date = photo.date if known_id is None: known_id = photo.id else: return if known_id is None: known_id = utils.get_peer_id(target) formatter = defaultdict( str, id=known_id, context_id=utils.get_peer_id(target), sender_id=utils.get_peer_id(target), ext='.jpg', type='chatphoto', filename=date.strftime('chatphoto_%Y-%m-%d_%H-%M-%S'), name=utils.get_display_name(target) or 'unknown', sender_name=utils.get_display_name(target) or 'unknown') filename = date.strftime(self.photo_fmt).format_map(formatter) if not filename.endswith(formatter['ext']): if filename.endswith('.'): filename = filename[:-1] filename += formatter['ext'] os.makedirs(os.path.dirname(filename), exist_ok=True) return self.client.download_file(types.InputFileLocation( volume_id=location.volume_id, local_id=location.local_id, secret=location.secret), file=filename, part_size_kb=256)
def get_entity_rows_by_id(self, key, exact=True): if exact: row = models.Entity.objects.filter(id=key).first() else: ids = (utils.get_peer_id(PeerUser(key)), utils.get_peer_id(PeerChat(key)), utils.get_peer_id(PeerChannel(key))) row = models.Entity.objects.filter(id__in=ids).first() return (row.id, row.hash) if row else None
def get_entity_rows_by_id(self, id, exact=True): if exact: query = "SELECT * FROM c WHERE c.id = {}".format(id) else: query = "SELECT * FROM c WHERE c.id in ({}, {}, {})".format( utils.get_peer_id(PeerUser(id)), utils.get_peer_id(PeerChat(id)), utils.get_peer_id(PeerChannel(id)) ) return self.get_entity_from_container(query)
def get_entity_rows_by_id(self, key, exact=True): if exact: query = self._db_query(self.Entity, self.Entity.id == key) else: ids = (utils.get_peer_id(PeerUser(key)), utils.get_peer_id(PeerChat(key)), utils.get_peer_id(PeerChannel(key))) query = self._db_query(self.Entity, self.Entity.id in ids) row = query.one_or_none() return (row.id, row.hash) if row else None
def dump_channel(self, channel_full, channel, photo_id, timestamp=None): """Dump a Channel into the Channel table Params: ChannelFull, Channel to dump, MediaID of the profile photo in the DB Returns -""" # Need to get the full object too for 'about' info values = (get_peer_id(channel), timestamp or round(time.time()), channel_full.about, channel.title, channel.username, photo_id, channel_full.pinned_msg_id) self._insert_if_valid_date('Channel', values, date_column=1, where=('ID', get_peer_id(channel)))
def get_entity_rows_by_id(self, key: int, exact: bool = True) -> Optional[Tuple[int, int]]: if exact: query = self._db_query(self.Entity, self.Entity.id == key) else: ids = ( utils.get_peer_id(PeerUser(key)), utils.get_peer_id(PeerChat(key)), utils.get_peer_id(PeerChannel(key)) ) query = self._db_query(self.Entity, self.Entity.id.in_(ids)) row = query.one_or_none() return (row.id, row.hash) if row else None
async def _dump_messages(self, messages, target): """ Helper method to iterate the messages from a GetMessageHistoryRequest and dump them into the Dumper, mostly to avoid excessive nesting. Also enqueues any media to be downloaded later by a different coroutine. """ for m in messages: if isinstance(m, types.Message): media_id = self.dumper.dump_media(m.media) if media_id and self._check_media(m.media): self.enqueue_media( media_id, utils.get_peer_id(target), m.from_id, m.date ) m._client = self.client event_id = None patterns = [re.compile(x) for x in [r'\s(\d+)\s*', r'(\d+)$']] buttons = await m.get_buttons() if buttons: for row in buttons: if row[0].data: for pattern in patterns: match = pattern.search(row[0].data.decode("utf-8")) if match: event_id = match.group(1) self.dumper.dump_message( message=m, context_id=utils.get_peer_id(target), forward_id=self.dumper.dump_forward(m.fwd_from), media_id=media_id, event_id=event_id ) elif isinstance(m, types.MessageService): if isinstance(m.action, types.MessageActionChatEditPhoto): media_id = self.dumper.dump_media(m.action.photo) self.enqueue_photo(m.action.photo, media_id, target, peer_id=m.from_id, date=m.date) else: media_id = None self.dumper.dump_message_service( message=m, context_id=utils.get_peer_id(target), media_id=media_id )
def dump_chat(self, chat, photo_id, timestamp=None): """Dump a Chat into the Chat table Params: Chat to dump, MediaID of the profile photo in the DB Returns -""" if isinstance(chat.migrated_to, types.InputChannel): migrated_to_id = chat.migrated_to.channel_id else: migrated_to_id = None values = (get_peer_id(chat), timestamp or round(time.time()), chat.title, migrated_to_id, photo_id) return self._insert_if_valid_date('Chat', values, date_column=1, where=('ID', get_peer_id(chat)))
def enqueue_entities(self, entities): """ Enqueues the given iterable of entities to be dumped later by a different coroutine. These in turn might enqueue profile photos. """ for entity in entities: eid = utils.get_peer_id(entity) self._displays[eid] = utils.get_display_name(entity) if isinstance(entity, types.User): if entity.deleted or entity.min: continue # Empty name would cause IntegrityError elif isinstance(entity, types.Channel): if entity.left: continue # Getting full info triggers ChannelPrivateError elif not isinstance(entity, (types.Chat, types.InputPeerUser, types.InputPeerChat, types.InputPeerChannel)): # Drop UserEmpty, ChatEmpty, ChatForbidden and ChannelForbidden continue if eid in self._checked_entity_ids: continue else: self._checked_entity_ids.add(eid) if isinstance(entity, (types.User, types.InputPeerUser)): self._user_queue.put_nowait(entity) else: self._chat_queue.put_nowait(entity)
def _dump_admin_log(self, events, target): """ Helper method to iterate the events from a GetAdminLogRequest and dump them into the Dumper, mostly to avoid excessive nesting. Also enqueues any media to be downloaded later by a different coroutine. """ for event in events: assert isinstance(event, types.ChannelAdminLogEvent) if isinstance(event.action, types.ChannelAdminLogEventActionChangePhoto): media_id1 = self.dumper.dump_media(event.action.new_photo) media_id2 = self.dumper.dump_media(event.action.prev_photo) self.enqueue_photo(event.action.new_photo, media_id1, target, peer_id=event.user_id, date=event.date) self.enqueue_photo(event.action.prev_photo, media_id2, target, peer_id=event.user_id, date=event.date) else: media_id1 = None media_id2 = None self.dumper.dump_admin_log_event(event, utils.get_peer_id(target), media_id1, media_id2) return min(e.id for e in events)
async def dump_supergroup(self, supergroup_full, supergroup, photo_id, timestamp=None): """Dump a Supergroup into the Supergroup table Params: ChannelFull, Channel to dump, MediaID of the profile photo in the DB. Returns -""" # Need to get the full object too for 'about' info row = { 'id': get_peer_id(supergroup), 'date_updated': timestamp or round(time.time()), 'about': getattr(supergroup_full, 'about', None) or '', 'title': supergroup.title, 'username': supergroup.username, 'photo_id': photo_id, 'pin_message_id': supergroup_full.pinned_msg_id } for callback in self._dump_callbacks['supergroup']: callback(row) return await update_by_invalidation_time(db_super_group, row, self.invalidation_time)
async def dump_channel(self, channel_full, channel, photo_id, timestamp=None): """Dump a Channel into the Channel table. Params: ChannelFull, Channel to dump, MediaID of the profile photo in the DB Returns -""" # Need to get the full object too for 'about' info row = { 'id': get_peer_id(channel), 'date_updated': timestamp or round(time.time()), 'about': channel_full.about, 'title': channel.title, 'username': channel.username, 'photo_id': photo_id, 'pin_message_id': channel_full.pinned_msg_id } for callback in self._dump_callbacks['channel']: callback(row) return await update_by_invalidation_time(db_channel, row, self.invalidation_time)
async def download_past_media(self, dumper, target_id): """ 下载已经转储到数据库但尚未下载的媒体。 格式化文件名之后已存在的文件的媒体将被忽略,不会再次重新下载。 """ # TODO Should this respect and download only allowed media? Or all? target_in = await self.client.get_input_entity(target_id) target = await self.client.get_entity(target_in) target_id = utils.get_peer_id(target) rows = db_message.find({ 'context_id': target_id, "check": { '$ne': None } }) for row in rows: await self._media_queue.put( (row['media_id'], target_id, row['from_id'], datetime.datetime.utcfromtimestamp(row['date']))) chat_name = utils.get_display_name(target) media_consumers = [ asyncio.ensure_future(self._media_consumer(self._media_queue, chat_name, i), loop=self.loop) for i in range(10) ] await asyncio.wait(media_consumers)
async def live_lottery_group(event): sender = await event.get_sender() from_name = utils.get_display_name(sender) from_id = utils.get_peer_id(sender) logging.info(f'来自{event.chat_id}: {from_id} | {from_name}说的>>{event.message.text}') if event.chat_id == group_id: await client.forward_messages(forward_bot_id, event.message)
def test_formatter_get_chat(self): """ Ensures that the BaseFormatter is able to fetch the expected entities when using a date parameter. """ chat = types.Chat( id=123, title='Some title', photo=types.ChatPhotoEmpty(), participants_count=7, date=datetime.now(), version=1 ) dumper = Dumper(self.dumper_config) fmt = BaseFormatter(dumper.conn) for month in range(1, 13): await dumper.dump_chat(chat, None, timestamp=int(datetime( year=2010, month=month, day=1 ).timestamp())) dumper.commit() cid = tl_utils.get_peer_id(chat) # Default should get the most recent version date = fmt.get_chat(cid).date_updated assert date == datetime(year=2010, month=12, day=1) # Expected behaviour is to get the previous available date target = datetime(year=2010, month=6, day=29) date = fmt.get_chat(cid, target).date_updated assert date == datetime(year=2010, month=6, day=1) # Expected behaviour is to get the next date if previous unavailable target = datetime(year=2009, month=12, day=1) date = fmt.get_chat(cid, target).date_updated assert date == datetime(year=2010, month=1, day=1)
async def heheh(event): Ll = [] err = "" async with event.client.conversation(event.chat_id) as conv: await conv.send_message( "• Send The Chat Id(s), which you want user to Join Before using Chat/Pm Bot" ) await conv.send_message( "Example : \n`-1001234567\n-100778888`\n\nFor Multiple Chat(s).") try: msg = await conv.get_response() except AsyncTimeOut: return await conv.send_message("TimeUp!\nStart from /start back.") if not msg.text or msg.text.startswith("/"): return await conv.send_message("Cancelled!", buttons=get_back_button("chatbot")) for chat in msg.message.split("\n"): if chat.startswith("-") or chat.isdigit(): chat = int(chat) try: CHSJSHS = await event.client.get_entity(chat) Ll.append(get_peer_id(CHSJSHS)) except Exception as er: err += f"**{chat}** : {er}\n" if err: return await conv.send_message(err) udB.set("PMBOT_FSUB", str(Ll)) await conv.send_message("Done!\nRestart Your Bot.", buttons=get_back_button("chatbot"))
async def download_past_media(self, dumper, target_id): """ Downloads the past media that has already been dumped into the database but has not been downloaded for the given target ID yet. Media which formatted filename results in an already-existing file will be *ignored* and not re-downloaded again. """ # TODO Should this respect and download only allowed media? Or all? target_in = await self.client.get_input_entity(target_id) target = await self.client.get_entity(target_in) target_id = utils.get_peer_id(target) bar = tqdm.tqdm(unit='B', desc='media', unit_divisor=1000, unit_scale=True, bar_format=BAR_FORMAT, total=0, postfix={'chat': utils.get_display_name(target)}) msg_cursor = dumper.conn.cursor() msg_cursor.execute( 'SELECT ID, Date, FromID, MediaID FROM Message ' 'WHERE ContextID = ? AND MediaID IS NOT NULL', (target_id, )) msg_row = msg_cursor.fetchone() while msg_row: await self._download_media(media_id=msg_row[3], context_id=target_id, sender_id=msg_row[2], date=msg_row[1], bar=bar) msg_row = msg_cursor.fetchone()
async def heheh(event): Ll = [] err = "" async with event.client.conversation(event.chat_id) as conv: await conv.send_message( "• Send The Chat Id(s), which you want user to Join Before using Chat/Pm Bot\n\n• Send /clear to disable PmBot Force sub..\n• • Send /cancel to stop this process.." ) await conv.send_message( "Example : \n`-1001234567\n-100778888`\n\nFor Multiple Chat(s).") try: msg = await conv.get_response() except AsyncTimeOut: return await conv.send_message( "**• TimeUp!**\nStart from /start back.") if not msg.text or msg.text.startswith("/"): timyork = "Cancelled!" if msg.text == "/clear": udB.del_key("PMBOT_FSUB") timyork = "Done! Force Subscribe Stopped\nRestart your Bot!" return await conv.send_message( "Cancelled!", buttons=get_back_button("cbs_chatbot")) for chat in msg.message.split("\n"): if chat.startswith("-") or chat.isdigit(): chat = int(chat) try: CHSJSHS = await event.client.get_entity(chat) Ll.append(get_peer_id(CHSJSHS)) except Exception as er: err += f"**{chat}** : {er}\n" if err: return await conv.send_message(err) udB.set_key("PMBOT_FSUB", str(Ll)) await conv.send_message("Done!\nRestart Your Bot.", buttons=get_back_button("cbs_chatbot"))
def _dump_entity(self, entity): needed_sleep = 1 eid = utils.get_peer_id(entity) if isinstance(entity, types.User): full = self.client(functions.users.GetFullUserRequest(entity)) photo_id = self.dumper.dump_media(full.profile_photo) self.dumper.dump_user(full, photo_id=photo_id) self.download_profile_photo(full.profile_photo, entity) elif isinstance(entity, types.Chat): needed_sleep = 0 photo_id = self.dumper.dump_media(entity.photo) self.dumper.dump_chat(entity, photo_id=photo_id) self.download_profile_photo(entity.photo, entity) elif isinstance(entity, types.Channel): full = self.client( functions.channels.GetFullChannelRequest(entity)) photo_id = self.dumper.dump_media(full.full_chat.chat_photo) if entity.megagroup: self.dumper.dump_supergroup(full.full_chat, entity, photo_id) else: self.dumper.dump_channel(full.full_chat, entity, photo_id) self.download_profile_photo(full.full_chat.chat_photo, entity) self._pending_ids.discard(eid) self._dumped_ids.add(eid) return needed_sleep
async def get_portal_by_tgid(self, request: web.Request) -> web.Response: err = self.check_authorization(request) if err is not None: return err try: tgid, _ = resolve_id(int(request.match_info["tgid"])) except ValueError: return self.get_error_response(400, "tgid_invalid", "Given chat ID is not valid.") portal = Portal.get_by_tgid(tgid) if not portal: return self.get_error_response(404, "portal_not_found", "Portal to given Telegram chat not found.") user, _ = await self.get_user(request.query.get("user_id", None), expect_logged_in=None, require_puppeting=False) return web.json_response({ "mxid": portal.mxid, "chat_id": get_peer_id(portal.peer), "peer_type": portal.peer_type, "title": portal.title, "about": portal.about, "username": portal.username, "megagroup": portal.megagroup, "can_unbridge": (await portal.can_user_perform(user, "unbridge")) if user else False, })
def _entity_to_row(self, e): if not isinstance(e, types.TLObject): return try: p = utils.get_input_peer(e, allow_self=False) marked_id = utils.get_peer_id(p) except TypeError: # Note: `get_input_peer` already checks for non-zero `access_hash`. # See issues #354 and #392. It also checks that the entity # is not `min`, because its `access_hash` cannot be used # anywhere (since layer 102, there are two access hashes). return if isinstance(p, (types.InputPeerUser, types.InputPeerChannel)): p_hash = p.access_hash elif isinstance(p, types.InputPeerChat): p_hash = 0 else: return username = getattr(e, 'username', None) or None if username is not None: username = username.lower() phone = getattr(e, 'phone', None) name = utils.get_display_name(e) or None return self._entity_values_to_row(marked_id, p_hash, username, phone, name)
async def get_chats(self, request: web.Request) -> web.Response: data, user, err = await self.get_user_request_info(request, expect_logged_in=True) if err is not None: return err if not user.is_bot: chats = await user.get_dialogs() return web.json_response([{ "id": get_peer_id(chat), "title": chat.title, } for chat in chats]) else: return web.json_response([{ "id": get_peer_id(chat.peer), "title": chat.title, } for chat in user.portals.values() if chat.tgid])
def add_read_action(entity, target, action): if is_read(entity, target): action() else: read_actions.add( MessageAction(chat_id=get_peer_id(entity), message_id=target.id, action=action))
async def receive_new_message_handler(event): try: sender = await event.get_sender() peer_id = utils.get_peer_id(sender) if peer_id == dialog_id: await callback(event.text) except: pass
def dump_supergroup(self, supergroup_full, supergroup, photo_id, timestamp=None): """Dump a Supergroup into the Supergroup table Params: ChannelFull, Channel to dump, MediaID of the profile photo in the DB Returns -""" # Need to get the full object too for 'about' info values = (get_peer_id(supergroup), timestamp or round(time.time()), supergroup_full.about if hasattr( supergroup_full, 'about') else '', supergroup.title, supergroup.username, photo_id, supergroup_full.pinned_msg_id) return self._insert_if_valid_date('Supergroup', values, date_column=1, where=('ID', get_peer_id(supergroup)))
def my_handler(event: events.NewMessage.Event): global recent_reacts # This utils function gets the unique identifier from peers (to_id) to_id = utils.get_peer_id(event.message.to_id) # Through event.raw_text we access the text of messages without format words = re.split('\W+', event.raw_text) # Try to match some reaction for trigger, response in REACTS.items(): if len(recent_reacts[to_id]) > 3: # Silently ignore triggers if we've recently sent 3 reactions break if trigger in words: # Remove recent replies older than 10 minutes recent_reacts[to_id] = [ a for a in recent_reacts[to_id] if datetime.now() - a < timedelta(minutes=10) ] # Send a reaction as a reply (otherwise, event.respond()) event.reply(response) # Add this reaction to the list of recent actions recent_reacts[to_id].append(datetime.now()) # Automatically send relevant media when we say certain things # When invoking requests, get_input_entity needs to be called manually if event.out: if event.raw_text.lower() == 'x files theme': client.send_voice_note(event.message.to_id, 'xfiles.m4a', reply_to=event.message.id) if event.raw_text.lower() == 'anytime': client.send_file(event.message.to_id, 'anytime.png', reply_to=event.message.id) if '.shrug' in event.text: event.edit(event.text.replace('.shrug', r'¯\_(ツ)_/¯'))