File size: 1,600 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
55
56
57
58
59
60
import logging

from telethon import TelegramClient, events
from telethon.errors import ChannelPrivateError


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()

channel_link = 'https://t.me/redakciya_channel'
recipient = 'https://t.me/kazakov123'

client = TelegramClient('data/session.session', api_id, api_hash)


@client.on(events.NewMessage(chats=channel_link))
async def on_new_message(event):
    if '#ньюсдня' in event.raw_text:
        print(event.message)
    else:
        print('@@@ other message: ')
        print(event.message)
    await client.forward_messages(
        entity=recipient,
        messages=event.message.message,
        from_peer=channel_link
    )


async def check_channel():
    try:
        channel = await client.get_entity(channel_link)
        print(f'Channel open: {channel.title}')
        async for msg in client.iter_messages(entity=channel, limit=100):
            if msg.message.lower().find('#ньюсдня') != -1:
                print(f'Last message:')
                print(msg.message)
                break
        print(f'Listening for new messages...')
    except ChannelPrivateError:
        print("The channel is private and you don't seem to have access.")
    except ValueError as e:
        print(f"An error occurred: {e}")


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