我正在使用以下教程:https://medium.com/@jiayu./automatic-replies-for-telegram-85075f28321
因此,我的代码是:
import time
from telethon import TelegramClient, events
# sample API_ID from https://github.com/telegramdesktop/tdesktop/blob/f98fdeab3fb2ba6f55daf8481595f879729d1b84/Telegram/SourceFiles/config.h#L220
# or use your own
api_id = XXXX
api_hash = 'XXXXXXX'
# fill in your own details here
phone = 'XXXXX'
session_file = '/path/to/session/file' # use your username if unsure
password = 'YOUR_PASSWORD' # if you have two-step verification enabled
# content of the automatic reply
message = "This message is autogenerated.\nApologies, I am unavailable at the moment. Feel free to call me for anything urgent."
count = 1
if __name__ == '__main__':
# Create the client and connect
# use sequential_updates=True to respond to messages one at a time
client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True)
@client.on(events.NewMessage(incoming=True))
async def handle_new_message(event):
if event.is_private: # only auto-reply to private chats
from_ = await event.client.get_entity(event.from_id) # this lookup will be cached by telethon
if not from_.bot: # don't auto-reply to bots
print(count, time.asctime(), '-', event.message) # optionally log time and message
# print( '-', time.asctime()) # optionally log time and message
count += 1 # incrementing the count
time.sleep(1) # pause for 1 second to rate-limit automatic replies
await event.respond(message)
print(time.asctime(), '-', 'Auto-replying...')
client.start(phone, password)
client.run_until_disconnected()
print(time.asctime(), '-', 'Stopped!')当我第一次运行代码时,代码工作得很好。然后我停止了它,改变了信息,它不再起作用了。它以“自动回复.”的信息启动但不回复电报上的信息。重新启动计算机或删除会话文件没有帮助。
这和电报阻塞连接有关吗?
编辑:有时它工作得很好,工作也很完美。有时它根本不起作用
发布于 2022-01-20 22:26:30
在代码中将from_替换为sender_ everywhere,即3次。
断裂变化
Message.from_id现在是一个同龄人,而不是int!如果您想要标记的发件人ID (很像以前的行为),请将.from_id的所有用法替换为.sender_id。这将主要工作,但当然,在旧的和新的版本,你必须考虑的事实,这个发件人可能不再是用户。https://docs.telethon.dev/en/stable/misc/changelog.html?highlight=from_id#id14
https://stackoverflow.com/questions/63951591
复制相似问题