Send message to a Telegram channel using PHP

Do you know How to send messages to a Telegram channel using PHP?

Today we are going to make a telegram bot that will send messages to a telegram channel using PHP.

In order to be able to do so, you will have first to

  • Create a Telegram Channel (public now)
  • Create  a telegram bot using botfather
  • Set the bot as an administrator in your channel

Creating your telegram bot

If you don’t know how to create a telegram bot using bot father. please check this video, you can watch it from creating a telegram bot.

Now you can able to send message to message to your telegram channel by HTTP GET method by using the following link

https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHANNEL_NAME]&text=[MY_MESSAGE_TEXT]
  • [BOT_API_KEY] – Replace with your API Key generated by BotFather when you created your bot
  • [MY_CHANNEL_NAME] –  the handle of your channel (e.g. @yourchannelname)
  • [MY_MESSAGE_TEXT] – is the message you want to send

 

So let’s make message to Telegram channel PHP

$apiToken = "my_bot_api_token";

$data = [
    'chat_id' => '@yourchannelname',
    'text' => 'Hello world!'
];

$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) );
// Do what you want with result

How to send messages to telegram channel using HTML form and PHP 

Here you can find the code which I used to send a text message to the telegram channel using PHP.
First I created an HTML form and on the same file, I wrote the telegram API.

This is the easiest method you can find on the internet today.




<form action="" method="post">
    <input type="text" name="message">
    <input type="submit" name="submit">
</form>


<?php
    if(isset($_POST['submit']))
    {
        $apiToken = "******";
        $data = [
            'chat_id' => '@*****', 
            'text' => $_POST['message']
        ];
        $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data) );	
    }
?>

How to send images in telegram Bot using PHP

A few people from my youtube channel ask me how to send images to the telegram channels using Telegram PHP API. here it is.
You can use this code to send images to the telegram channel using PHP



$apiToken = "my_bot_api_token";
$data = [
    'chat_id' => '@yourchannelname',
    'photo' => 'http://yourimageurl.com/image.jpg'  //replace with your image url
];
$response = file_get_contents("https://api.telegram.org/bot$apiToken/sendPhoto?" . http_build_query($data) );
// Do what you want with result

 

 

Download Source Code : 

 

 

Checkout more awesome tutorials

 

What is Telegram Bots

Bots are just software-controlled Telegram accounts that frequently incorporate AI features. Anything is possible for them, including teaching, playing, searching, broadcasting, reminding, connecting, integrating with other services, and even sending commands to the Internet of Things.

Is telegram Provides PHP API

Yes we are using telegram http API to work with PHP

Can we send messages from a website to telegram channel

Yes. possible. i am explaining all details in this blog

Is telegram bot API is free

Yes telegram API is 100% Free to use

Is telegram bot is illigal

No. Telegram Bots are completely legal. But your purpose of using a bot define that.

Does this Telegram PHP API has any Limits

No. You can send unlimited Messages with Telegram PHP API

Is possible to send images using Telegram PHP API

Yes it is possible to send Images using Telegram PHP API and you can find the code here

Post Your Questions on our forum

Post a question on Forum

Ajith Jojo Joseph

Self taught, dedicated young entrepreneur with many licensed products under his sleeve. Passionate about technology, business and excellence in general.

Share with your friends:

Comments are closed.

How to integrate Paypal API in Laravel

Are you looking to integrate Paypal API in your Laravel project for seamless payment processing? Look no further! In this […]

April 3, 2024

How to integrate Razorpay API in Laravel

Integrating payment gateways into web applications has become an essential part of e-commerce websites. In this tutorial, we will discuss […]

April 3, 2024

Laravel 11 Ajax CRUD Operation Tutorial Example

**Mastering CRUD Operations with Laravel 11 Ajax: A Comprehensive Tutorial** In the world of web development, interaction between the front-end […]

April 3, 2024

Login as Client in Laravel – Login with user id

**Unlock the Power of Laravel with Login as Client – Login with User ID** Laravel, the popular PHP framework, offers […]

April 3, 2024

Digital Marketing Toolkit

Get Free Access to Digital Marketing Toolkit. You can use all our tools without any limits

Get Free Access Now