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:


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>';


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!

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

$telegramchatid=<Chat ID>;


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

// Telegram function which you can call
function telegram($msg) {
        global $telegrambot,$telegramchatid;
        return $result;

// Set your Bot ID and Chat ID.

// 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!

23 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

    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( 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

  5. hello, please i have a php mailer, i want to receive form input in my telegram bot but i only get this string “Here is your message!!”, help please

    1. You should edit the “Here is your message” variable with the $_POST variables you want to use.
      Like: “Hi $_POST[‘firstname’], here is your message”. Just make sure you have a field in your form with the name “firstname”.

  6. buenos dias como podria ejecutar desde Telegram archivo php o callback ?

  7. Hi, any idea how we can modify this to send NOT TO A TELEGRAM CHAT GROUP but to the user cell phone / obile phone

    Currently via phpmailer, I can send email reminders to users directly ( we have their email addresses and mobile numbers in our database ), so i would now like to send them Telegram reminders as well

    1. Hi Marco,

      As written in your other comment it is not possible to initiate a chat in telegram with another user from the bot’s side.
      The user needs to start the chat so you have a chatid where you can post to.

Leave a Reply

Your email address will not be published.