You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP开发Telegram Bot内联键盘不显示问题求助

Hey there! Let's fix your inline keyboard issue step by step—you're almost there, just a couple of small mistakes in your code:

1. Critical Typo in the Keyboard Field Name

The biggest problem is a simple typo: you wrote incline_keyboard instead of inline_keyboard in your array. Telegram's API only recognizes the exact field name inline_keyboard, so the misspelling makes the bot ignore your keyboard entirely.

2. Missing URL Encoding for the JSON Payload

When you append the JSON-encoded keyboard to the API URL, special characters like {, }, and " will break the URL structure. You need to run urlencode() on the final JSON string to make it safe for URL parameters.

3. Text Encoding That Breaks Markdown

Your current urlencode($text) will escape Markdown syntax characters (like * or _), making them stop working. Using rawurlencode() instead preserves these characters while still making the text safe for the URL.


Here's the fixed version of your inlinekeyboard function:

function inlinekeyboard($button, $cid, $text) {
    // Preserve Markdown with rawurlencode instead of urlencode
    $encodedText = rawurlencode($text);
    
    // Fix the typo: inline_keyboard is the correct field name
    $inlineKey = ["inline_keyboard" => $button];
    $inlineKeyJson = json_encode($inlineKey);
    
    // URL-encode the JSON to avoid breaking the API request URL
    $encodedKeyboard = urlencode($inlineKeyJson);
    
    apiRequest("sendMessage?chat_id=".$cid."&text=".$encodedText."&parse_mode=Markdown&reply_markup=".$encodedKeyboard);
}

Your first button definition method is clean and correct, so you can keep using it (I also fixed the tiny "Bottun" typo for consistency):

$button = [
    [
        ["text" => "Button 1", "url" => "https://www.aaa.com"],
        ["text" => "Button 2", "url" => "https://www.bbb.com"]
    ]
]; 
inlinekeyboard($button, $cid, "Click to choose");

内容的提问来源于stack exchange,提问作者Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:32:38