Skip to content

Instantly share code, notes, and snippets.

@OlegKorn
Last active January 5, 2025 02:20
Show Gist options
  • Save OlegKorn/d08689f96462fa280c0655623c5e1f7a to your computer and use it in GitHub Desktop.
Save OlegKorn/d08689f96462fa280c0655623c5e1f7a to your computer and use it in GitHub Desktop.
telethon - logging channel's posts
import os
import logging
from telethon import TelegramClient, events
from telethon import errors
from telethon.tl.custom import Button
from telethon.errors.rpcerrorlist import FloodWaitError
import asyncio
import os, re, sys
def delete_forbidden_chars(string):
# deleting forbidden chars
FORBIDDEN_CHARS = '''!()-[]{};:'"\,<>./?@🇿`~*#$%^&*_~_'''
cleared_string = re.sub('['+FORBIDDEN_CHARS+']', '', string).replace('"', "")
return cleared_string
channel = 'https://t.me/..'
THIS_SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
API_ID = ...
API_HASH = '....'
FORMAT = '%(message)s'
logging.basicConfig(
handlers=[logging.FileHandler(THIS_SCRIPT_DIR + '/' + channel.split('/')[-1] + '.log', 'w', 'utf-8')],
level=logging.INFO,
format=FORMAT,
)
if __name__ == '__main__':
client = TelegramClient(
'test',
API_ID,
API_HASH
)
client.start()
for message in client.iter_messages(channel):
link_to_post = channel + f'/{str(message.id)}'
msg_text = delete_forbidden_chars(str(message.text))
print(message.id, link_to_post, msg_text, sep='\n')
print()
print()
print()
'''
logging.info('-' * 30)
logging.info(message.id)
logging.info(link_to_post)
logging.info(msg_text)
logging.info('\n')
logging.info('\n')
logging.info('\n')
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment