Telegram: Send Message using PHP

To send a message to a Telegram channel using PHP, you need to know a Telegram API token and a chat ID, that you will get when you create a Telegram bot.

This note shows the PHP code examples for sending messages, images and other data types to the Telegram channel through API.

Cool Tip: How to send a message to a Telegram channel using Python! Read more →

Send Message to Telegram using PHP

To send a message to the Telegram channel use the following PHP script example:

<?php
  $apiToken = "5082654068:AAF7quCLZ4xuTq2FBdo3POssdJsM_FRHwTs";
  $data = [
      'chat_id' => '515382482',
      'text' => 'Hello from PHP!'
  ];
  $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" .
                                 http_build_query($data) );
?>

You can create a telegram-send-message.php script with the code above and run it from a command line as follows:

$ php telegram-send-message.php

In case of the error below, look at your php.ini, search for extension=openssl and if it is disabled (commented with a semi-colon), enable it and restart a web server if you use one:

Warning: file_get_contents(): Unable to find the wrapper “https” – did you forget to enable it when you configured PHP? in <path> on line 9
Warning: file_get_contents(https://api.telegram.org/…): failed to open stream: No such file or directory in <path> on line 9

Cool Tip: How to find the location of the php.ini file! Read more →

To send messages to the Telegram channel through a web form, you can create an HTML page with the PHP code as follows:

<?php
  if(isset($_POST['submitButton'])){
  $apiToken = "5082654068:AAF7quCLZ4xuTq2FBdo3POssdJsM_FRHwTs";
  $data = [
    'chat_id' => '515382482', 
    'text' => $_POST['inputMessage']
  ];
  $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" .
                                 http_build_query($data) );
  }    
?>

<html>
<body>   
<form action="" method="post">
  <input type="text" name="inputMessage"/>
  <input type="submit" name="submitButton"/>
</form>   
</body>
</html>

To play with this web form locally, you can create the index.php file with the code above and execute this command within the same folder to start the built-in development server:

$ php -S localhost:8000
- sample output -
[Fri Jan 1 01:48:43 2033] PHP 7.4.3 Development Server (http://localhost:8000) started

Then open your web browser and go to http://localhost:8000, which will show you the web form for sending the messages to the Telegram channel.

You can also send images, video, audio, documents, etc. to the Telegram channel through API using PHP.

For example, to send an image to the Telegram channel, use the PHP code as follows:

<?php
  $apiToken = "5082654068:AAF7quCLZ4xuTq2FBdo3POssdJsM_FRHwTs";
  $data = [
      'chat_id' => '515382482',
      'photo' => 'https://en.wikipedia.org/static/images/project-logos/enwiki.png'
  ];
  $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendPhoto?" .
                                 http_build_query($data) );
?>

Cool Tip: How to run a PHP script from the command line! Read more →

One Reply to “Telegram: Send Message using PHP”

  1. what if it is multiple input like name email gender and message

Leave a Reply