为什么电报键盘不随枪口请求一起发送 [英] Why Telegram keyboard not send with Guzzle request

查看:104
本文介绍了为什么电报键盘不随枪口请求一起发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ params = ['chat_id'=>$ this-> chatID,'文本'=>'什么?','reply_markup'=>['resize_keyboard'=>真的,'键盘'=>[[['text'=>'文本1'],['text'=>'text2'],]]]];$ this->客户端-> request('POST',$ this-> apiUrl.'/sendMessage',array('query'=> $ params)); 

该消息有效!但是龙骨没有显示.

解决方案

问题似乎是Guzzle不会对任何深度查询参数进行urlencode,我已经设法通过使用

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

The message works! But the keboards doesn't show.

解决方案

The issue seems to be that Guzzle does not urlencode any deep query params, I've managed to send the keyboard by using json_encode on the keyboard, before sending to Guzzle;

<?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"
                        ]
                    ]
                ]
            ])
        ]
    ]);

这篇关于为什么电报键盘不随枪口请求一起发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