Search by

toropyga / telegram

Toropyga

Libraries for working with Telegram bot from Toropyga

Package info

github.com/Toropyga/Telegram

pkg:composer/toropyga/telegram

Statistics

Installs: 144

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v3.2.0 2026-09-14 12:12 UTC

This package is auto-updated.

Last update: 2026-09-14 12:20:32 UTC


README

Sending messages on behalf of a Telegram bot

License Version PHP

Content

General description

PHP library for sending Telegram bot messages, media files, and webhook requests through the Telegram Bot API.

To work, you need PHP version 8.1 or higher and the json, curl, and dom extensions. The package also uses Base and NetContent.

The Telegram class remains the backward-compatible facade. Its responsibilities are split into TelegramMessageSender, TelegramWebhook, TelegramTextConverter, and TelegramPayloadBuilder.

Installation

Install the package with Composer:

composer require toropyga/telegram

Configuration

Default values can be supplied to the constructor or configured with constants declared before creating the class:

Work description

Including the class

require_once("NetContent.php");
require_once("Base.php");
require_once("Telegram.php");

or use composer

require_once("vendor/autoload.php");

Initialising the class

$token = "000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$chat_id = 000000000;

$net = new Toropyga\NetContent();
$telegram = new Toropyga\Telegram($net, $token, $chat_id);

or with automatic connection of the NetContent class

$telegram = new Toropyga\Telegram(null, $token, $chat_id);

or use constants configured before creating the class

$telegram = new Toropyga\Telegram();

Setting the method of interaction with the Telegram API when sending messages

$telegram->setMethod('sendVideo');

Supported methods

Setting the parse mode

$telegram->setParseMode('HTML');

Supported values are HTML, Markdown, MarkdownV2, and an empty string for plain text.

Converting text markup

The class provides static helpers for converting between HTML, Telegram Markdown, and MarkdownV2:

$markdown = Toropyga\Telegram::htmlToMarkdown('<strong>Hello</strong>');
$markdown_v2 = Toropyga\Telegram::htmlToMarkdownV2('<strong>Hello</strong>');
$html = Toropyga\Telegram::markdownToHtml('*Hello*');
$markdown_v2 = Toropyga\Telegram::markdownToMarkdownV2('*Hello*');
$markdown = Toropyga\Telegram::markdownV2ToMarkdown('*Hello*');

For custom conversions, use convertText($text, $from, $to). Supported formats are html, markdown, markdownv2, and plain.

Setting the bot token

$telegram->setToken("000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

Configuring the webhook

$webhook_url = 'https://your_server_path'; // Webhook server URL
$certificate = '/home/user/ssl/my_ssl'; // optional certificate path
$ip_address = '000.000.000.000'; // optional fixed IP address
$max_connections = 40; // maximum simultaneous connections [1-100]

$telegram->setWebhook ($webhook_url, $certificate, $ip_address, $max_connections);

Removing the webhook

$telegram->deleteWebhook();

Getting webhook information

$info = $telegram->getWebhookInfo();

Setting the recipient chat ID

$telegram->setChatID("000000000");

Enabling debug logging

$telegram->setDebug(true);

When enabled, debug messages are stored in the internal log. Do not log bot tokens in production.

Getting logs

$telegram->getLogs();

Returns the event log and configured log file name.

Sending a text message

$message = "<strong>Hi!</strong>";
$token = "000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Telegram bot token
$chat_id = 000000000; // recipient chat ID

$telegram->setMethod('sendMessage');
$telegram->setParseMode('HTML');

$telegram->setToken($token);
$telegram->setChatID($chat_id);
if (!$answer = $telegram->sendToTelegram($message)) Base::dump($telegram->getLogs());
else Base::dump($answer);

Sending a message with the configured default parameters:

$message = "Hi!";

if (!$answer = $telegram->sendToTelegram($message)) Base::dump($telegram->getLogs());
else Base::dump($answer);

Sending a file

$token = "000000000:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Telegram bot token
$chat_id = 000000000; // recipient chat ID
$reply_message_id = 0000; // message to reply to, default: 0
$caption = "It's my photo!";
$path_to_photo = "/home/images/my.jpeg";

$telegram->setDebug(true);
$telegram->setToken($token);
$telegram->setChatID($chat_id);
$telegram->setMethod('sendPhoto');
if (!$answer = $telegram->sendToTelegram($caption, $path_to_photo, $reply_message_id)) Base::dump($telegram->getLogs());
else Base::dump($answer);

Sending a document to a user:

$chat_id = 111111111; // recipient chat ID
$caption = "Report";
$path_to_document = "/home/documents/report.pdf";

$telegram->setMethod('sendDocument');
if (!$answer = $telegram->sendToTelegram($caption, $path_to_document, 0, $chat_id)) Base::dump($telegram->getLogs());
else Base::dump($answer);

Sending a photo with the configured default parameters:

$caption = "It's my photo!";
$path_to_photo = "/home/images/my.jpeg";

$telegram->setMethod('sendPhoto');
if (!$answer = $telegram->sendToTelegram($caption, $path_to_photo)) Base::dump($telegram->getLogs());
else Base::dump($answer);