PHP中的Telegram Bot自定义键盘

时间:2015-06-28 11:06:01

标签: php telegram-bot

我试图用自定义键盘在PHP中创建一个Telgram Bot。邮件已发送,但自定义键盘无法正常工作。 $ keyb = array(' keyboard' => array(array(" A"," B")));也没有成功。

sendMessage方法引用ReplyKeyboardMarkup对象。为ReplyKeyboardMarkup创建一个数组并不起作用。还尝试了json_encode($ keyb),但这也不是解决方案。

我在GitHub中搜索了一些示例,但我还没找到使用自定义键盘的地方。电报在iPhone和桌面上运行,都是最新的。

示例代码:

$url = "https://api.telegram.org/bot<token>/sendMessage";

$keyb = array('ReplyKeyboardMarkup' => array('keyboard' => array(array("A", "B"))));
$content = array('chat_id' => <chat_id>, 'reply_markup' => $keyb, 'text' => "Test");

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //fix http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/

// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);

2 个答案:

答案 0 :(得分:23)

文档似乎表明你需要提供reply_markup参数作为JSON序列化对象...对于表单POST端点有点愚蠢:

$replyMarkup = array(
    'keyboard' => array(
        array("A", "B")
    )
);
$encodedMarkup = json_encode($replyMarkup);
$content = array(
    'chat_id' => <chat_id>,
    'reply_markup' => $encodedMarkup,
    'text' => "Test"
);

这个有用吗?

答案 1 :(得分:10)

   $keyboard = array(array("[Destaques]","[Campinas e RMC]","[esportes]"));
   $resp = array("keyboard" => $keyboard,"resize_keyboard" => true,"one_time_keyboard" => true);
   $reply = json_encode($resp);
   $url = $GLOBALS[website]."/sendmessage?chat_id=".$chatId."&text=oi&reply_markup=".$reply;
    file_get_contents($url);

此代码工作正常!