【问题标题】:How to proceed to the next cycle?如何进入下一个周期?
【发布时间】:2018-10-05 00:39:30
【问题描述】:

如果有新成员加入,此代码会向 Telegram 超级组发送消息。当发送消息时发生错误,我想更改我的帐户以继续。可以转到下一个“项目”。 收到错误时如何循环转到下一个帐户?

from pyrogram import Client, Filters

list_account = ['001', '002']

for item in list_account:
    app = Client(item)
    @app.on_message(Filters.chat("public_link_chat") & Filters.new_chat_members)
    def welcome(client, message):
        try:
            client.send_message(
                message.chat.id, 'Test',
                reply_to_message_id=message.message_id,
                disable_web_page_preview=True
            )
        except Exception as e:
            print(e)
            # How do I go to the next account in a loop when I receive an error?

    app.start()
    app.join_chat("public_link_chat")
    app.idle()

在这种情况下,“继续”功能不起作用。

此处功能说明:https://docs.pyrogram.ml/resources/UpdateHandling

【问题讨论】:

  • 当你说“得到这样的东西”时,你是什么意思?是什么阻止你这样做? (是的,您是在事件驱动的基础上调用的,但这对您有何影响?)
  • 我看不出你卡在哪里了。 return 是从函数传回值的方式。如果您想要 welcome 值,请使用 return 并将其保存在调用程序的变量中。
  • @Prune, welcomepyrogram 调用,而不是由 OP 的代码直接调用,因此返回值的处理由该库控制。并不是说这使 OP 的问题更清楚,但是当 Welcome 是由单独的组件调用的回调时,“只从 Welcome 中返回一个值”是没有意义的。
  • ...当然,他们的welcome函数可以调用number_one()并打印返回值。
  • 电报呼叫您的欢迎功能。所以你甚至没有从 for 循环中被调用,这就是为什么你不能继续下一次迭代。

标签: python pyrogram


【解决方案1】:

只需添加app.is_idle = False

from pyrogram import Client, Filters

list_account = ['001', '002']

for item in list_account:
    app = Client(item)
    @app.on_message(Filters.chat("public_link_chat") & Filters.new_chat_members)
    def welcome(client, message):
        try:
            client.send_message(
                message.chat.id, 'Test',
                reply_to_message_id=message.message_id,
                disable_web_page_preview=True
            )
        except Exception as e:
            print(e)
            # How do I go to the next account in a loop when I receive an error?
            app.is_idle = False

    app.start()
    app.join_chat("public_link_chat")
    app.idle()

你一定要查看热图源代码中空闲逻辑的these lines

while self.is_idle:
    time.sleep(1)

如果你想要一个无限循环,check outitertools.cycle,可以这样使用:

for item in itertools.cycle(list_account):
    do_something()

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2021-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多