Telegram Bot PHP键盘不起作用

问题描述 投票:0回答:2

我在为我的电报机器人创建自定义键盘时遇到问题。

简单来说,它不起作用,我不知道原因......

这是我的代码:

<?
$botToken = "*****";
$website = "https://api.telegram.org/bot".$botToken;

$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);

$chatId = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
$telegramusername = $update["message"]["from"]["username"];
$message_id = $update["message"]["message_id"];
$message_name = $update["message"]["chat"]["first_name"];


switch($message)
{
case "ciao":
funzioneCiao($chatId);
break;
case "youtube":
TastieraInline($chatId);
break;
default:
TastieraMenuPrincipale($chatId);
break;
}

function inviaMessaggio($chatId, $messaggio)
{
    $url = "$GLOBALS[website]/sendMessage?chat_id=$chatId&parse_mode=HTML&text=".urlencode($messaggio);
    file_get_contents($url);
}

function funzioneCiao($chatId)
{
    $messaggio = "ciao";
inviaMessaggio($chatId, $messaggio);
}



function TastieraMenuPrincipale($chatId)
{
    $messaggio = "ciaaaao";
    $tastiera = '&reply_markup={"keyboard":[["Menu Principale"],["Developer"]]}';
    $url = "$GLOBALS[website]"."/sendMessage?chat_id=".$chatId."&parse_mode=HTML&text=".$messaggio.$tastiera;
    file_get_contents($url);

}

function TastieraInline($chatId)
{
    $message = "Iscriviti subito";
    $tastiera = '&reply_markup={"inline_keyboard":[[{"text":"SEGUIMI!","url":"http://www.youtube.com"}]]}';
    $url = $GLOBALS[website].'/sendMessage?chat_id='.$chatId.'&parse_mod=HTML&text='.$message.$tastiera;
    file_get_contents($url);
}
?>

功能:“Hello函数($ chatId);”和“sendMessage($ chatId,$ message)”有效,但“KeyboardMenuPrincipale($ chatId)”和“KeyboardInLine($ ChatId)”不起作用。

我是PHP的初学者,所以我有很多困难......

谢谢。

bots telegram telegram-bot
2个回答
0
投票

我刚刚测试了代码,它完美地工作了......

enter image description here


0
投票

你为什么不使用CURL而不是file_get_contents();

只需将此函数写入代码,然后在获取新更新后调用它:

function makeHTTPRequest($method, $types = []){
    $url = 'https://api.telegram.org/bot'.$botToken.'/'.$method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($types));
    $res = curl_exec($ch);
    if (curl_error($ch)){
        var_dump(curl_error($ch));
    } else {
        return json_decode($res);
    }
} 

确保在您的托管服务器上激活CURL!

© www.soinside.com 2019 - 2024. All rights reserved.