Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update is stuck if I use await in closures #73

Open
parmonov98 opened this issue Aug 13, 2022 · 1 comment
Open

update is stuck if I use await in closures #73

parmonov98 opened this issue Aug 13, 2022 · 1 comment

Comments

@parmonov98
Copy link

I'm trying to use react/async:3.0 's await in closures.

$bot->onCommand('update',
    function (Context $ctx) {
            $update = $ctx->getUpdate();
            $message = $update->getMessage();
            $chat = $update->getEffectiveChat();
            $sender = $update->getEffectiveUser();

            if ($chat->getType() === 'supergroup'){
                $chat_settings_path = 'settings/' . $chat->getId() . '.json';
                $settings = [];
                if (file_exists($chat_settings_path)){
                    $settings = json_decode(file_get_contents($chat_settings_path), 1);
                }
                $settings['title'] = $chat->getTitle();
                $settings['id'] = $chat->getId();
                $settings['type'] = $chat->getType();
                $settings['username'] = $chat->getUsername();
                $get_group_chat = await($ctx->getChat($chat->getId()));
                $get_admin_members = await($ctx->getChatAdministrators($chat->getId()));
                $group_chat = $get_group_chat;
                $admin_members = $get_admin_members;

                $admins = [];
                $owner = null;
                foreach ($admin_members as $member){
                    $admins[] = $member->getUser();
                    if ($member->getStatus() == 'creator')
                        $owner = $member->getUser();
                }
                if ($sender->getId() == $owner->getId()){
                    $settings['linked_channel_id'] = $group_chat->getLinkedChatId();
                    $settings['administrators'] = $admins;
                    $settings['owner'] = $owner;
                    file_put_contents($chat_settings_path, json_encode($settings));
                    $response = await($ctx->sendMessage("Guruh sozlamalari yangilandi!", [
                        'chat_id' => $sender->getId(),
                    ]));
                    return React\Http\Message\Response::plaintext(
                        'test0'
                    );
                }else{
                    $sender_id = $sender->getId();
                    $name = $sender->getFirstName();
                    $promise = $ctx->sendMessage("<a href='tg://user?id=$sender_id'>$name</a>, Bu buyruqni faqat guruh admini bera oladi!", [
                        'parse_mode' => 'HTML'
                    ]);
                    $response = await($promise);
                    var_dump($response);

                }
            }

        }
);

It send works correctly but the update is not processed. I mean the update is stuck in pending updates. if I restart the bot it process update and doesn't drop it.

and new updates are being queued.

What am I missing in my code to drop update after processing?

@awohsen
Copy link
Contributor

awohsen commented Nov 8, 2023

Looking at docs on await(), it clearly says:

While the promise is pending, this function will assume control over the event loop. Internally, it will run() the default loop until the promise settles and then calls stop() to terminate execution of the loop. This means this function is more suited for short-lived promise executions when using promise-based APIs is not feasible. For long-running applications, using promise-based APIs by leveraging chained then() calls is usually preferable.

Therefore it will stop the loop which means it will stop new updates requests.

However, you can use coroutine() for that and use yield to get the same looking function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants