defstudio / telegraph

Telegraph is a Laravel package for fluently interacting with Telegram Bots
MIT License
675 stars 116 forks source link

Long polling does not work #518

Closed just-pthai-it closed 7 months ago

just-pthai-it commented 7 months ago

Just like the title, I am unable to use long poling. It always return response immediately. Here is my script: `

    $bot = TelegraphBot::fromToken('6187681620:token');

    $bot->updates(60, 823310133)->each(function(TelegramUpdate $update){
        // do smt
    });

` I tried long polling by calling telegram getUpdates API using Postman with timeout and offset parameters and it did work.

just-pthai-it commented 7 months ago

[Update] I tried to debug src code and found that the body of POST request is empty. The property data of Telegraph class is empty when called in InteractsWithTelegram trait. No timeout and offset parameters were passed. That 's why long polling did not work.

just-pthai-it commented 7 months ago

[Update 2] The reason why the data of Telegraph is empty is that you set the data property of Telegraph instance but you return the clone version of it in the function below.

HasBotsAndChats.php

public function botUpdates(int $timeout = null, int $offset = null, int $limit = null, array $allowedUpdates = null): Telegraph
{
    $telegraph = clone $this;

    $telegraph->endpoint = self::ENDPOINT_GET_BOT_UPDATES;

    if($offset !== null) {
        $this->data['offset'] = $offset;
    }

    if($limit !== null) {
        $this->data['limit'] = $limit;
    }

    if($timeout !== null) {
        $this->data['timeout'] = $timeout;
    }

    if($allowedUpdates !== null) {
        $this->data['allowed_updates'] = $allowedUpdates;
    }

    return $telegraph;
}
fabio-ivona commented 7 months ago

fixed, we are releasing a patch right now

thanks!