【问题标题】:Why Telegram keyboard not send with Guzzle request为什么 Telegram 键盘不与 Guzzle 请求一起发送
【发布时间】:2021-02-14 20:55:25
【问题描述】:
$params = [
    'chat_id' => $this->chatID,
    'text' => 'What?',
    'reply_markup' => [
        'resize_keyboard' => true,
        'keyboard' => [
            [
                ['text' => 'text1'],
                ['text' => 'text2'],
            ]
        ]
    ]
];
$this->client->request('POST', $this->apiUrl . '/sendMessage', array('query' => $params));

消息有效!但是 keboards 没有显示。

【问题讨论】:

  • 你在对所有字符进行urlencoding吗?
  • 嗯,guzzle http 客户端,使用 urlencoding?
  • 请,当您询问有关与不同编程语言一起使用的库或包的问题时,请在文本和/或标签中指定您使用什么语言

标签: php telegram telegram-bot guzzle


【解决方案1】:

问题似乎是 Guzzle 没有对任何深度查询参数进行 urlencode,在发送到 Guzzle 之前,我已经设法通过在键盘上使用 json_encode 发送键盘;

<?php

    use GuzzleHttp\Client;
    require_once __DIR__ . '/vendor/autoload.php';

    CONST CHAT_ID = '~~';
    CONST TOKEN   = '~~';
    CONST BASE    = 'https://api.telegram.org/bot' . TOKEN;

    $client = new Client();
    $client->request('POST', BASE . '/sendMessage', [
        'query' => [
            'chat_id' => CHAT_ID,
            'text' => 'Hi Maxim!',
            'reply_markup' => json_encode([
                "inline_keyboard" => [
                    [
                        [
                            "text" => "Yes",
                            "callback_data" => "yes"
                        ],
                        [
                            "text" => "No",
                            "callback_data" => "no"
                        ]
                    ]
                ]
            ])
        ]
    ]);

【讨论】:

  • 谢谢,回复标记需要json_encode
猜你喜欢
  • 2019-02-08
  • 1970-01-01
  • 2018-02-07
  • 2017-08-28
  • 2020-05-01
  • 2012-02-10
  • 2019-05-19
  • 1970-01-01
相关资源
最近更新 更多