2017-08-26 152 views
2

由于公司需要,我必须在php中进行编程......但是我第一次使用php ......而且这是我第一次使用telegram bot:'( 在某种程度上,之前,当我跑的命令/startdoWork一切工作...如何在PHP Bot Telegram中创建内联按钮

但现在我必须修改BOT,在某种程度上,所有的命令都是“隐藏”背后的一些电报按钮...在这里我如何编辑我的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); 

} 

随着BotFather我跑的命令/setinline太...

所以我认为我的工作方式parameters阵列..任何人都可以帮助我吗?

诗篇:(如果任何人都可以建议我也是一个IDE我请工作...我使用记事本+ +现在)

谢谢大家

回答

2

首先你不需要使用在botFather中使用/setinline命令。此命令用于“内联模式”,而您正在使用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 

最后,你需要通过卷曲发送。这里是我在我的代码中使用的功能:

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; 
} 

P.S.我个人使用PhpStorm,它不错;)