Skip to content

Instantly share code, notes, and snippets.

@SawyerHood
Last active July 23, 2016 22:52
Show Gist options
  • Save SawyerHood/892a993346360b7e66f45384edf6576c to your computer and use it in GitHub Desktop.
Save SawyerHood/892a993346360b7e66f45384edf6576c to your computer and use it in GitHub Desktop.
async function send_msg_batch(Traversable<int> $targets, array $msg_body): Awaitable<void> {
$chunks = new Vector(array_chunk($targets, 50));
$to_process = $chunks->map($sub_targets ==> send_msg_batch_helper($sub_targets, $msg_body));
await \HH\Asio\v($to_process);
}
async function send_msg_batch_helper(Traversable<int> $targets, array $msg_body): Awaitable<void> {
$data = ['batch' => []];
foreach ($targets as $target) {
$data['batch'][] = [
'method' => 'POST',
'relative_url' => 'me/messages',
'body' => http_build_query([
'recipient' => ['id' => $target],
'message' => $msg_body
])
];
}
$json_data = json_encode($data);
$headers = [];
$headers[] = 'Content-Type: application/json';
$ch = curl_init('https://graph.facebook.com/v2.6' . "?access_token=" . $_ENV['CHAT_FB_TOKEN']);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
await \HH\Asio\curl_exec($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment