Messages, Editing, Pinning & Rich Text Formatting

Raw MD
Complete guide to sendMessage, editMessageText, deleteMessage, pinChatMessage, copyMessage, and full HTML / MarkdownV2 rich typography.

Telegram Message Lifecycle Overview

Every interaction in Telegram begins with message delivery. Beyond standard text, Telegram bots have complete programmatic control over editing existing bubbles, deleting spam, pinning announcements, forwarding/copying messages, and styling typography with rich HTML and MarkdownV2 entities.

1. Sending Messages & Link Preview Options (sendMessage)

In modern Telegram Bot API (v7.0+ / 8.x), link previews are controlled via the dedicated link_preview_options object rather than deprecated boolean flags:

PHP — sendMessage with Advanced Options
<?php
// ── Botlify Cloud .env — auto-injected, never hardcode secrets ──
$BOT_TOKEN     = getenv('BOT_TOKEN');     // Telegram bot token from @BotFather
$ADMIN_CHAT_ID = getenv('ADMIN_CHAT_ID'); // Your numeric admin Telegram user ID
$BOT_USERNAME  = getenv('BOT_USERNAME');  // Bot username without @

declare(strict_types=1);

bot('sendMessage', [
    'chat_id'              => $chatId,
    'text'                 => "🚀 Botlify Cloud v2.5 Released!\n\nCheck out our high-speed SQLite WAL engine at https://botlify.cloud\n\nEnjoy zero-config deployment!",
    'parse_mode'           => 'HTML',
    'protect_content'      => false, // Set true to forbid forwarding & screenshots
    'link_preview_options' => [
        'is_disabled'       => false,
        'url'               => 'https://botlify.cloud',
        'prefer_large_media'=> true,
        'show_above_text'   => false // Place preview thumbnail below text
    ],
    'reply_parameters'     => [
        'message_id' => $incomingMessageId // Quote user message
    ]
]);

2. Comprehensive HTML Formatting Reference

Botlify Cloud strongly recommends parse_mode => 'HTML' for all bot messages because HTML tags can be safely escaped with PHP's native htmlspecialchars($var, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') without risk of syntax breaks.

Formatting Style HTML Tag Syntax Visual Output / Behavior
Bold <b>bold text</b> or <strong>text</strong> bold text
Italic <i>italic text</i> or <em>text</em> italic text
Underline <u>underlined text</u> or <ins>text</ins> underlined text
Strikethrough <s>strikethrough</s> or <del>text</del> strikethrough
Spoiler <tg-spoiler>secret</tg-spoiler> Blurs text until user taps to reveal
Inline Code <code>$balance = 50;</code> Monospaced inline snippet (one-tap copy in Telegram)
Code Block <pre><code class="language-php">code</code></pre> Multi-line monospaced block with syntax label
Blockquote <blockquote>Quoted note</blockquote> Indented quotation with accent bar
Expandable Quote <blockquote expandable>Long text...</blockquote> Collapsible quotation with "Show More" accordion (API 7.3+)
Hyperlink <a href="https://botlify.cloud">Visit Site</a> Clickable web link
Mention by User ID <a href="tg://user?id=123456789">John Doe</a> Mentions user even if they do not have a public username
Custom Emoji <tg-emoji emoji-id="5368324170671202286">🔥</tg-emoji> Renders verified Telegram Premium custom emoji (API 6.2+)
PHP — Master Rich HTML Typography Example
<?php
// ── Botlify Cloud .env — auto-injected, never hardcode secrets ──
$BOT_TOKEN     = getenv('BOT_TOKEN');     // Telegram bot token from @BotFather
$ADMIN_CHAT_ID = getenv('ADMIN_CHAT_ID'); // Your numeric admin Telegram user ID
$BOT_USERNAME  = getenv('BOT_USERNAME');  // Bot username without @

declare(strict_types=1);

$richHtml = <<<'RAW_HTML'
⚡ Botlify Cloud Server Report

Server Status: Operational Uptime: 99.98% | Load: 0.12, 0.08, 0.04
Detailed Diagnostic Metrics: • Database: SQLite 3 WAL Mode (-32MB cache) • Peak Response Time: 18ms • Active Webhook Sockets: 4,120 • SSL Handshake: TLSv1.3 Perfect Forward Secrecy
Your security key is: SEC-9842-ALPHA-77 To update your runtime settings, run: php api/v1 --restart

{
  "status": "healthy",
  "engine": "Botlify Cloud 20ms",
  "mcp_ready": true
}
Need help? Contact Platform Support or browse Documentation. RAW_HTML; bot('sendMessage', [ 'chat_id' => $chatId, 'text' => $richHtml, 'parse_mode' => 'HTML' ]);

3. Editing Messages & Captions Dynamically

Rather than sending a flood of new messages, update existing bubbles in place to create smooth, app-like interfaces:

PHP — editMessageText & editMessageCaption
<?php
// ── Botlify Cloud .env — auto-injected, never hardcode secrets ──
$BOT_TOKEN     = getenv('BOT_TOKEN');     // Telegram bot token from @BotFather
$ADMIN_CHAT_ID = getenv('ADMIN_CHAT_ID'); // Your numeric admin Telegram user ID
$BOT_USERNAME  = getenv('BOT_USERNAME');  // Bot username without @

declare(strict_types=1);

// 1. Edit Text Message
function update_chat_screen(string|int $chatId, int $messageId, string $newHtml, array $newKeyboard = []): void {
    bot('editMessageText', [
        'chat_id'      => $chatId,
        'message_id'   => $messageId,
        'text'         => $newHtml,
        'parse_mode'   => 'HTML',
        'reply_markup' => !empty($newKeyboard) ? $newKeyboard : null
    ]);
}

