M/s VIKASH TECH - We Write Imaginations to Codes..!!

How to send message via Telegram Bot using PHP – Telegram API

telegram bot

Telegram is a great platform for chat and communication purposes. Having a large community and large user base, it is one of the best places to work & use. Being an open-source chatting app, it can be used in multiple ways. It can be used for chatting, internal organization notification purposes, a custom application, and many more.

Telegram can be used to send notifications using its own Telegram API. Here we are going to discuss how we can send messages using a Telegram bot, using PHP and Telegram API.

The official documentation for using Telegram API can be found here

To send a message using Telegram, you need to get a Bot first!

To create your first Bot, follow the steps below:

  • Open Telegram App and search for BotFather – https://t.me/BotFather
  • Send a message /newbot
  • Provide a name to your bot
  • Choose a username for your bot. Make sure to provide a unique name add the word “Bot” must be at the end of the username. Eg – VIKASHTECH_bot
  • Once done you will be provided with a token to access the bot via API copy it and keep it safe, it will be required in future steps.
Telegram Bot

Once. the Bot is created, you can further use the BotFather bot to modify its details, like icon, description, etc.

Now, coming to the main part.
To send a message using a Telegram Bot, using API, you should have a chat ID of the receiver. To get the chat ID, you need to search for a bot named IDBot – https://t.me/myidbot

Now send /getid to receive your chat ID. This Chat ID will be used to send messages using Telegram Bot.

Coming to the code

Check the write the below code in your favorite text editor and save it as a PHP file on your local/global server. Below is a simple set of codes to send a “Hello world” message to a specific chat ID.

<?php
$botToken = "<your bot token that you got from BotFather>";
$chatId = "<Your chat ID, which you got from IDBot>";
$message = "Hello World";
$data = array(
        'chat_id' => urlencode($chatId),
        'text' => $message,
        'parse_mode'=>'markdown'
    );

$url = "https://api.telegram.org/bot".$botToken."/sendMessage";
  $ch = curl_init();
  //  set the url
  curl_setopt($ch, CURLOPT_URL, $url);
  //  number of POST vars
  curl_setopt($ch, CURLOPT_POST, count($data));
  //  POST data
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  //  To display result of curl
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  //  execute post
  $result = curl_exec($ch);
  //print_r($result);
  //  close connection
  curl_close($ch);
?>

Now when you run the above code, you will receive a message Hello World on your Telegram.

You can use the above way to create multiple alert systems/ notification systems, applications where you can automate notifications using CRON/Schedulers on your server.

The bot can be used to send notifications to groups, and channels too. To know how, comment below!
If in case you want help, write down your queries in the comment below! or connect with us here

Follow us on Instagram, Facebook

Subscribe to our Telegram Channel here

Category :

Comments

No comments found.

Leave a Reply

….

FLAT 50% OFF

Latest Posts

telegram bot

How to send message via Telegram Bot using PHP - Telegram API

Telegram is a great platform for chat and communication purposes. Having a large community and large user base, it is…

SuiteCRM, an open source tool for startups to use from an early stage.

When we talk about businesses in their early stage, it's quite a big deal to choose good management and sales…

How to Implement Google QAuth Authentication | Google Login - PHP MySQL

PHP Google OAuth API allows users to login into a website with their Google credentials. A user with a Google…

How to use Google Two Factor Authentication in your project?

At our end we take security at a priority, implementation of different ways to secure website should be the priority…

%d bloggers like this: