How to send long messages with a Telegram bot using PHP?

Hey folks, I’m struggling with my Telegram bot. It was working fine for short messages, but now I’m trying to send longer texts from my database and it’s not cooperating. I keep getting a HTTP 400 Bad Request error.

I’ve tried using file_get_contents(), but no luck. Here’s a snippet of what I’m working with:

function sendMessage($chatID, $message) {
    $url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/sendMessage';
    $params = [
        'chat_id' => $chatID,
        'text' => $message
    ];
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    curl_close($ch);
    
    return $result;
}

Any ideas on how to fix this? I’m pretty new to PHP and Telegram bots, so any help would be awesome. Thanks!

hey ethan, i’ve dealt with this before. telegram has a message length limit (4096 chars). try splitting ur long messages into chunks. use mb_substr() to break it up and send each part separately. that should fix ur issue without changing much code. good luck!

I encountered a similar issue when working on a Telegram bot project. The solution lies in message chunking. Telegram’s API has a 4096 character limit per message, so for longer texts you need to split your message into smaller parts. Below is an example modification of your function:

function sendMessage($chatID, $message) {
    $maxLength = 4000;
    $messages = str_split($message, $maxLength);
    
    foreach ($messages as $part) {
        $url = 'https://api.telegram.org/bot' . BOT_TOKEN . '/sendMessage';
        $params = [
            'chat_id' => $chatID,
            'text' => $part
        ];
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $result = curl_exec($ch);
        curl_close($ch);
    }
    
    return $result;
}

This approach should resolve your HTTP 400 error by effectively handling long messages in chunks.

I’ve been in your shoes, Ethan. When I first started with Telegram bots, the character limit caught me off guard too. Here’s what worked for me: Instead of sending one big message, I implemented a function to split long texts into smaller chunks. It’s not just about using str_split() though - you want to make sure you’re not cutting words in half. I created a custom function that respects word boundaries. It splits the message into parts, each under 4000 characters, and sends them sequentially. This approach has been rock-solid for me, handling everything from short updates to lengthy database extracts. One tip: add a slight delay between sending each chunk to avoid hitting Telegram’s rate limits. A sleep(1) between sends should do the trick. Remember, when dealing with APIs, it’s always good to check their documentation for limitations like this. It can save you a lot of headaches down the road.