我正在尝试从电报频道中获取消息,但是当我运行此代码时,我得到了奇怪的输出

问题描述

我不知道该怎么办,对我来说似乎没问题,但是确实如此。我可以通过电报上的bot获得最后一个消息ID,可以插入URL,但输出结果是奇怪的,无限循环。

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)

是这样的:

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

我将非常感谢每个帮助人员!!!我正在为程序员准备好发明的好主意。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)