【问题标题】:Telegram Bot custom keyboard in PHPPHP 中的 Telegram Bot 自定义键盘
【发布时间】:2015-09-14 22:29:12
【问题描述】:

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

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

我在 GitHub 中搜索了示例,但没有找到使用自定义键盘的示例。 Telegram 在 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);

【问题讨论】:

    标签: php telegram-bot


    【解决方案1】:

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

    这个有用吗?

    【讨论】:

    • 不,也不起作用.. 忘了提,但也试过了
    • 根据reddit,这似乎是正确的方法:reddit.com/r/Telegram/comments/3bblz0/… 可能值得转储您的帖子数据并进行比较。祝你好运。
    • 是的,这个有效!我虽然都试过了,但显然我跳过了这个。谢谢!
    • 作为记录,必须将所有字段都包含到ReplyKeyboardMarkup 对象中,而不仅仅是keyboard
    • @DeerHunter 这是假的,其余字段是可选的。
    【解决方案2】:
       $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);
    

    这段代码运行良好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-26
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多