// 2. Edit Media Caption (for Photos, Videos, Documents)
function update_photo_caption(string|int $chatId, int $messageId, string $newCaption): void {
    bot('editMessageCaption', [
        'chat_id'    => $chatId,
        'message_id' => $messageId,
        'caption'    => $newCaption,
        'parse_mode' => 'HTML'
    ]);
}

// 3. Edit Only the Inline Keyboard Below a Message
function update_buttons_only(string|int $chatId, int $messageId, array $keyboard): void {
    bot('editMessageReplyMarkup', [
        'chat_id'      => $chatId,
        'message_id'   => $messageId,
        'reply_markup' => $keyboard
    ]);
}

4. Deleting, Pinning & Copying Messages

PHP — Deleting, Pinning & copyMessage
<?php
// ── Botlify Cloud .env — auto-injected, never hardcode secrets ──
$BOT_TOKEN     = getenv('BOT_TOKEN');     // Telegram bot token from @BotFather
$ADMIN_CHAT_ID = getenv('ADMIN_CHAT_ID'); // Your numeric admin Telegram user ID
$BOT_USERNAME  = getenv('BOT_USERNAME');  // Bot username without @

declare(strict_types=1);

// 1. Delete a Single Message (Clean up command /start or spam)
bot('deleteMessage', [
    'chat_id'    => $chatId,
    'message_id' => $incomingMessageId
]);

// 2. Batch Delete Multiple Messages (API 7.0+)
bot('deleteMessages', [
    'chat_id'     => $chatId,
    'message_ids' => [$msgId1, $msgId2, $msgId3]
]);

// 3. Pin Message as Announcement
bot('pinChatMessage', [
    'chat_id'              => $chatId,
    'message_id'           => $announcementMsgId,
    'disable_notification' => false // Send push notification to all members
]);

// 4. Unpin Messages
bot('unpinChatMessage', ['chat_id' => $chatId, 'message_id' => $announcementMsgId]);
bot('unpinAllChatMessages', ['chat_id' => $chatId]);

// 5. copyMessage (Superior to forwardMessage: strips author header & customizes caption)
bot('copyMessage', [
    'chat_id'      => $targetChatId,
    'from_chat_id' => $sourceChatId,
    'message_id'   => $sourceMsgId,
    'caption'      => '📢 Official Broadcast via Botlify Cloud',
    'parse_mode'   => 'HTML'
]);

5. Realistic Bot UX with Chat Actions (sendChatAction)

When performing heavy tasks (such as AI generation, downloading files, or complex SQLite queries), tell the user your bot is working by showing dynamic status indicators in the Telegram header:

Action Name Displayed Header Status Ideal Use Case
typing "is typing..." Waiting for AI responses, text formatting, or database lookups.
upload_photo "is sending a photo..." Generating or fetching high-resolution images.
record_video / upload_video "is sending a video..." Processing or uploading MP4 video files.
record_voice / upload_voice "is sending audio..." Synthesizing text-to-speech voice memos.
upload_document "is sending a file..." Compiling ZIP archives or generating PDF reports.
choose_sticker "is choosing a sticker..." Selecting animated or reaction stickers.
record_video_note "is sending a video message..." Processing circular round video notes.
PHP — sendChatAction Implementation
<?php
// ── Botlify Cloud .env — auto-injected, never hardcode secrets ──
$BOT_TOKEN     = getenv('BOT_TOKEN');     // Telegram bot token from @BotFather
$ADMIN_CHAT_ID = getenv('ADMIN_CHAT_ID'); // Your numeric admin Telegram user ID
$BOT_USERNAME  = getenv('BOT_USERNAME');  // Bot username without @

declare(strict_types=1);

// Send typing indicator while generating response
bot('sendChatAction', [
    'chat_id' => $chatId,
    'action'  => 'typing'
]);

// In forum topics, pass message_thread_id
if (!empty($threadId)) {
    bot('sendChatAction', [
        'chat_id'           => $chatId,
        'message_thread_id' => $threadId,
        'action'            => 'upload_document'
    ]);
}

6. Real-Time Streaming Message Drafts (sendMessageDraft)

Telegram allows AI bots to stream partial message drafts in real time to the user while content is being generated:

PHP — Streaming Message Drafts
<?php
// ── Botlify Cloud .env — auto-injected, never hardcode secrets ──
$BOT_TOKEN     = getenv('BOT_TOKEN');     // Telegram bot token from @BotFather
$ADMIN_CHAT_ID = getenv('ADMIN_CHAT_ID'); // Your numeric admin Telegram user ID
$BOT_USERNAME  = getenv('BOT_USERNAME');  // Bot username without @

declare(strict_types=1);

function stream_ai_draft(string|int $chatId, string $partialText, bool $canStop = true): void {
    bot('sendMessageDraft', [
        'chat_id'      => $chatId,
        'text'         => $partialText,
        'can_stop'     => $canStop,      // Displays native "Stop Generating" button
        'keep_on_stop' => true           // Keeps text generated so far if user stops
    ]);
}

// Handling Stopped Generation Update
if (isset($update['stopped_message_generation'])) {
    $stopData = $update['stopped_message_generation'];
    $userId = $stopData['user']['id'];
    error_log("User {$userId} aborted AI text generation.");
    http_response_code(200);
    exit;
}
Botlify Cloud Runtime
Credentials are auto-injected in .env — always load them with getenv(): BOT_TOKEN, ADMIN_CHAT_ID, BOT_USERNAME. Main database: database.db (SQLite, WAL mode). Runtime errors: error.log. Quick test: send to $ADMIN_CHAT_ID. Full details: Runtime Reference.