Sending Telegram Bot Messages with PHP cURL: Handling HTTP Status, JSON Errors, and Telegram's ok Field
Introduction When you need to send a simple message from a Telegram bot, the most direct way is to call the sendMessage method over HTTPS. Using PHP’s cURL extension gives you full control over the request, allowing you to inspect the HTTP status code, handle JSON decoding errors, and react to Telegram’s ok field. This guide shows a minimal, production‑ready helper that focuses on those checks.…
When it comes to sending a straightforward message from a Telegram bot, the most straightforward approach involves using HTTPS to call the sendMessage method. By leveraging PHP's cURL extension, developers gain complete control over the request, enabling them to examine the HTTP status code, manage JSON decoding errors, and respond to Telegram's ok field.
This article presents a concise, production-grade helper function that emphasizes these crucial checks. It does not offer a comprehensive SDK, nor does it handle webhook updates or persist state such as update_id for idempotency. These responsibilities are left to the caller or a separate service layer.
The provided code snippet presents a self-contained function that encapsulates all the necessary safety checks when invoking sendMessage. It retrieves the bot token from an environment variable, constructs the request payload, sets appropriate timeouts, inspects the HTTP response status, parses the JSON body securely, and finally verifies that Telegram returned ok:true. Any failure at these stages triggers an exception with a detailed message.
To begin, the function extracts the token from the TELEGRAM_BOT_TOKEN environment variable. If the token is missing or invalid, it raises a RuntimeException. Next, it forms the request URL using the bot token. The payload is then configured, including the chat_id, message text, and optional parameters. The function supports HTML parsing mode by escaping the user-provided text before submission.
For MarkdownV2 parsing mode, additional escaping logic would be required, which is currently unsupported. Other optional flags like disable_web_page_preview and disable_notification are also handled.
Subsequently, a cURL handle is initialized, and various cURL options are set. These include the URL, marking the request as POST, specifying the payload, enabling response retrieval, allowing for redirections up to five levels deep, setting request and connection timeouts, and configuring HTTP version and SSL verification. Following the execution of the cURL request, the response body, HTTP status code, and any cURL errors are retrieved. If cURL encounters issues, a RuntimeException is thrown with a descriptive error message.
HTTP status checks are performed next. If the HTTP status code is not within the 2xx range, an exception is raised. This ensures Telegram's ok field is verified to be true, confirming successful message delivery. The function concludes by returning the decoded JSON response from Telegram. Should any stage fail—ranging from network issues to HTTP errors, JSON parsing problems, or Telegram's ok field not being true—the function throws a targeted exception, making troubleshooting straightforward for developers.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.