DEV Community

Cover image for PHP+Telegram: 5 easy ways how to send a message
Alex
Alex

Posted on • Edited on

PHP+Telegram: 5 easy ways how to send a message

In the world of online communication, Telegram stands out as a popular messaging platform. If you're a developer, it might be useful to know how to easily send a message to Telegram from the code. This article will show you 5 easy ways to do it using PHP.


Preconditions:

  • PHP installed and curl extension enabled
  • Public Telegram channel created
  • Telegram bot created using BotFather
  • Bot added to channel administrators

Telegram API

Telegram provides a variety of API endpoints and methods, which can be found in the documentation.

Today we're gonna focus on the sendMessage endpoint. It sends some text {TEXT} to the particular channel {CHANNEL_ID} by the bot {BOT_API_TOKEN}

https://api.telegram.org/bot{BOT_API_TOKEN}/sendMessage?chat_id={CHANNEL_ID}&text={TEXT}
Enter fullscreen mode Exit fullscreen mode

There are a lot of ways to call this endpoint and send a message to the channel inside a PHP project. We will cover 5 of them.


HTTP request with cURL

Functional style

<?php

$botApiToken = 'your bot api token';
$channelId ='your channel id';
$text = 'Hello, I am from PHP!';

$query = http_build_query([
    'chat_id' => $channelId,
    'text' => $text,
]);
$url = "https://api.telegram.org/bot{$botApiToken}/sendMessage?{$query}";

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
));
curl_exec($curl);
curl_close($curl);
Enter fullscreen mode Exit fullscreen mode

OOP style

Installation

composer require curl/curl
Enter fullscreen mode Exit fullscreen mode

Usage

<?php

require 'vendor/autoload.php';

$botApiToken = 'your bot api token';
$channelId ='your channel id';
$text = 'Hello, I am from PHP!';

$query = http_build_query([
    'chat_id' => $channelId,
    'text' => $text,
]);
$url = "https://api.telegram.org/bot{$botApiToken}/sendMessage?{$query}";

$curl = new \Curl\Curl();
$curl->get($url);
Enter fullscreen mode Exit fullscreen mode

One line magic using file_get_contents

<?php

$botApiToken = 'your bot api token';
$channelId = 'your channel id';
$text = 'Hello, I am from PHP file_get_contents!';
$query = http_build_query([
    'chat_id' => $channelId,
    'text' => $text,
]);
$url = "https://api.telegram.org/bot{$botApiToken}/sendMessage?{$query}";
file_get_contents($url);
Enter fullscreen mode Exit fullscreen mode

Symfony HTTP client

Symfony is one of the most popular PHP frameworks. It provides a lot of different components out of the box and one of them is HTTP Client. It allows you to make HTTP requests easily.

Installation

composer require symfony/http-client
Enter fullscreen mode Exit fullscreen mode

Standalone

<?php

require 'vendor/autoload.php';

use Symfony\Component\HttpClient\HttpClient;

$botApiToken = 'your bot api token';
$channelId ='your channel id';
$text = 'Hello, I am from Symfony HTTP Client!';

$client = HttpClient::create([
    'base_uri' => 'https://api.telegram.org',
]);

$client->request('GET', "/bot{$botApiToken}/sendMessage", [
    'query' => [
        'chat_id' => $channelId,
        'text' => $text,
    ],
]);
Enter fullscreen mode Exit fullscreen mode

Inside Symfony app

Prepare scoped client inside config/packages/framework.yaml

framework:
    http_client:
        scoped_clients:
            telegram.client:
                base_uri: 'https://api.telegram.org'
Enter fullscreen mode Exit fullscreen mode

And use it inside your service

<?php

namespace App;

use Symfony\Contracts\HttpClient\HttpClientInterface;

class YourService
{
    public function __construct(
        private readonly HttpClientInterface $telegramClient,
    )
    {
    }

    public function sendMessage(string $text): void
    {
        $botApiToken = 'your bot api token';
        $channelId ='your channel id';

        $this->telegramClient->request('GET', "/bot{$botApiToken}/sendMessage", [
            'query' => [
                'chat_id' => $channelId,
                'text' => $text,
            ],
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Symfony Telegram Notifier

Another Symfony component that can send a message to the Telegram channel is Telegram Notifier.

Installation

composer require symfony/telegram-notifier
Enter fullscreen mode Exit fullscreen mode

Standalone

<?php

require 'vendor/autoload.php';

use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransport;
use Symfony\Component\Notifier\Chatter;
use Symfony\Component\Notifier\Message\ChatMessage;

$botApiToken = 'your bot api token';
$channelId ='your channel id';

$text = 'Hello, I am from Symfony Telegram Notifier!';

$telegramTransport = new TelegramTransport($botApiToken, $channelId);
$chatter = new Chatter($telegramTransport);
$chatMessage = new ChatMessage($text);
$chatter->send($chatMessage);
Enter fullscreen mode Exit fullscreen mode

Inside Symfony app

Configure notifier chatter transport inside
config/packages/framework.yaml

parameters:
  env(TELEGRAM_DSN): 'telegram://BOT_API_TOKEN@default?channel=CHANNEL_ID'

framework:
    notifier:
        chatter_transports:
            telegram: '%env(TELEGRAM_DSN)%'
Enter fullscreen mode Exit fullscreen mode

And use it inside your service

<?php

namespace App;

use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Message\ChatMessage;

class YourService
{
    public function __construct(
        private readonly ChatterInterface $telegramChatter
    )
    {
    }

    public function sendMessage(string $text): void
    {
        $this->telegramChatter->send(new ChatMessage($text));
    }
}
Enter fullscreen mode Exit fullscreen mode

Telegram Bot SDK

Telegram Bot SDK lets you develop Telegram Bots in PHP easily! Supports Laravel framework and comes with addons to enhance your bot development experience.

Installation

composer require irazasyed/telegram-bot-sdk
Enter fullscreen mode Exit fullscreen mode

Standalone

<?php

require 'vendor/autoload.php';

use Telegram\Bot\Api;

$botApiToken = 'your bot api token';
$channelId ='your channel id';

$telegram = new Api($botApiToken);
$telegram->sendMessage([
    'chat_id' => $channelId,
    'text' => $text,
]);
Enter fullscreen mode Exit fullscreen mode

Inside Laravel app

php artisan vendor:publish --tag="telegram-config"
Enter fullscreen mode Exit fullscreen mode
<?php

namespace App;

use Telegram\Bot\Api;

class YourService
{
    public function __construct(private readonly Api $telegram)
    {
    }

    public function sendMessage(string $channelId, string $text): void
    {
        $this->telegram->sendMessage([
            'chat_id' => $channelId,
            'text' => $text,
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Summary

There are many ways to achieve the same goal. With so many options available, there's no definitive "right" way to send a Telegram message using PHP. The choice you make depends on your needs and the technologies you're working with. This article shows five simple ways to send messages to Telegram using PHP, in fact there are many more than five and it's up to you which one to use.

Top comments (4)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Well done!

Collapse
 
p1ngger profile image
Alex

Thanks a lot for the feedback!

Collapse
 
__7116f8f1 profile image
Артем Паламарчук

Can I programmatically send the message to a STRANGE user, who has no my/(my bot) contact in his contacts list?

Collapse
 
p1ngger profile image
Alex

I don't think it's possible, but I'm not sure. It should be checked with the Telegram API docs.
One of the prerequisites mentioned in the article is "Bot added to channel administrators", which means that the bot should be given write permission before sending messages.
I think Telegram does not offer the option to send messages to random people, as this can lead to uncontrolled spam. Again - I'm not sure, I haven't checked.