|
import logging |
|
|
|
from telethon import TelegramClient, events |
|
|
|
|
|
logging.basicConfig( |
|
format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', |
|
level=logging.WARNING |
|
) |
|
|
|
|
|
with open('data/tg_api_id.txt') as f: |
|
api_id = f.read().strip() |
|
|
|
with open('data/tg_app_api_hash.txt') as f: |
|
api_hash = f.read().strip() |
|
|
|
client = TelegramClient('data/session.session', api_id, api_hash) |
|
|
|
channel_of_interest_id = None |
|
|
|
|
|
async def main(): |
|
global channel_of_interest_id |
|
async for dialog in client.iter_dialogs(): |
|
if dialog.name.lower().find('редакция') != -1: |
|
channel_of_interest_id = dialog.id |
|
print(f'Tracking channel "{dialog.name}" with id: {channel_of_interest_id}') |
|
break |
|
|
|
assert channel_of_interest_id is not None |
|
|
|
async for msg in client.iter_messages(entity=channel_of_interest_id, limit=100): |
|
if msg.message.lower().find('#ньюсдня') != -1: |
|
print(msg.message) |
|
break |
|
|
|
|
|
@client.on(events.NewMessage(incoming=True)) |
|
async def my_event_handler(event): |
|
chat = await event.get_chat() |
|
sender = await event.get_sender() |
|
chat_id = event.chat_id |
|
sender_id = event.sender_id |
|
print(event.stringify()) |
|
|
|
|
|
|
|
|
|
|
|
client.start() |
|
client.loop.run_until_complete(main()) |
|
client.run_until_disconnected() |
|
|