def extract_mentioned_users(client: Client, message: Message) -> List[SimplifiedUser]: """ Recibe un mensaje para sacar de él todas las menciones de usuario y devolver el ID y First Name, tanto menciones directas (@coleguita) como text_mentions de usuarios sin username. :param client: Pyrogram Client :param message: Pyrogram Message object :return: Dictionary with "uid" <int> and "fname" <str> fields. """ users_mentioned = list() for entity in message.entities: errors = False try: if entity.type == "mention": mention_in_message = message.text[ entity.offset : entity.offset + entity.length ] username = mention_in_message[1:] # removes @ user = client.get_users(username) users_mentioned.append( SimplifiedUser(uid=user.id, fname=user.first_name) ) elif entity.type == "text_mention": users_mentioned.append( SimplifiedUser(uid=entity.user.id, fname=entity.user.first_name) ) except BadRequest: errors = True return users_mentioned, errors
def findfc(client: Client, message: Message): find_result = Find() mongo = db_tools.use_mongo() # fetch id from username if not Filters.reply(message): if len(message.command) < 2: text = '你沒有輸入 username 呀' message.reply_text(text) return username = message.command[1] # check failure. if username[0] != '@': text = '你輸入的 {input_} 應該不是正確的 username 格式,應該是 `@` 開頭'.format( input_=username) message.reply_text(text, parse_mode='markdown') return # ready to go. user_id = None while True: try: user_id = client.get_users(username).id except BadRequest: text = find_result.USERNOTFOUND.format(username=username) break except FloodWait as wait: sleep(wait.x) else: break else: # fetch id from reply message # prevent ?_? if message.reply_to_message.from_user.is_self: text = '你找一個別人回覆啊,我是機器人啊,難不成我要用腦波跟你玩?' message.reply_text(text) return user_id = message.reply_to_message.from_user.id username = '******'.format( username=message.reply_to_message.from_user.username ) if message.reply_to_message.from_user.username else '' if user_id: # make query to get ready. mongo_query = {'chat.id': user_id} mongo_result = mongo.nintendo.find_one(mongo_query) if not isinstance(mongo_result, dict): text = find_result.NOTEXIST else: # make it to an obkect. user = from_dict(data_class=users, data=mongo_result) # privacy is basic human right hex project. if user.privacy: text = find_result.NOTVISBLE else: text = find_result.FOUND.format(username=username, fcode=user.fcode) message.reply_text(text, parse_mode='markdown')
async def handle_raw_update(self, client: Client, update: Update, *_args) -> None: if isinstance(update, pyrogram.api.types.UpdateDeleteChannelMessages): return self.trackers.push(update, True) if isinstance(update, pyrogram.api.types.UpdateDeleteMessages): return self.trackers.push(update, True) if isinstance(update, (pyrogram.api.types.UpdateUserName, pyrogram.api.types.UpdateUserPhoto)): userObj = client.get_users(update.user_id) return self.trackers.push_user(userObj) if isinstance(update, pyrogram.api.types.UpdateUserStatus) and \ isinstance(update.status, (pyrogram.api.types.UserStatusOffline, pyrogram.api.types.UserStatusOnline)): return self.trackers.push(update, True)
def get_users(client: Client, uids: Iterable[Union[int, str]]) -> Optional[List[User]]: # Get users result = None try: result = client.get_users(user_ids=uids) except FloodWait as e: raise e except PeerIdInvalid: return None except Exception as e: logger.warning(f"Get users {uids} error: {e}", exc_info=True) return result
from pyrogram import Client, filters app = Client('polcovnic', phone_number='+380957441355') with app: code = input('Enter code: ') app.phone_code = code user = app.get_users('self') original_text = 'message' message = app.send_message(user.id, original_text) message.delete()
members = app.get_chat_members(chat_id=int(chatID), limit=int(limit)) chatMembersInfoPrint(members) elif choice == "2": userInput = input( Fore.CYAN + "[user-id (id)/username (u)/phone number (pn)]: " + Style.RESET_ALL) members = app.get_chat_member(chat_id=int(chatID), user_id=userID(userInput)) chatMembersInfoPrint(members, total=False) elif choice == "2": userInput = input( Fore.CYAN + "[user-id (id)/username (u)/phone number (pn)]: " + Style.RESET_ALL) lookupResult = app.get_users([userID(userInput)]) singleUserLookup({'user': lookupResult[0]}) elif choice == "3": dialogs = app.get_dialogs() userInput = input( Fore.CYAN + "[user-id (id)/username (u)/phone number (pn)]: " + Style.RESET_ALL) for d in dialogs: if int(d['chat']['id']) < 0: try: members = app.get_chat_member( chat_id=int(d['chat']['id']), user_id=userID(userInput)) if members: chatListPrint(d['chat']) except BadRequest as e:
try: with app: current_name = app.get_users("me").first_name if current_name != original_name: restore_original_name() original_name = current_name except Exception as e: print(e) pass print("TIEMPO") sleep(waitTime) if __name__ == '__main__': with app: original_name = app.get_users("me").first_name print("Original name: " + original_name) while True: try: checkForNewSong() except KeyboardInterrupt: print('KeyboardInterruption') try: restore_original_name() remove_first_profile_photo() print("TECLADO") sys.exit(0) except SystemExit: restore_original_name() sys.exit(0) pass
def start(self): app = Client(session_name=str(self.folder_name / "my_account"), api_id=self.config['credentials']['pyrogram_api_id'], api_hash=self.config['credentials']['pyrogram_api_hash']) app.start() self.my_id = app.get_users('me').id app.stop() @app.on_message(filters.text) def my_handler(client, message: types.Message): message_data = message.text.split(' ') command = message_data[0] if message.chat.id == self.my_id: if command == "!help": message.reply( "In chat with yourself you can change settings of your addon:\n\n" "<code>!ping_all_from_non_me</code> - toggle using !ping_all command " f"by all members of chat\n<b>({self.ping_all_from_non_me})</b>\n\n", parse_mode="html") elif command == "!ping_all_from_non_me": self.ping_all_from_non_me = not self.ping_all_from_non_me message.reply( f"ping_all_from_non_me = <b>{self.ping_all_from_non_me}</b> ", parse_mode="html") else: if command == '!ping_all': if self.ping_all_from_non_me or (self.my_id == message.from_user.id): logging.warning(f"Pinging all in {message.chat.title}") list_mentions = [] for member in message.chat.iter_members(): if not member.user.is_bot: try: list_mentions.append(member.user.mention) except AttributeError: pass conclusion = '' if self.my_id != message.from_user.id: conclusion = '\n\n<b>Requested by:</b>\n' try: conclusion += message.from_user.mention except AttributeError: pass message.reply('\n'.join(list_mentions) + conclusion) logging.warning( f"Done pinging all in {message.chat.title}") elif command == "!get_all": message.delete() list_mentions = [] for member in message.chat.iter_members(): if not member.user.is_bot: try: list_mentions.append(member.user.mention) except AttributeError: pass app.send_message( "me", "\n".join(list_mentions) + f"\n\nfrom {message.chat.title}") app.run()
from pyrogram import Client from pyrogram.api import functions import time import sys import setting app = Client(session_name="new_my_account_covach", api_id=299527, api_hash='27fb778154da0c8b9f37b80eecfed58b') # my_account_covach #or #my_account_vielfrass app.start() #print(app.get_me()) #g=int(input("lol : ")) x = 0 print(app.get_users('me')) #while x<g: #app.join_chat('t.me/joinchat/Hl0rgBG-KSsumYlURb9uWQ') # app.leave_chat(1351754478,False) # time.sleep(4) #app.join_chat('t.me/joinchat/Hl0rgBG-KSsumYlURb9uWQ') #app.get_messages #app.leave_chat('t.me/joinchat/Hl0rgBG-KSsumYlURb9uWQ',False) # x = str(app.send(functions.messages.CheckChatInvite('Hl0rgBG-KSsumYlURb9uWQ'))) #print(x) s = str.split(x, 'id": ') print(s[1]) #app.send(1351754478,x) b = 0