I'm trying to get message from telegram channel but when I run this code I get strange output

203 views Asked by At

I don't know what to do, for me it seems like no problem is here but it is. I can get last message ID with bot on telegram I can insert URL but that output is strange, infinite loop.

from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.types import (
PeerChannel
)
# Setting configuration values
api_id = 'my_api_is'
api_hash ='my_api_hash'

phone = 'my_phone_number'
username = 'my_username'

# Create the client and connect
client = TelegramClient(username, api_id, api_hash)
client.start()
print("Client Created")
# Ensure you're authorized
if not client.is_user_authorized():
    client.send_code_request(phone)
    try:
        client.sign_in(phone, input('Enter the code: '))
    except SessionPasswordNeededError:
        client.sign_in(password=input('Password: '))

user_input_channel = input("enter entity(telegram URL or entity id):")

if user_input_channel.isdigit():
    entity = PeerChannel(int(user_input_channel))
else:
    entity = user_input_channel

my_channel = client.get_entity(entity)

actual_id = 'Id of last message' 
last_mess = ''
while True:
    actual_mess = client.get_messages(
        entity=my_channel,
        limit=1,
        ids=actual_id
    )
    if actual_mess != last_mess:
        actual_id = actual_id + 1
        last_mess = actual_mess
        print(last_mess)

It's like this:

<coroutine object MessageMethods.get_messages at 0x000002313D2F90C8>

<coroutine object MessageMethods.get_messages at 0x000002313D2F9148>

<coroutine object MessageMethods.get_messages at 0x000002313D2F90C8>

<coroutine object MessageMethods.get_messages at 0x000002313D2F9148>

<coroutine object MessageMethods.get_messages at 0x000002313D2F90C8>

<coroutine object MessageMethods.get_messages at 0x000002313D2F9148>

<coroutine object MessageMethods.get_messages at 0x000002313D2F90C8>

<coroutine object MessageMethods.get_messages at 0x000002313D2F9148>

<coroutine object MessageMethods.get_messages at 0x000002313D2F90C8>

I will appreciate every help guys!!! I'm starting programmer with good ideas for programs to invent.

0

There are 0 answers