File size: 1,434 Bytes
9ca6868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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())
    # if 'hello' in event.raw_text:
    #     await event.reply('hi!')
    # else:
    #     await event.reply('what?')

client.start()
client.loop.run_until_complete(main())
client.run_until_disconnected()