def test_cdn_download(): client = TelegramClient(None, api_id, api_hash) client.session.set_dc(0, '149.154.167.40', 80) assert client.connect() try: phone = '+999662' + str(randint(0, 9999)).zfill(4) client.send_code_request(phone) client.sign_up('22222', 'Test', 'DC') me = client.get_me() data = os.urandom(2 ** 17) client.send_file( me, data, progress_callback=lambda c, t: print('test_cdn_download:uploading {:.2%}...'.format(c/t)) ) msg = client.get_message_history(me)[1][0] out = BytesIO() client.download_media(msg, out) assert sha256(data).digest() == sha256(out.getvalue()).digest() out = BytesIO() client.download_media(msg, out) # Won't redirect assert sha256(data).digest() == sha256(out.getvalue()).digest() client.log_out() finally: client.disconnect()
def main(): client = TelegramClient('session_name', config.API_ID, config.API_HASH) client.connect() # me = client.sign_in(code='27105') me = client.get_me() target_user = client.get_entity(config.TARGET_USER_ID) limit = None total_messages, messages, users = client.get_message_history( config.TARGET_USER_ID, limit=limit) header = [ 'id', 'from_id', 'to_id', 'date', 'message', 'is_media', 'is_edited' ] with open('message_history.csv', 'w') as f: writer = csv.writer(f) writer.writerow(header) for message in messages: print_message(message) is_media = message.media is not None is_edited = message.edit_date is not None row = [ message.id, message.from_id, message.to_id.user_id, message.date, message.message, is_media, is_edited ] writer.writerow(row) if message.views is not None: print('What are those?') import pdb pdb.set_trace()
def main(): logger.info("Client started") client = TelegramClient(cfg.telegram['channel'], cfg.telegram['api_id'], cfg.telegram['api_hash']) client.connect() delay = cfg.util['delay'] # Ensure you're authorized if not client.is_user_authorized(): try: client.send_code_request(cfg.telegram['phone']) client.sign_in(cfg.telegram['phone'], input('Enter the code: ')) # Two-step verification may be enabled except SessionPasswordNeededError: pw = getpass('Two step verification is enabled. ' 'Please enter your password: '******'channel']) start_time = datetime.datetime.strptime(cfg.util['start'], cfg.util['format']) end_time = datetime.datetime.strptime(cfg.util['end'], cfg.util['format']) messages = client.get_message_history(channel, offset_date=end_time) with open('sended_posts.txt', 'r') as file: sended_posts = file.read().splitlines() for message in messages: if message.date > start_time and str(message.id) not in sended_posts: try: tweet_post(message.id, message.message) logger.info("Tweet with message_id " + message.id + "sent") with open('sended_posts.txt', 'a') as file: file.write(str(message.id) + '\n') time.sleep(delay) except AttributeError: logger.error("Message with id " + message.id + " has no message attribute") logger.info("Client finished")
class IFTTTClient: def __init__(self, api_id=196708, api_hash='f0c865a3ecc24c4af25dfae27d475938'): self.latest_msg_date = maya.now() logging.debug(f'set latest msg date to - {self.latest_msg_date}') logging.info('starting telegram client') self.client = TelegramClient('session_name', api_id, api_hash) self.client.start() def get_latest_command(self): logging.debug('getting message history') msg = self.client.get_message_history('IFTTT')[0] date = maya.MayaDT.from_datetime(msg.date) if date > self.latest_msg_date: logging.debug(f'new msg found - {repr(msg.message)}') self.latest_msg_date = date logging.debug(f'set latest msg date to - {self.latest_msg_date}') return msg.message logging.debug('no new msg found')
]) #My API values api_id = 181055 api_hash = '8c267b7f0bcbdc604fe27b12142a9687' phone_number = '+37368351333' client = TelegramClient('me', api_id, api_hash) client.start() #print(client.get_me().stringify()) #client.send_message('self', 'Hello World from Telethon!') #dialogs, entities = client.get_dialogs(dialog_count) for message in client.get_message_history('CryptoBredNews', limit=1): last = message.id print(last) while True: for message in client.get_message_history('CryptoBredNews', min_id=last): #print(utils.get_display_name(message.sender), message.id, message.message) cryptomessage = message.message if message.id > last: last = message.id # print(last) if cryptomessage != '': mesg1 = cryptomessage.encode('utf-8') # print(mesg1) client.send_message('cryptoanalizatorfeed', mesg1)
class TelegramClient(object): clients = {} def __init__(self, session, phone, api_id, api_hash): if isinstance(phone, str): if phone.startswith('+'): phone = int(phone[1:]) self.session = session self.phone = phone self.api_id = api_id self.api_hash = api_hash self.authorized = False self.client = BaseTelegramClient( self.session, self.api_id, self.api_hash, ) self.client.connect() def authorize(self): if not self.client.is_user_authorized(): self.client.send_code_request(self.phone) return False return True def sign_in_code(self, code): try: self.client.sign_in(phone=self.phone, code=code) except SessionPasswordNeededError: return NEED_PASSWORD else: self.authorized = True def sign_in_password(self, password): self.client.sign_in(phone=self.phone, password=password) def dialogs(self): dialogs, entities = self.client.get_dialogs(limit=100) _entities = (e.to_dict() for e in entities) entities = {e['id']: e for e in _entities} dialogs = [d.to_dict() for d in dialogs] dialogs.sort(key=itemgetter('top_message'), reverse=True) for d in dialogs: if 'user_id' in d['peer']: d['peer'].update({'meta': entities[d['peer']['user_id']]}) elif 'chat_id' in d['peer']: d['peer'].update({'meta': entities[d['peer']['chat_id']]}) elif 'channel_id' in d['peer']: d['peer'].update({'meta': entities[d['peer']['channel_id']]}) return {'dialogs': dialogs} def messages(self, entity_id, limit=20, offset_date=None, offset_id=0, max_id=0, min_id=0): if isinstance(entity_id, str) and entity_id.startswith( '@') or entity_id.startswith('+'): entity = self.client.get_entity(entity_id) else: try: entity = self.client.get_entity(PeerUser(int(entity_id))) except ValueError: try: entity = self.client.get_entity(PeerChat(int(entity_id))) except ValueError: entity = self.client.get_entity(PeerChannel( int(entity_id))) count, messages, senders = self.client.get_message_history( entity, limit=limit, offset_date=offset_date, offset_id=offset_id, max_id=max_id, min_id=min_id, ) messages = [m.to_dict() for m in messages] # ToDo: Fix media! for m in messages: if 'media' in m and m['media']: m.update({'media': None}) _senders = (s.to_dict() for s in senders) senders = {s['id']: s for s in _senders} messages.sort(key=itemgetter('date'), reverse=False) return {'count': count, 'messages': messages, 'senders': senders} def send_message(self, entity_id, message): message = self.client.send_message(int(entity_id), message) return {'message': message.to_dict()}
class TelegramApi: client = None dialogs = [] messages = [] need_update_message = 0 need_update_online = 0 need_update_current_user = -1 need_update_read_messages = 0 def __init__(self): config = configparser.ConfigParser() config.read('config.ini') api_id = config.get('telegram_api', 'api_id') api_hash = config.get('telegram_api', 'api_hash') workers = config.get('telegram_api', 'workers') session_name = config.get('telegram_api', 'session_name') self.timezone = int(config.get('other', 'timezone')) self.message_dialog_len = int(config.get('app', 'message_dialog_len')) # proxy settings if config.get('proxy', 'type') == "HTTP": proxy_type = socks.HTTP elif config.get('proxy', 'type') == "SOCKS4": proxy_type = socks.SOCKS4 elif config.get('proxy', 'type') == "SOCKS5": proxy_type = socks.SOCKS5 else: proxy_type = None proxy_addr = config.get('proxy', 'addr') proxy_port = int(config.get('proxy', 'port')) if config.get('proxy', 'port').isdigit() else None proxy_username = config.get('proxy', 'username') proxy_password = config.get('proxy', 'password') proxy = (proxy_type, proxy_addr, proxy_port, False, proxy_username, proxy_password) # create connection self.client = TelegramClient(session_name, api_id, api_hash, update_workers=int(workers), spawn_read_thread=True, proxy=proxy) self.client.start() self.me = self.client.get_me() self.dialogs = self.client.get_dialogs(limit=self.message_dialog_len) self.messages = len(self.dialogs) * [None] self.online = len(self.dialogs) * [""] self.messages[0] = self.client.get_message_history(self.dialogs[0].entity, limit=self.message_dialog_len) # event for new messages @self.client.on(events.NewMessage) def my_event_handler(event): for i in range(len(self.dialogs)): # if event message from user if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \ self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id: self.event_message(i) # from chat elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event._chat_peer, 'chat_id') and \ self.dialogs[i].dialog.peer.chat_id == event._chat_peer.chat_id: self.event_message(i) # from chat elif hasattr(self.dialogs[i].dialog.peer, 'channel_id') and hasattr(event._chat_peer, 'channel_id') and \ self.dialogs[i].dialog.peer.channel_id == event._chat_peer.channel_id: self.event_message(i) # other else: pass # event for read messages @self.client.on(events.Raw(types=None)) def my_event_handler(event): if hasattr(event, 'confirm_received') and hasattr(event, 'max_id'): for i in range(len(self.dialogs)): # from user if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event.peer, 'user_id') and \ self.dialogs[i].dialog.peer.user_id == event.peer.user_id: self.dialogs[i].dialog.read_outbox_max_id = event.max_id self.need_update_current_user = i # from chat elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event.peer, 'chat_id') and \ self.dialogs[i].dialog.peer.chat_id == event.peer.chat_id: self.dialogs[i].dialog.read_outbox_max_id = event.max_id self.need_update_current_user = i # other else: pass self.need_update_read_messages = 1 # event for online/offline @self.client.on(events.UserUpdate(chats=None, blacklist_chats=False)) def my_event_handler(event): for i in range(len(self.dialogs)): if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \ self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id: # I think need little bit change this if event.online: self.online[i] = "Online" elif event.last_seen is not None: self.online[i] = "Last seen at " + str(event.last_seen + (timedelta(self.timezone) // 24)) else: self.online[i] = "" self.need_update_current_user = i self.need_update_online = 1 def event_message(self, user_id): if self.messages[user_id] is None: self.get_messages(user_id) new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=self.messages[user_id][0].id - 1) else: new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=self.messages[user_id][0].id) for j in range(len(new_message) - 1, -1, -1): self.messages[user_id].insert(0, new_message[j]) self.dialogs[user_id].unread_count += 1 self.messages[user_id].sort(key=lambda x: x.id, reverse=True) self.remove_duplicates(self.messages[user_id]) self.need_update_message = 1 self.need_update_current_user = user_id def get_messages(self, user_id): if self.messages[user_id] is None: data = self.client.get_message_history(self.dialogs[user_id].entity, limit=self.message_dialog_len) # need check exceptions self.messages[user_id] = data self.messages[user_id].sort(key=lambda x: x.id, reverse=True) return data else: return self.messages[user_id] def get_message_by_id(self, user_id, message_id): for i in range(len(self.messages[user_id])): if self.messages[user_id][i].id == message_id: return self.messages[user_id][i] # return self.client.get_message_history(self.dialogs[user_id].entity, limit=1, min_id=message_id-1) def delete_message(self, user_id, message_id): self.client.delete_messages(self.dialogs[user_id].entity, message_id) def download_media(self, media, path): return self.client.download_media(media, path) def message_send(self, message, user_id, reply=None): data = self.client.send_message(self.dialogs[user_id].entity, message, reply_to=reply) # read message self.client.send_read_acknowledge(self.dialogs[user_id].entity, max_id=data.id) # save message new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1)) for j in range(len(new_message) - 1, -1, -1): self.messages[user_id].insert(0, new_message[j]) self.messages[user_id].sort(key=lambda x: x.id, reverse=True) self.remove_duplicates(self.messages[user_id]) def file_send(self, file, user_id, func): data = self.client.send_file(self.dialogs[user_id].entity, file, progress_callback=func) # save message new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1)) for j in range(len(new_message) - 1, -1, -1): self.messages[user_id].insert(0, new_message[j]) self.messages[user_id].sort(key=lambda x: x.id, reverse=True) self.remove_duplicates(self.messages[user_id]) def read_all_messages(self, user_id): if hasattr(self.messages[user_id][0], 'id'): self.client.send_read_acknowledge(self.dialogs[user_id].entity, max_id=self.messages[user_id][0].id) def remove_duplicates(self, messages): i = 0 while i < len(messages) - 1: if messages[i].id == messages[i + 1].id: del messages[i] i = i - 1 i = i + 1 return messages
me = client.get_me() print(me.stringify()) # Print self def print_entity(name): ent = client.get_entity(name) print(ent.stringify()) return ent lonami = print_entity("lonami") # user #movies_inc = print_entity("movies_inc") # channel mp3downloads1 = print_entity("javascript_all") # megagroup total, messages, senders = client.get_message_history(mp3downloads1) offset_id = 0 import time print(total) tot = 0 while True: result = client( GetHistoryRequest(mp3downloads1, limit=100, offset_date=None, offset_id=offset_id, max_id=0, min_id=0, add_offset=0))
taddcells = wks.range('B2:B' + str(len(tdates) + 1)) tinvcells = wks.range('C2:C' + str(len(tdates) + 1)) tdelcells = wks.range('D2:D' + str(len(tdates) + 1)) for i in range(len(tdatecells)): tdatecells[i].value = tdates[i] taddcells[i].value = tadds[i] tinvcells[i].value = tinvs[i] tdelcells[i].value = tdels[i] wks.update_cells(tdatecells) wks.update_cells(taddcells) wks.update_cells(tinvcells) wks.update_cells(tdelcells) h = client.get_message_history("tiesdb", limit=15000) x = pd.DataFrame([i.to_dict() for i in h.data]) with open('/home/aslepnev/git/ej/teleties.pickle', 'wb') as tmp: pickle.dump(x, tmp, protocol=pickle.HIGHEST_PROTOCOL) offset, limit, p = 0, 100, [] while True: participants = client( GetParticipantsRequest(client.get_entity('https://t.me/tiesdb'), ChannelParticipantsSearch(''), offset, limit, hash=0)) if not participants.users: break p.extend(participants.users)
class BotChecker(object): def __init__(self, session_name, api_id, api_hash, phone_number, updater=None): self.phone_number = phone_number self.client = TelegramClient(session_name, api_id, api_hash, update_workers=1, spawn_read_thread=True) self.client.connect() self._pinged_bots = [] self._responses = {} self.botbuilders = [] if not self.client.is_user_authorized(): log.info("Sending code request...") self.client.send_code_request(phone_number) if updater: updater.bot.send_message(settings.ADMINS[0], CONFIRM_PHONE_CODE, reply_markup=ForceReply()) updater.dispatcher.add_handler(MessageHandler( Filters.reply & Filters.user(settings.ADMINS[0]), lambda bot, update: authorization_handler( bot, update, self)), group=3) self.pending_authorization = True else: self.client.send_code_request(phone_number) self.client.sign_in(phone_number, input('Enter code: ')) else: self._initialize() def reset(self): self._pinged_bots = [] self._responses = {} def authorize(self, code): self.client.sign_in(self.phone_number, code) self._initialize() def _initialize(self): self.pending_authorization = False self._run_update_handler() def _run_update_handler(self): self.update_thread = threading.Thread(target=self._update_handler, daemon=True) self.update_thread.start() def _update_handler(self): def inner(update): try: uid = update.message.from_id except AttributeError: try: uid = update.user_id except AttributeError: return # try: # entity = self.client.get_entity(uid) # log.debug("Received response from @{}".format(entity.username)) # except: log.debug("Received message from {}".format(uid)) if uid in self._pinged_bots: message_text = None if hasattr(update, 'message'): if hasattr(update.message, 'message'): message_text = update.message.message self._responses[uid] = message_text while True: try: ud = self.client.updates.poll() if ud: inner(ud) else: log.error( "Received an empty update. Assuming that the bot responded." ) for b in self._pinged_bots: self._responses[b] = True except Exception as e: log.info("Exception in update thread. Continuing...") log.exception(e) time.sleep(0.2) def _init_thread(self, target, *args, **kwargs): thr = Thread(target=target, args=args, kwargs=kwargs) thr.start() def schedule_conversation_deletion(self, peer, delay=5): def inner(): time.sleep(delay) entity = self.client.get_input_entity(peer) self.client(DeleteHistoryRequest(entity, max_id=999999999)) log.debug("Deleted conversation with {}".format(entity)) thr = threading.Thread(target=inner, args=()) thr.start() def delete_all_conversations(self): all_peers = [ utils.resolve_id(x[0]) for x in self.client.session.entities.get_input_list() ] for peer in all_peers: log.debug("Deleting conversation with {}...".format(peer)) try: input_entity = self.client.session.entities.get_input_entity( peer[0]) self.client( DeleteHistoryRequest(input_entity, max_id=9999999999999999)) except: log.error("Couldn't find {}".format(peer[0])) def get_bot_entity(self, username) -> User: entity = self.client.get_entity(username) if not hasattr(entity, 'bot'): raise NotABotError("This user is not a bot.") time.sleep(1) return entity def _response_received(self, bot_user_id): return bot_user_id in [k for k in self._responses.keys()] def _delete_response(self, bot_user_id): del self._responses[bot_user_id] def ping_bot(self, entity, timeout=30): input_entity = utils.get_input_peer(entity) time.sleep(1) bot_user_id = input_entity.user_id self._pinged_bots.append(bot_user_id) log.debug('Pinging @{username}...'.format(username=entity.username)) self.client.send_message(input_entity, '/start') start = datetime.datetime.now() while not self._response_received(bot_user_id): if datetime.datetime.now() - start > datetime.timedelta( seconds=timeout): self._pinged_bots.remove(bot_user_id) log.debug('@{} did not respond after {} seconds.'.format( entity.username, timeout)) return False time.sleep(0.2) response_text = self._responses[bot_user_id] self._delete_response(bot_user_id) self._pinged_bots.remove(bot_user_id) if isinstance(response_text, str): if 'Use /off to pause your subscription.' in response_text \ or 'Use /stop to unsubscribe.' in response_text: self.botbuilders.append(entity) # Evaluate WJClub's ParkMeBot flags reserved_username = ZERO_CHAR1 + ZERO_CHAR1 + ZERO_CHAR1 + ZERO_CHAR1 parked = ZERO_CHAR1 + ZERO_CHAR1 + ZERO_CHAR1 + ZERO_CHAR2 maintenance = ZERO_CHAR1 + ZERO_CHAR1 + ZERO_CHAR2 + ZERO_CHAR1 if zero_width_encoding(response_text) in (reserved_username, parked, maintenance): return False return True def get_bot_last_activity(self, entity): entity = self.get_bot_entity(entity) _, messages, _ = self.client.get_message_history(entity, limit=5) peer_messages = [m for m in messages if m.from_id == entity.id] if len(peer_messages) == 0: return None last_peer_message = peer_messages[-1] return last_peer_message.date def disconnect(self): self.client.disconnect()
'USERNAME', 'FLOODSTORM \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n @username // @spam' ) sleep(3) client.send_message( 'USERNAME', 'FLOODSTORM \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n @username // @spam' ) sleep(3) client.send_message( 'USERNAME', 'FLOODSTORM \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n @username // @spam' ) sleep(3) client.send_message( 'USERNAME', 'FLOODSTORM \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n @username // @spam' ) sleep(3) client.send_message( 'USERNAME', 'FLOODSTORM \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n @username // @spam' ) sleep(3) '''INFORMATION: MORE LINES = MORE MESSAGES''' print("Messages sent!") client.download_profile_photo(me) messages = client.get_message_history('leavemeblank') client.download_media(messages[0])
def run(self, conf, args, plugins): session_file = os.path.join(os.path.expanduser("~"), ".config/harpoon/telegram") client = TelegramClient(session_file, int(conf['Telegram']['id']), conf['Telegram']['hash']) client.connect() if not client.is_user_authorized(): client.send_code_request(conf['Telegram']['phone']) code_ok = False while not code_ok: code = input("Enter Telegram code:") try: code_ok = client.sign_in(conf['Telegram']['phone'], code) except SessionPasswordNeededError: # FIXME: getpass is not imported, that would not work password = getpass( 'Two step verification enabled. Please enter your password: '******'subcommand'): if args.subcommand == 'id': try: res = client.get_entity(args.ID) print( json.dumps(res.to_dict(), sort_keys=True, indent=4, default=json_serial)) except ValueError: print('Identifier not found') elif args.subcommand == "messages": entity = client.get_entity(args.ID) messages = client.get_message_history(entity, args.limit) if args.format == "text": print("%i messages downloaded:" % messages[0]) for msg in messages[1]: if isinstance(msg, telethon.tl.types.MessageService): print("[%s] Message Service: %s" % (msg.date.isoformat(), msg.action.message)) else: if msg.media is None: print("[%s] %s (%i views)" % (msg.date.isoformat(), msg.message, msg.views)) else: print("[%s] Media (%i views)" % (msg.date.isoformat(), msg.views)) elif args.format == "json": msg = [m.to_dict() for m in messages[1]] print( json.dumps(msg, sort_keys=True, indent=4, default=json_serial)) else: print("Not implemented yet, sorry!") elif args.subcommand == "users": # List users from a group try: entity = client.get_entity(args.ID) offset = 0 limit = args.limit all_participants = [] while True: participants = client.invoke( GetParticipantsRequest( entity, ChannelParticipantsSearch(''), offset, limit, hash=0)) if not participants.users: break all_participants.extend(participants.users) offset += len(participants.users) sleep( 1 ) # This line seems to be optional, no guarantees! except ChatAdminRequiredError: print( "You don't have required access rights to get this list" ) else: if args.format == "text": for p in all_participants: print("[+] User: %s (%s %s)" % (p.username, p.first_name, p.last_name)) elif args.format == "json": users = [u.to_dict() for u in all_participants] print( json.dumps(users, sort_keys=True, indent=4, default=json_serial)) else: print("Not implemented yet, sorry!") else: self.parser.print_help() else: self.parser.print_help()
class Application(Frame): def __init__(self, master): super().__init__(master) self.client = None self.entities = [] frame_login = Frame(self) label_phone = Label(frame_login, text='Phone Number:') self.entry_phone = Entry(frame_login) self.entry_phone.bind('<Return>', lambda e: self.login()) self.button_login = Button(frame_login, text='Login', command=self.login) frame_chats = Frame(self) label_chats = Label(frame_chats, text='Chat List:') scroll_xchats = Scrollbar(frame_chats, orient=HORIZONTAL) scroll_ychats = Scrollbar(frame_chats, orient=VERTICAL) self.listbox_chats = Listbox(frame_chats, xscrollcommand=scroll_xchats.set, yscrollcommand=scroll_ychats.set, width='40', selectmode=EXTENDED) self.button_select_all = Button( frame_chats, text='All', command=lambda: self.listbox_chats.select_set(0, END)) self.button_select_none = Button( frame_chats, text='None', command=lambda: self.listbox_chats.select_clear(0, END)) self.button_ok = Button(frame_chats, text='OK', command=lambda: threading.Thread( target=self.dump, daemon=True).start()) frame_console = Frame(self) scroll_xconsole = Scrollbar(frame_console, orient=HORIZONTAL) scroll_yconsole = Scrollbar(frame_console, orient=VERTICAL) self.text_console = Text(frame_console, wrap=NONE, xscrollcommand=scroll_xconsole.set, width='300', height='100', yscrollcommand=scroll_yconsole.set, bg='black', fg='white', bd=0) frame_login.columnconfigure(1, weight=0) frame_login.columnconfigure(2, weight=1) frame_login.columnconfigure(3, weight=0) frame_login.rowconfigure(1, weight=0) label_phone.grid(row=1, column=1) self.entry_phone.grid(row=1, column=2, sticky='EW') self.button_login.grid(row=1, column=3) frame_chats.columnconfigure(1, weight=0) frame_chats.columnconfigure(2, weight=0) frame_chats.columnconfigure(3, weight=0) frame_chats.columnconfigure(4, weight=0) frame_chats.rowconfigure(1, weight=0) frame_chats.rowconfigure(2, weight=1) frame_chats.rowconfigure(3, weight=0) frame_chats.rowconfigure(4, weight=0) label_chats.grid(row=1, column=1, columnspan=3) self.listbox_chats.grid(row=2, column=1, columnspan=3, sticky='NSEW') scroll_xchats.grid(row=3, column=1, columnspan=3, sticky='EW') scroll_ychats.grid(row=2, column=4, sticky='NS') scroll_xchats.config(command=self.listbox_chats.xview) scroll_ychats.config(command=self.listbox_chats.yview) self.button_select_all.grid(row=4, column=1, sticky='E') self.button_select_none.grid(row=4, column=2, sticky='EW') self.button_ok.grid(row=4, column=3, sticky='W') frame_console.columnconfigure(1, weight=1) frame_console.columnconfigure(2, weight=0) frame_console.rowconfigure(1, weight=1) frame_console.rowconfigure(2, weight=0) self.text_console.grid(row=1, column=1, padx=0, sticky='EWSN') scroll_xconsole.grid(row=2, column=1, sticky='EW') scroll_yconsole.grid(row=1, column=2, sticky='NS') scroll_xconsole.config(command=self.text_console.xview) scroll_yconsole.config(command=self.text_console.yview) self.pack(fill=BOTH, expand=1) self.columnconfigure(1, weight=1) self.columnconfigure(2, weight=0) self.rowconfigure(1, weight=0) self.rowconfigure(2, weight=1) frame_login.grid(row=1, column=1, sticky='EW', padx=(10, 16), pady=10) frame_console.grid(row=2, column=1, sticky='NSWE', padx=(10, 0), pady=(0, 35)) frame_chats.grid(row=1, column=2, rowspan=2, sticky='NS', padx=10, pady=(23, 10)) self.redirector = StdoutRedirector(self.text_console) sys.stdout = self.redirector sys.stderr = self.redirector self.entry_phone.focus() self.button_ok['state'] = 'disabled' self.button_select_all['state'] = 'disabled' self.button_select_none['state'] = 'disabled' @staticmethod def save_file(): filename = filedialog.asksaveasfilename( defaultextension=".xlsx", filetypes=(("Excel sheet", "*.xlsx"), ("All Files", "*.*"))) return filename def login(self): self.button_login['state'] = 'disabled' self.button_ok['state'] = 'disabled' self.button_select_all['state'] = 'disabled' self.button_select_none['state'] = 'disabled' self.text_console.delete('1.0', END) self.listbox_chats.delete(0, END) self.client = TelegramClient( session=self.entry_phone.get(), api_id=108674, api_hash='292a4c72c0c026690a8b02fe021d8eaa') print('Connecting to Telegram servers...') try: self.authorize_user() threading.Thread(target=self.populate_list()).start() except Exception as e: print(e) print('ERROR: Could not connect to Telegram servers.') finally: self.button_login['state'] = 'normal' def populate_list(self): print('Getting chat list...') self.list_dialogs() self.button_ok['state'] = 'normal' self.button_select_all['state'] = 'normal' self.button_select_none['state'] = 'normal' def dump(self): self.button_ok['state'] = 'disabled' self.button_select_all['state'] = 'disabled' self.button_select_none['state'] = 'disabled' try: selected_indices = self.listbox_chats.curselection() self.entities = [self.entities[int(i)] for i in selected_indices] messages = self.get_dialog_history() save_to = self.save_file() self.dump_messages(messages, save_to) print('Saved to %s' % save_to) print('=== DONE ===') except Exception as e: print(e) print('ERROR: Could not finish process.') finally: self.button_login['state'] = 'normal' def authorize_user(self): user_phone = self.entry_phone.get() if not self.client.connect(): raise Exception('Connection failed.') # Then, ensure we're authorized and have access if not self.client.is_user_authorized(): print('First run. Sending code request...') self.client.send_code_request(user_phone) self_user = None while self_user is None: # code = self.input('Enter the code you just received: ') code = simpledialog.askstring( 'Login Code', 'Enter the code you just received:') try: self_user = self.client.sign_in(user_phone, code) # Two-step verification may be enabled except SessionPasswordNeededError: pw = simpledialog.askstring( 'Two-Step Verification', 'Two step verification is enabled. Please enter your password:'******'*') self_user = self.client.sign_in(password=pw) return True def list_dialogs(self): users = [] chats = [] last_date = None chunk_size = 100 while True: result = self.client( GetDialogsRequest(offset_date=last_date, offset_id=0, offset_peer=InputPeerEmpty(), limit=chunk_size)) if not result.dialogs: break users.extend(result.users) chats.extend(result.chats) last_date = min(msg.date for msg in result.messages) sleep(.5) self.entities = chats + users # dialogs, self.entities = self.client.get_dialogs(1000) for i, entity in enumerate(self.entities, start=0): display_name = Application.get_display_name(entity) try: self.listbox_chats.insert(i, display_name) except: self.listbox_chats.insert(i, convert65536(display_name)) print( 'Chat list ready. Please select dialogs from the list and press OK.' ) # def select_dialog(self, entities): # print('\n') # str_ids = self.input('Select dialog IDs (separate by space): ').split() # ids = [int(i if i else 0) - 1 for i in str_ids] # return [entities[i] for i in ids] def get_dialog_history(self): window_size = 100 result = [] e = 0 for entity in self.entities: e += 1 history = [] total, _, _ = self.client.get_message_history(entity, limit=1) print('"(%d/%d) %s"' % (e, len(self.entities), self.get_display_name(entity))) with tqdm(total=total, bar_format= ' {percentage:3.0f}% |{bar}| {n_fmt}/{total_fmt}', ncols=100) as bar: for offset in range(0, total, window_size): while True: try: count, messages, senders = self.client.get_message_history( entity, limit=window_size, add_offset=offset) for i in range(len(messages)): messages[i].from_id = senders[i].id messages[ i].sender_name = self.get_display_name( senders[i]) messages[ i].phone = senders[i].phone if isinstance( senders[i], User) else None messages[i].username = senders[i].username except FloodWaitError as e: print(e) sleep(e.seconds + 1) print('continue') continue break history += messages sleep(1) bar.update(len(messages)) result += history return result @staticmethod def dump_messages(messages, filename): id_map = {} global id_count id_count = 0 def get_msg_id(sender_id, date): global id_count if (sender_id, date) in id_map: return id_map[(sender_id, date)] else: id_map[(sender_id, date)] = id_count id_count += 1 return id_count - 1 print('Saving received data...') with open('temp.csv', 'w', encoding='utf-8') as file: writer = csv.writer(file) writer.writerow(('MsgID', 'Date', 'Time', 'SenderID', 'Name', 'Username', 'Phone', 'Message')) for message in messages: if isinstance(message, Message): sender_id = message.from_id sender_name = message.sender_name username = message.username phone = message.phone msg_text = message.message datetime = message.date jdate = jd.datetime.fromgregorian(year=datetime.year, month=datetime.month, day=datetime.day) date = jdate.strftime('%y/%m/%d') time = datetime.strftime('%H:%M:%S') # fwd_id = None if message.fwd_from: msg_id = get_msg_id(message.fwd_from.from_id, message.fwd_from.date) else: msg_id = get_msg_id(message.from_id, message.date) writer.writerow((msg_id, date, time, sender_id, sender_name, username, phone, msg_text)) data = pd.read_csv('temp.csv', index_col=False, parse_dates=['Time'], dtype={'Phone': 'str'}) writer = pd.ExcelWriter(filename) data.to_excel(writer, 'Sheet1', index=False) writer.save() @staticmethod def get_display_name(entity): """Gets the input peer for the given "entity" (user, chat or channel) Returns None if it was not found""" if isinstance(entity, User): if entity.last_name and entity.first_name: return '{} {}'.format(entity.first_name, entity.last_name) elif entity.first_name: return entity.first_name elif entity.last_name: return entity.last_name else: return '(No name)' if isinstance(entity, Chat) or isinstance(entity, Channel): return entity.title return '(unknown)'
from telethon import TelegramClient from telethon.tl.types.input_peer_chat import InputPeerChat # API config: https://my.telegram.org/apps # Official examples: # https://github.com/LonamiWebs/Telethon/blob/0e38ab4/telethon_examples/interactive_telegram_client.py # Docs: https://telethon.readthedocs.io/en/latest/basic/quick-start.html # https://habr.com/ru/post/425151/ chat_id = 844216 client = TelegramClient('session-telethon', **API) client.connect() chat = InputPeerChat(chat_id) total_count, messages, senders = client.get_message_history(chat, limit=10) for msg in reversed(messages): # Format the message content if getattr(msg, 'media', None): content = '<{}> {}'.format( # The media may or may not have a caption msg.media.__class__.__name__, getattr(msg.media, 'caption', '')) elif hasattr(msg, 'message'): content = msg.message elif hasattr(msg, 'action'): content = str(msg.action) else: # Unknown message, simply print its class name content = msg.__class__.__name__ text = '[{}:{}] (ID={}) {}: {} type: {}'.format(msg.date.hour,
except: print("Connecting error.") quit() print("Connected.") chatTarget = 'chtwrsbot' # Lazy loop, sorry for that while True: # Get current time. timeNow = datetime.datetime.now() # Get target last message, I know its fallible, but who cares? messages = client.get_message_history(chatTarget, limit=1) # Check Foray if messages[0].message.find('/go') != -1: print(str(timeNow) + " - Foray incoming:") print(" " + str(messages[0].message)) forayTime(chatTarget) # Check Wartime if isWartime(timeNow): print(str(timeNow) + " - Defense time!") isDefended = True defenseTime(chatTarget) # Timer, in seconds. time.sleep(60)
bot = telegram.Bot(token=API_TOKEN) print(bot.get_me()) client = TelegramClient('test_session', API_ID, API_HASH) client.connect() # If you already have a previous 'session_name.session' file, skip this. client.sign_in(phone=PHONE) me = client.sign_in(code=79076) # Put whatever code you received here. print(me.stringify()) client.send_message('sensefilter', 'Hello! Talking to you from Telethon') #client(JoinChannelRequest(channel)) total, messages, senders = client.get_message_history('attractor137') x = client.download_media(messages[0]) print(x) from telethon.tl.functions.messages import GetDialogsRequest from telethon.tl.types import InputPeerEmpty from time import sleep dialogs = [] users = [] chats = [] messages = [] last_date = None chunk_size = 200 while True:
class TelegramApi: client = None dialogs = [] messages = [] need_update_message = 0 need_update_online = 0 need_update_current_user = -1 need_update_read_messages = 0 def __init__(self): config = configparser.ConfigParser() config.read('config.ini') api_id = config.get('telegram_api', 'api_id') api_hash = config.get('telegram_api', 'api_hash') workers = config.get('telegram_api', 'workers') session_name = config.get('telegram_api', 'session_name') self.timezone = int(config.get('other', 'timezone')) self.message_dialog_len = int(config.get('app', 'message_dialog_len')) # proxy settings if config.get('proxy', 'type') == "HTTP": proxy_type = socks.HTTP elif config.get('proxy', 'type') == "SOCKS4": proxy_type = socks.SOCKS4 elif config.get('proxy', 'type') == "SOCKS5": proxy_type = socks.SOCKS5 else: proxy_type = None proxy_addr = config.get('proxy', 'addr') proxy_port = int(config.get('proxy', 'port')) if config.get('proxy', 'port').isdigit() else None proxy_username = config.get('proxy', 'username') proxy_password = config.get('proxy', 'password') proxy = (proxy_type, proxy_addr, proxy_port, False, proxy_username, proxy_password) # create connection self.client = TelegramClient(session_name, api_id, api_hash, update_workers=int(workers), spawn_read_thread=True, proxy=proxy) self.client.start() self.me = self.client.get_me() self.dialogs = self.client.get_dialogs(limit=self.message_dialog_len) self.messages = len(self.dialogs) * [None] self.online = len(self.dialogs) * [""] self.messages[0] = self.client.get_message_history(self.dialogs[0].entity, limit=self.message_dialog_len) # event for new messages @self.client.on(events.NewMessage) def my_event_handler(event): for i in range(len(self.dialogs)): # if event message from user if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \ self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id: self.event_message(i) # from chat elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event._chat_peer, 'chat_id') and \ self.dialogs[i].dialog.peer.chat_id == event._chat_peer.chat_id: self.event_message(i) # from chat elif hasattr(self.dialogs[i].dialog.peer, 'channel_id') and hasattr(event._chat_peer, 'channel_id') and \ self.dialogs[i].dialog.peer.channel_id == event._chat_peer.channel_id: self.event_message(i) # other else: pass # event for read messages @self.client.on(events.Raw()) def my_event_handler(event): if hasattr(event, 'confirm_received') and hasattr(event, 'max_id'): for i in range(len(self.dialogs)): # from user if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event.peer, 'user_id') and \ self.dialogs[i].dialog.peer.user_id == event.peer.user_id: self.dialogs[i].dialog.read_outbox_max_id = event.max_id self.need_update_current_user = i # from chat elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event.peer, 'chat_id') and \ self.dialogs[i].dialog.peer.chat_id == event.peer.chat_id: self.dialogs[i].dialog.read_outbox_max_id = event.max_id self.need_update_current_user = i # other else: pass self.need_update_read_messages = 1 # event for online/offline @self.client.on(events.UserUpdate(chats=None, blacklist_chats=False)) def my_event_handler(event): for i in range(len(self.dialogs)): if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \ self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id: # I think need little bit change this if event.online: self.online[i] = "Online" elif event.last_seen is not None: self.online[i] = "Last seen at " + str(event.last_seen + (timedelta(self.timezone) // 24)) else: self.online[i] = "" self.need_update_current_user = i self.need_update_online = 1 def event_message(self, user_id): if self.messages[user_id] is None: self.get_messages(user_id) new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=self.messages[user_id][0].id - 1) else: new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=self.messages[user_id][0].id) for j in range(len(new_message) - 1, -1, -1): self.messages[user_id].insert(0, new_message[j]) self.dialogs[user_id].unread_count += 1 self.messages[user_id].sort(key=lambda x: x.id, reverse=True) self.remove_duplicates(self.messages[user_id]) self.need_update_message = 1 self.need_update_current_user = user_id def get_messages(self, user_id): if self.messages[user_id] is None: data = self.client.get_message_history(self.dialogs[user_id].entity, limit=self.message_dialog_len) # need check exceptions self.messages[user_id] = data self.messages[user_id].sort(key=lambda x: x.id, reverse=True) return data else: return self.messages[user_id] def get_message_by_id(self, user_id, message_id): for i in range(len(self.messages[user_id])): if self.messages[user_id][i].id == message_id: return self.messages[user_id][i] # return self.client.get_message_history(self.dialogs[user_id].entity, limit=1, min_id=message_id-1) def delete_message(self, user_id, message_id): self.client.delete_messages(self.dialogs[user_id].entity, message_id) def download_media(self, media, path): return self.client.download_media(media, path) def message_send(self, message, user_id, reply=None): data = self.client.send_message(self.dialogs[user_id].entity, message, reply_to=reply) # read message self.client.send_read_acknowledge(self.dialogs[user_id].entity, max_id=data.id) # save message new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1)) for j in range(len(new_message) - 1, -1, -1): self.messages[user_id].insert(0, new_message[j]) self.messages[user_id].sort(key=lambda x: x.id, reverse=True) self.remove_duplicates(self.messages[user_id]) def file_send(self, file, user_id, func): data = self.client.send_file(self.dialogs[user_id].entity, file, progress_callback=func) # save message new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1)) for j in range(len(new_message) - 1, -1, -1): self.messages[user_id].insert(0, new_message[j]) self.messages[user_id].sort(key=lambda x: x.id, reverse=True) self.remove_duplicates(self.messages[user_id]) def read_all_messages(self, user_id): if hasattr(self.messages[user_id][0], 'id'): self.client.send_read_acknowledge(self.dialogs[user_id].entity, max_id=self.messages[user_id][0].id) def remove_duplicates(self, messages): i = 0 while i < len(messages) - 1: if messages[i].id == messages[i + 1].id: del messages[i] i = i - 1 i = i + 1 return messages
api_id = 1234 api_hash = 'hash here' phone = 'phon number' username = '******' client = TelegramClient(username, api_id, api_hash) client.connect() if not client.is_user_authorized(): client.send_code_request(phone) try: client.sign_in(phone, input('Enter the code: ')) except Exception: client.sign_in(password=input('Password: ')) me = client.get_me() print(me) chats = client.get_dialogs() for c in chats: chatname = str(c.name) print(chatname) if ("Konrad" in chatname): messages = client.get_message_history(c.entity) for m in messages: print(str(m))