The Problem:
You’re encountering issues sending a Telegram message with an inline keyboard built from a PHP array. The debug output shows the button array is correctly generated, but the message doesn’t appear in Telegram. Your text includes Persian characters encoded in UTF-8.
Understanding the “Why” (The Root Cause):
The problem likely stems from how your inline keyboard is structured and how PHP handles JSON encoding for special characters. Telegram’s API has specific requirements for the format of the reply_markup JSON object, particularly regarding the structure of inline keyboards. Sending all buttons in a single row might exceed Telegram’s display limits, or improper JSON encoding could corrupt the data and prevent the message from being sent.
Step-by-Step Guide:
Step 1: Restructure the Inline Keyboard:
The core issue is likely the structure of your $button_list. Telegram’s API expects each button to be in its own array, and each array of buttons represents a row. Your current code creates a single, potentially very long row. Modify your code as follows:
$inline_keyboard = [];
foreach ($poetry_data['results'] as $entry) {
$inline_keyboard[] = [
[
"text" => $entry['poem']['title'] . ' - ' . $entry['author']['name'] . ' (' . $entry['book']['title'] . ')',
"callback_data" => $entry['id']
]
];
}
// Send message with keyboard
apiRequestJson("sendMessage", [
'chat_id' => $user_id,
"text" => "Here are your search results:",
'reply_markup' => ['inline_keyboard' => $inline_keyboard]
]);
This code ensures each button is in its own row within the inline_keyboard array. We also bypass the InlineKeyboardMarkup class to simplify the JSON structure and reduce potential encoding issues with the class constructor.
Step 2: Verify JSON Encoding:
Ensure your apiRequestJson function encodes the data correctly, handling UTF-8 characters. Use the JSON_UNESCAPED_UNICODE flag in json_encode to prevent issues with Persian characters:
$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
Step 3: Check apiRequestJson Function:
Inspect your apiRequestJson function. Ensure it’s correctly setting the Content-Type header to application/json; charset=utf-8. Improper header settings can lead to encoding problems. If using cURL, this would look something like:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json; charset=utf-8"
));
Common Pitfalls & What to Check Next:
- Empty
poetry_data: Double-check that $poetry_data['results'] actually contains data. An empty array will result in an empty keyboard.
- Telegram API Limits: Very long button texts can cause issues. Consider shortening the text if necessary. Also, be mindful of Telegram’s rate limits; too many requests in a short time might lead to errors.
- Callback Data Length: The
callback_data field has a 64-byte limit. If your $entry['id'] exceeds this, shorten it or use a different approach to handle the data.
Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!