toropyga / telegram
Libraries for working with Telegram bot from Toropyga
Requires
- php: >=8.1.0
- ext-curl: *
- ext-dom: *
- ext-json: *
- toropyga/base: ^2.0.0
- toropyga/netcontent: ^5.0.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Sending messages on behalf of a Telegram bot
Content
- General description
- Installation
- Configuration
- Work description
- Including the class
- Initialising the class
- Setting the method of interaction with the Telegram API when sending messages
- Setting the parse mode
- Converting text markup
- Setting the bot token
- Configuring the webhook
- Removing the webhook
- Getting webhook information
- Setting the recipient chat ID
- Enabling debug logging
- Getting logs
- Sending a text message
- Sending a file
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:
TELEGRAM_TOKEN- Telegram bot token;TELEGRAM_ID- recipient chat ID;TELEGRAM_DEBUG- enable or disable debug logging;TELEGRAM_LOG_NAME- log file name.
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
- 'sendMessage' - send a text message;
- 'sendPhoto' - send a photo or image;
- 'sendDocument' - send a document;
- 'sendVideo' - send a video;
- 'sendAudio' - send an audio file;
- 'sendVoice' - send a voice message;
- 'sendAnimation' - send an animation.
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);