【问题标题】:Telegram Bot PHP keyboard doesn't worksTelegram Bot PHP 键盘不起作用
【发布时间】:2017-07-22 09:05:49
【问题描述】:

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

简直就是不行,也不知道是什么原因……

这是我的代码:

<?
$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);
}
?>

函数:“funzioneCiao($chatId);”并且“inviaMessaggio($chatId, $messaggio)”有效,但是, “TastieraMenuPrincipale($chatId)”和“TastieraInLine($ChatId)”不起作用。

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

谢谢。

【问题讨论】:

  • 你开枪显示你的机器人令牌。它可能被滥用。您可以使用任何随机字母代替它。
  • 这是一个例子,它不是我真正的 Bot ;)

标签: bots telegram telegram-bot


【解决方案1】:

你为什么不使用 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!

【讨论】:

    【解决方案2】:

    我刚刚测试了代码,它运行良好...

    【讨论】:

    • 嗯,我又试了一次,但它对我不起作用....我正在使用免费托管网站来使用我的机器人,但不起作用...你能帮帮我吗?
    • 我使用 url (api.telegram......) 进行了测试,它可以工作。所以可能的问题有两个:" 和 ' 会出现问题,或者我的 freewebhosting 无法做到这一点
    • 这是完整的代码...现在唯一不起作用的是 API KEY,因为我删除了机器人...您可以在 pvt 中与我联系吗? alessandrosiveri@gmail.com
    • 那你应该用@BotFather制作另一个机器人并再次测试代码
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2015-09-14
    • 2016-10-20
    • 1970-01-01
    • 2019-08-07
    • 2016-04-10
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多