irazasyed / telegram-bot-sdk

🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
https://telegram-bot-sdk.com
BSD 3-Clause "New" or "Revised" License
3.03k stars 670 forks source link

setAsyncRequest(true) is not working #762

Closed ilshatlatyp closed 4 years ago

ilshatlatyp commented 4 years ago

I try to use with Laravel:

$result = Telegram::setAsyncRequest(true)->sendMessage([
                'chat_id' => 'chat_id',
                'text' => $message
              ]);

and got no error, but message is not sends But the code is working without setAsyncRequest(true)

foremtehan commented 4 years ago

Same here.

Ilhatl commented 4 years ago

i solved it by using Telegram::sendMessage([ 'chat_id' => 'chat_id', 'text' => $message ]); inside laravel job https://laravel.com/docs/7.x/queues. I didnt found any other solution

foremtehan commented 4 years ago

You solved the problem by not using it ?

KielD-01 commented 4 years ago

@foremtehan @Ilhatl I have tested it, and it works just fine. Test it with this code block:

class WebHookController extends Controller
{
    public function hook()
    {
        /** @var Update $update */
        $update = Telegram::commandsHandler(true);

        if ($update->getChat()->get('id')) {
            /** @var Update $response */
            Telegram::setAsyncRequest(true)->sendMessage([
                'text' => 'Test Async Message Response',
                'chat_id' => $update->getChat()->get('id'),
            ]);
        }
    }
}
Result : Async Messages

@ilshatlatyp as You are using chat_id as not the ID of the chat, but a string value. Hope, this helps.

foremtehan commented 4 years ago

Its not working in laravel Listener.

KielD-01 commented 4 years ago

@foremtehan Please, share the code You are using.

foremtehan commented 4 years ago

Put your codes inside the laravel Job or Listener (ShouldQueued)) and you will see its not working, I'm sure its not about my codes because when i remove the setAsyncRequest(true) it working whenever it inside a Job or anywhere else.

KielD-01 commented 4 years ago

@foremtehan It's not a problem with this package. If You would like to have it worked out for You, make Your HttpClientHandler and do the same with implementing methods from the HttpClientInterface. This is the problem with Guzzle and how it works with the async requests from the Queue/Job.

foremtehan commented 4 years ago

Bot the author claims "Supports Laravel out of the box."Queue and Listener" is the part of laravel.

KielD-01 commented 4 years ago

@foremtehan As I have written before, this is not the problem with this package (telegram-bot-sdk). It's all about how Guzzle works. Yes - this package supports Laravel, but Guzzle. If You are not satisfied with the standard HTTP handler then only an option - is to write own.

irazasyed commented 4 years ago

Async and queues are different.

kksandr7-deprecated commented 1 year ago

Hi, I had the same problem. In my case the problem was in Guzzle and it is not solved https://github.com/guzzle/guzzle/issues/2752 (__destruct() was called before execute() in CurlMultiHandler)

So in addition to resolving promises via Telegram\Bot\HttpClients\GuzzleHttpClient::__destruct, I added resolution via register_shutdown_function:

register_shutdown_function([$your_bot->getClient()->getHttpClientHandler(), '__destruct']);