PHP 中的 Telegram Bot 自定义键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31099087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:14:43  来源:igfitidea点击:

Telegram Bot custom keyboard in PHP

phptelegram-bot

提问by Martin

I'm trying to make a Telgram Bot in PHP with a custom keyboard. The message is delivered, but the custom keyboard won't work. $keyb = array('keyboard' => array(array("A", "B"))); also no succes.

我正在尝试使用自定义键盘在 PHP 中制作 Telgram Bot。消息已发送,但自定义键盘不起作用。$keyb = array('keyboard' => array(array("A", "B"))); 也没有成功。

The sendMessagemethod referrers to ReplyKeyboardMarkupfor the object. Making an array for ReplyKeyboardMarkup doesn't work. Also tried to json_encode($keyb) but that's also not the solution.

所述的sendMessage方法引荐ReplyKeyboardMarkup为对象。为 ReplyKeyboardMarkup 创建数组不起作用。也尝试过 json_encode($keyb) 但这也不是解决方案。

I searched in GitHub for examples but I haven't found one where the custom keyboard is used. Telegram runs on iPhone and desktop, both uptodate.

我在 GitHub 中搜索了示例,但没有找到使用自定义键盘的示例。Telegram 在 iPhone 和桌面上运行,两者都是最新的。

Sample code:

示例代码:

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

回答by Dan Belden

The docs seem to indicate you need to provide the reply_markup parameter as a JSON serialised object... kinda stupid for a form POST endpoint:

文档似乎表明您需要将 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"
);

Does this one work?

这个有用吗?

回答by Gabriel Sgobi Martinelli

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

This code works fine!

这段代码工作正常!