Skip to the content.

Telegram Message Bot API

Telegram is a messaging and chat application. Telegram lets you send and receive messages from your users. The Telegram Bot API lets you build applications that interact with Telegram bots and sends any message to any user at any time.

1. Create Bot

2. Add Bot to a Group

3. Send Message with PHP

<?php

function TelegramSendMessage($TOKEN, $GROUPNAME, $MESSAGE)
{
  $curl = curl_init();

  curl_setopt_array($curl, [
    CURLOPT_URL => "https://api.telegram.org/bot{$TOKEN}/sendMessage",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode([
      'text' => "{$MESSAGE}",
      'disable_web_page_preview' => false,
      'disable_notification' => false,
      'reply_to_message_id' => null,
      'chat_id' => "{$GROUPNAME}"
    ]),
    CURLOPT_HTTPHEADER => [
      "accept: application/json",
      "content-type: application/json"
    ],
  ]);

  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #:" . $err;
  } else {
    echo $response;
  }
}

$TOKEN = "6468124398:AAGEYxUoCMg39WVUZVwHfAumfBZj2Dxm27o";
$GROUPNAME = "@SAMPLEGROUPNAME";
$MESSAGE = "Hello World !";

TelegramSendMessage($TOKEN, $GROUPNAME, $MESSAGE);
?>

EXTRA 1: If you want to send message to a person

EXTRA 2: If you want to send message from web browser

Refs: