Sending Telegram messages using PHP

Fire away those messages with this simple script!

First of all, create bot at @botfather by searching botfather and send him the following command:

/newbot

Fill in the long name and user name for the bot when @botfather is prompting for this.

You will now get back a Bot ID and Hash.
Save this ID and Hash in your PHP file.

$telegrambot='<Bot ID>:<Bot Hash>';

Example:
$telegrambot='7345345735:agADFsfjdfhSGTYJdfafhksdsdrQ';

In telegram send your newly create bot a message and go and fetch the Chat_ID

Browse to the following page using your own telegrambot values:
Please note the part where it says bot in front of your Bot ID!

https://api.telegram.org/bot7345345735:agADFsfjdfhSGTYJdfafhksdsdrQ/getUpdates

Search for the part “Chat” and look for the “ID“.
Place this ID underneath your $telegrambot value in your PHP file.

$telegramchatid=<Chat ID>;

Example:
$telegramchatid=374659345;

Now go and create a page with the current function and code.

<?php
// Telegram function which you can call
function telegram($msg) {
        global $telegrambot,$telegramchatid;
        $url='https://api.telegram.org/bot'.$telegrambot.'/sendMessage';$data=array('chat_id'=>$telegramchatid,'text'=>$msg);
        $options=array('http'=>array('method'=>'POST','header'=>"Content-Type:application/x-www-form-urlencoded\r\n",'content'=>http_build_query($data),),);
        $context=stream_context_create($options);
        $result=file_get_contents($url,false,$context);
        return $result;
}

// Set your Bot ID and Chat ID.
$telegrambot='7345345735:agADFsfjdfhSGTYJdfafhksdsdrQ';
$telegramchatid=374659345;

// Function call with your own text or variable
telegram ("Here is your message!!");
?>

In the end its just 3 lines of code with a small function.
You can place the Bot ID and Chat ID in a config.php file and place the function in a seperate functions.php file. This way you can call this function from all your pages, create API’s and so forth…

When you are done you can message the botfather again to customize your bot with his own profile picture, description, etc.

Good luck!

15 thoughts on “Sending Telegram messages using PHP

  1. after using “/newbot” in telegram there is no : in replay, it just give me the http api !

  2. I need to send messages to my all users that are more than 45
    And telegram avoid it, i heard we should use curl
    If you write the code and send it to my email
    I ll thank you
    Morata7mostafa@gmail.com

    1. Do you need to send them separately otherwise you can create a group where you can just add people into and the bot as well.
      Otherwise you can also create a loop using curl. The thing is that you need to have a chatid as bots cannot initiate a chat.
      In that case you should also add a webhook where people can register with a command.

  3. i want to create a bot in telegram for sent a message to all that in my channel telegram i want to create a system that provide real eastate the system use for alert to customer for paying money at monthly how it process please help me i don’t want all customer use cmd i want sent all to customer that have contact in database

    1. Hi,

      If you create a group where you let your users join together with your bot you can broadcast inside the channel.
      If you want to message users individually you need to let them registered their ChatID in a database so you can reply back.
      You can do this using a webhook which is explained here

  4. please help me i run this code with my oqn bot token and chat id. but i see that i dont receive any message . this code is not wotking in my localhost

    showing eror

    Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known. in sendmsg.php on line 26

    Warning: file_get_contents(https://api.telegram.org/bot1340297013:AAFcIh36vJEL6Njy3WjV_YgJdwbPTYznYMQ/sendMessage): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in sendmsg.php on line 26

    my code in line 26
    $result=file_get_contents($url,false,$context);

Leave a Reply

Your email address will not be published.