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

[v3.0.0] How to access async response? #564

Closed
schnz opened this issue Apr 19, 2018 · 1 comment
Closed

[v3.0.0] How to access async response? #564

schnz opened this issue Apr 19, 2018 · 1 comment

Comments

@schnz
Copy link

schnz commented Apr 19, 2018

How is the developer supposed to access the response for asynchronous requests? When calling $api->getMe() it returns an (non-populated/empty) Telegram\Bot\Objects\User object. I've followed the call-stack and cannot find a proper way to access the response promise:

#0: \Telegram\Bot\Api::getMe() (through \Telegram\Bot\Methods\Get)
#1: \Telegram\Bot\Api::get() (through \Telegram\Bot\Traits\Http) // <-- This call should preserve the promise!
#2: \Telegram\Bot\Api::sendRequest() (through \Telegram\Bot\Traits\Http)
#3: \Telegram\Bot\TelegramClient::sendRequest()
#4: \Telegram\Bot\TelegramClient::getResponse()
#5: \Telegram\Bot\TelegramResponse::__construct():

The problem is, that TelegramResponse seems to discard the promise once and for all in its constructor.

TelegramResponse.php:

// $response gets dismissed if it is an instanceof PromiseInterface!
public function __construct(TelegramRequest $request, $response)
{
    if ($response instanceof ResponseInterface) {
        // [Removed for readability]
    } elseif ($response instanceof PromiseInterface) {
        $this->httpStatusCode = null;
    } else {
       // [Removed for readability]
    }

    $this->request = $request;
    $this->endPoint = (string) $request->getEndpoint();
}

I just found this library and wanted to check if it proves useful for migrating an existing bot. I thought this would be a perfect choice since it relies on GuzzleHttp (which my current implementation also does) and supports asynchronous requests (which my current implementation also relies on).

So my question is basically: Am I missing something?, and if not: Is this feature supposed to be implemented in a future release? What I would expect on how to use the library is this:

// Create required instances
$api = new \Telegram\Bot\Api($myToken = '...', $async = true, $clientThatUsesCustomHandler);

$api
    ->getMe()
    ->then(function (\Telegram\Bot\Objects\User $user) {
        // Do something with $user...
        echo "I am executed asynchronously";
    });

// Enter main loop
while(true) {
    // Application logic goes here...

    // Required by guzzle to process pending requests...
    $guzzleHandlerThatIsUsedByMyClient->tick();
    \GuzzleHttp\Promise\queue()->run();

    // Probably sleep for some time
    usleep(0.025 * 1E6);
}

Ideally the \Telegram\Bot\HttpClients\GuzzleHttpClient would provide a tick() method on its own so that it can be used for integration in the main application event loop. This would render the need to create a cutom handler unnecessary when using the default implementation.

@welcome
Copy link

welcome bot commented Apr 19, 2018

👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

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