【发布时间】:2021-01-05 03:32:02
【问题描述】:
我正在使用以下教程: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!')
代码在我第一次运行时运行良好。然后我停止了它,更改了消息,它不再起作用了。它以“自动回复...”消息启动,但不回复 Telegram 上的消息。重新启动计算机或删除会话文件没有帮助。
是否与 Telegram 阻止连接有关?
编辑:有时它可以完美运行。有时它根本不起作用:(
【问题讨论】:
-
你能说得更具体点吗? “停止工作”是相当模糊的。它只是拒绝开始吗?它是否报告任何错误消息?你没有升级你的依赖还是什么?
-
@yedpodtrzitko,当然!抱歉:) 没有错误,它启动并且似乎没有错误。但是,它不会创建自动响应。有时它会(一天),而另一天则不会。我发现这是一个常见问题github.com/LonamiWebs/Telethon/issues/1541,我只需要时间来解决它。一旦找到解决方案,我将回到这篇文章。 PS 依赖项和包的版本正确。我正在为此使用 Conda。
-
@DaniilGannota 你解决了这个问题了吗?你能分享一下吗?
-
@AJHope,对不起,但没有????我发现了进一步的证据表明它不起作用,因为库需要更新但没有维护,所以我放弃了。如果您现在找到了解决方案,请随时分享!
-
@DaniilGannota 我解决了这个问题。更改参数sequential_updates=False。
标签: python api telegram telethon