<?php

function sendTelegram($token, $chat_id, $message)
{
    // pastikan chat_id selalu array
    if (!is_array($chat_id)) {
        $chat_id = [$chat_id];
    }

    $results = [];

    foreach ($chat_id as $id) {
        $url = "https://api.telegram.org/bot{$token}/sendMessage";

        $data = [
            'chat_id'    => $id,
            'text'       => $message,
            'parse_mode' => 'Markdown'
        ];

        $ch = curl_init($url);
        curl_setopt_array($ch, [
            CURLOPT_POST           => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS     => $data,
            CURLOPT_TIMEOUT        => 10,
        ]);

        $response = curl_exec($ch);

        if ($response === false) {
            $error = curl_error($ch);
            curl_close($ch);
            $results[$id] = [
                'ok'    => false,
                'error' => $error
            ];
        } else {
            curl_close($ch);
            $results[$id] = json_decode($response, true);
        }
    }

    return $results; // kembalikan hasil untuk tiap chat_id
}
