R
R
RMate2019-07-31 17:21:11
PHP
RMate, 2019-07-31 17:21:11

How to send a sequence of messages in strict order using batch?

Hello!
I am writing a small chat bot, there is a need to send a sequence of messages.
Send by method

$fb = new \Facebook\Facebook([ данные подключения ]);
$batch = [];
foreach ($messages as $msg) { // messages - уже валидное сообщение для фейсбука
    $batch[] = $fb->request('POST', '/me/messages', $msg);
}
$fb->sendBatchRequest($batch);

And everything works, but that's not the task, the sequence of messages is violated. It is not surprising, the documentation says that each request is executed asynchronously.
there is Official example and it works... tried like this:
$fb = new \Facebook\Facebook([ данные подключения ]);
$batch = [];
$i = 0;
foreach ($messages as $msg) { // messages - уже валидное сообщение для фейсбука
          if($i > 0) 
            $msg['message']['text'] = "{result=p_$i:$.*}" . $msg['text'];

          $batch['p_' . $i++] = $fb->request('POST', '/me/messages', $msg);
}
$fb->sendBatchRequest($batch);

and everything works again. And the sequence works. only here in the messages, of course, the inserted bilebird arrives. Can you tell me where to hide it ... if I'm moving in the right direction at all. Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question