Telegram php example send message

user3537042 picture user3537042 · Jul 24, 2015 · Viewed 75.5k times · Source

I can't find an example of sending message by telegram protocol from php. I tried to use this but failed. Can you give me any examples?

Answer

Mauro Delrio picture Mauro Delrio · Sep 30, 2015

I use the following function:

function sendMessage($chatID, $messaggio, $token) {
    echo "sending message to " . $chatID . "\n";

    $url = "https://api.telegram.org/bot" . $token . "/sendMessage?chat_id=" . $chatID;
    $url = $url . "&text=" . urlencode($messaggio);
    $ch = curl_init();
    $optArray = array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true
    );
    curl_setopt_array($ch, $optArray);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

and you call in this way

$token = "<insert bot token here>";
$chatid = "<chatID>";
sendMessage($chatid, "Hello World", $token);