如何在PHP Bot Telegram中创建内联按钮

Cic*_*cio 4 php telegram-bot php-telegram-bot

我必须在PHP程序,由于公司需要......但我与PHP的首次工作......这是第一次,我用电报机器人的工作:"(在某种程度上,之前,当我跑的命令/startdoWork一切正常......

但现在我必须修改机器人,所有命令都隐藏在某个电报按钮后面...这里我是如何编辑我的php页面的:

if(strpos($text, "/start") === 0)
{
    $response = "Ciao $firstname, benvenuto!";

    $keyboard = [
        'inline_keyboard' => [
            [
                ['text' => 'forward me to groups']
            ]
        ]
    ];

    $encodedKeyboard = json_encode($keyboard);
    $parameters = 
        array(
            'chat_id' => $chatId, 
            'text' => $response, 
            'reply_markup' => $encodedKeyboard
        );
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);

}
Run Code Online (Sandbox Code Playgroud)

有了BotFather,我也运行了命令/setinline......

所以我想我正在研究我的parameters阵列..有人能帮助我吗?

Ps.:(如果任何人都可以建议我也是我工作的IDE请...我现在使用notepad ++)

谢谢你们

小智 8

首先,您不需要/setinline在botFather中使用命令.当您inline_keyboard在正常聊天模式下使用自定义键盘时,此命令用于"内联模式" .

您还需要callback_data为每个按钮提供一个键盘数组:

$keyboard = [
    'inline_keyboard' => [
        [
            ['text' => 'forward me to groups', 'callback_data' => 'someString']
        ]
    ]
];
$encodedKeyboard = json_encode($keyboard);
$parameters = 
    array(
        'chat_id' => $chatId, 
        'text' => $response, 
        'reply_markup' => $encodedKeyboard
    );

send('sendMessage', $parameters); // function description Below
Run Code Online (Sandbox Code Playgroud)

最后你需要通过curl发送它.这是我在我的代码中使用的函数:

function send($method, $data)
{
    $url = "https://api.telegram.org/bot<Bot-Token>". "/" . $method;

    if (!$curld = curl_init()) {
        exit;
    }
    curl_setopt($curld, CURLOPT_POST, true);
    curl_setopt($curld, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curld, CURLOPT_URL, $url);
    curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($curld);
    curl_close($curld);
    return $output;
}
Run Code Online (Sandbox Code Playgroud)

PS我个人使用PhpStorm,它很好;)