badfarm / zanzara

Asynchronous PHP Telegram Bot Framework built on top of ReactPHP
MIT License
201 stars 28 forks source link

needs synchronous requests #18

Closed hamidqp closed 3 years ago

hamidqp commented 3 years ago

Hi, Thanks for the full Asynchronous PHP Telegram Bot Framework it makes telegram bots so fast.

but actually, sometimes I really need several methods that work Continuous and cant use ->then for all of them.

example when want to forward a message to many chats and then save the message-id of the sent message. or when want to send 50 messages to count 1 - 50, and many other things like that.

cheeghi commented 3 years ago

when want to forward a message to many chats and then save the message-id of the sent message

For this particular case there's no need to use synchronous requests, just do:

$chatIds = [869444431235, 869444431235, ....]; // 50 chats
foreach ($chatIds as $chatId) {
    $ctx->sendMessage('text', ['chat_id' => $chatId])
        ->then(function (Message $msg) {
            $msgId = $msg->getMessageId();
        });
}

Anyway, I can imagine in some case is useful to perform operations sequentially. You might use reactphp-block to perform synchronous requests.

hamidqp commented 3 years ago

Thanks a lot, reactphp-block completely solved that.