简体   繁体   English

内联键盘 Telegram bot [PHP]

[英]Inline keyboard Telegram bot [PHP]

I want that my bot when I write /orario he answer me with a inline keyboard.我希望我的机器人在我写 /orario 时他用内联键盘回答我。

So.. I created an array for my keyboard in this way:所以..我以这种方式为我的键盘创建了一个数组:

$tastierino_giorno = '[{"text":"Testo","callback_data":"StampaMessaggio"}]';

and in another function I write this:在另一个函数中我这样写:

function tastieraInline($chatid, $tastierino)
{
    global $token;
    $messaggio = "Scegli per che giorno inviare il messaggio:";
    $tastiera = '&reply_markup={"inline_keyboard": 
  ['.urlencode($tastierino).'],"resize_keyboard":true}';
    $url = "https://api.telegram.org/$token/sendMessage?chat_id=$chatId&parse_mode=HTML&text=".urlencode($messaggio).$tastiera;

    file_get_contents($url);

}

After this, with an if, I check if the users has write "/orario".在此之后,使用 if,我检查用户是否写入了“/orario”。

} elseif($message == "/orario"){
        tastieraInline($chatid, $tastierino_giorno); 
}

Now the problem is that it doesn't works... what's the problem?现在的问题是它不起作用......有什么问题?

You should pay attention to the result returned from your telegram calls.您应该注意电报调用返回的结果。 file_get_contents is great for getting something working quickly but doesn't return any error information. file_get_contents非常适合让某些事情快速运行但不返回任何错误信息。

You should use curl or a library like guzzle .您应该使用 curl 或像guzzle这样的库。 An example for curl:卷曲的一个例子:

$ch = curl_init( $url );
if ( $ch == FALSE )
{
  error_log( "Error initialising curl" );
  return FALSE;
}

curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_POST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 1 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $ch, CURLOPT_FORBID_REUSE, 1 );
// Set TCP timeout to 30 seconds
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Connection: Close' ) );

$result = curl_exec( $ch );
$error = curl_errno( $ch );
$errorStr = curl_error( $ch );
curl_close( $ch );
if ( $error != 0 )
{
  return array( $errorStr, $result );
}
return $result;

Guzzle is a lot simpler if you don't mind installing an additional library.如果您不介意安装额外的库,Guzzle 会简单得多。

Hopefully this will get you the error string from your failed telegram calls.希望这能让您从失败的电报调用中获取错误字符串。

Onto your actual issue.到你的实际问题。 You should create json for the keyboard markup using json_encode rather than appending strings.您应该使用json_encode而不是附加字符串为键盘标记创建 json。 http_build_query makes building URLs much easier. http_build_query使构建 URL 变得更加容易。

Change改变

$tastiera = '&reply_markup={"inline_keyboard": 
  ['.urlencode($tastierino).'],"resize_keyboard":true}';

to

$tastiera = '&reply_markup='.urlencode('{"inline_keyboard": 
  ['.$tastierino.'],"resize_keyboard":true}');

you should URL encode the whole JSON data你应该对整个 JSON 数据进行 URL 编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM