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!

4 thoughts on “Sending Telegram messages using PHP

Leave a Reply

Your email address will not be published.