telelog/log.py
Michael De Roover 7cc7d78e8c
Implement listing on .tl.log without arguments
Log files have been moved to logs directory
2021-11-29 22:58:59 +01:00

53 lines
1.3 KiB
Python
Executable File

#!/usr/bin/python3
from telethon import TelegramClient, events, functions, types
from config import *
client = TelegramClient('telelog', api_id, api_hash)
@client.on(events.NewMessage)
async def log(event):
if event.is_private:
if event.sender_id != myid:
#Incoming message
try:
f = open(f'logs/{event.chat_id}')
except IOError:
chat = await event.get_input_chat()
rpl = "This account does not accept private messages. "
rpl += "My userbot will block you now. "
rpl += "Please reach out to me in a common group if applicable. "
await event.respond(rpl)
await client(functions.contacts.BlockRequest(chat))
f = open(f'logs/{event.chat_id}', "a")
f.write("")
else:
#Outgoing message
f = open(f'logs/{event.chat_id}', "a")
f.write("")
f.write(event.raw_text + "\n")
f.close()
# Interactive commands
msg = event.raw_text
if event.sender_id != myid:
return
if msg.startswith('.tl.alive'):
rpl = "Telelog alive and well!"
await event.edit(rpl)
if msg.startswith('.tl.log'):
arr = msg.split()
if len(arr) == 1:
import os
rpl = "Available log files:\n"
rpl += os.popen("ls logs").read()
await event.edit(rpl)
else:
chat = f'logs/{arr[1]}'
f = open(chat)
await event.edit(f.read())
client.start()
client.run_until_disconnected()