68 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| import os
 | |
| import sys
 | |
| import time
 | |
| 
 | |
| #Gettit with pip bro   
 | |
| from telethon import TelegramClient, events, utils
 | |
| import json
 | |
| 
 | |
| def get_env(name, message, cast=str):
 | |
|     if name in os.environ:
 | |
|         return os.environ[name]
 | |
|     while True:
 | |
|         value = input(message)
 | |
|         try:
 | |
|             return cast(value)
 | |
|         except ValueError as e:
 | |
|             print(e, file=sys.stderr)
 | |
|             time.sleep(1)
 | |
| sesion = get_env('TG_SESION', 'Enter your API sesion name: ')
 | |
| api_id = get_env('TG_API_ID', 'Enter your API ID: ', int)
 | |
| api_hash = get_env('TG_API_HASH', 'Enter your API hash: ')
 | |
| 
 | |
| # Create and start the client so we can make requests
 | |
| with TelegramClient(sesion, api_id, api_hash, spawn_read_thread=False)).start() as client:
 | |
|     client.start()
 | |
|     me = client.get_me()
 | |
| 
 | |
| #TODO: selector de conversaciones si el bot no esta inicializado
 | |
|     chat1 = client.get_entity('t.me/lonami')
 | |
|     createNewDump('lonami', chat1)
 | |
| 
 | |
|     chat2 = client.get_entity('t.me/lol')
 | |
|     createNewDump('lol', chat2)
 | |
| 
 | |
|     chat3 = client.get_entity('t.me/telegram')
 | |
|     createNewDump('telegram', chat3)
 | |
|     
 | |
|     chats = (chat1, chat2, chat3)
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| @client.on.message(events.NewMessage(chats=chats, incoming = True))
 | |
| 
 | |
| def handler(events):
 | |
|     event.get 
 | |
| 
 | |
| def createNewDump(name, entity):
 | |
|     filePath = "./dumps/"+name+"Dump.json"
 | |
|     if !os.path.isfile(filePath):
 | |
|         with open(filePath,"w+") as f:
 | |
|             meta = {'meta':[{
 | |
|                             'name': name,
 | |
|                             'entity': entity,
 | |
|                             'creationDate': strftime("%Y-%m-%d %H:%M:%S", gmtime())
 | |
|                             }]}
 | |
|             json.dump(meta, f)
 | |
|             
 | |
| 
 | |
| def addEntryToDump(name, element):
 | |
|     #TODO Test si existe el dump anterior
 | |
|     filePath = "./dumps/"+name+"Dump.json"
 | |
|     with open(filePath,"w+") as f:
 | |
|         message = {'mensaje':[{
 | |
|                         'element': element,
 | |
|                         'Date': strftime("%Y-%m-%d %H:%M:%S", gmtime())
 | |
|                         }]}
 | |
|         json.dump(meta, f) |