4

This code sends a message to the Telegram Supergroup if a new member has joined. When an error occurs when sending a message, I want to change my account to continue. It is possible to go to the next "item". How do I go to the next account in a loop when I receive an error?

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

Function "continue" does not work in this case.

Description of function here: https://docs.pyrogram.ml/resources/UpdateHandling

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • When you say "get something like this", what do you mean? What's stopping you from doing so? (Yes, you're called on an event-driven basis, but how does that impede you?) – Charles Duffy Apr 24 '18 at 16:49
  • I don't see where you're stuck. `return` is how you pass a value back from a function. If you want the `welcome` value, then use `return` and keep it in a variable in the calling program. – Prune Apr 24 '18 at 16:49
  • 2
    @Prune, `welcome` is called by `pyrogram`, not by the OP's code directly, so the handling of the return value is controlled by that library. Not that that makes the OP's question any clearer, but "just return a value from welcome" doesn't make sense when welcome is a callback invoked by a separate component. – Charles Duffy Apr 24 '18 at 16:50
  • ...of course, their `welcome` function can call `number_one()` and print the returned value. – Charles Duffy Apr 24 '18 at 16:52
  • I really don't understand this question. – Jakub Bláha Apr 24 '18 at 17:06
  • Corrected the question. I think it will be so clearer. – Владимир Apr 24 '18 at 17:49
  • Try adding a `app.stop()` in your exception handler and see if it helps. If your `app.start()` raises an exception then you will need to handle that as well – Tarun Lalwani May 02 '18 at 17:42
  • @tarun-lalwani Does not work anyway – Владимир May 03 '18 at 08:19
  • Remove the `app.idle()` as well. Also put a breakpoint to see if your code moves after `app.start()` after the stop – Tarun Lalwani May 03 '18 at 08:41
  • 1
    telegram calls on message your welcome function. So you aren't even called from the for loop, and thats why you cant continue to next iteration. – Franz Forstmayr May 04 '18 at 16:07

1 Answers1

4

Just add 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()

You should definitely check out these lines of the idle logic at the pyrogram source code:

while self.is_idle:
    time.sleep(1)

If you want an infinite loop, check out the itertools.cycle, it may be used like:

for item in itertools.cycle(list_account):
    do_something()
d2718nis
  • 1,279
  • 9
  • 13