yageorgiy / botman-vk-community-callback-driver

A VK community callback API driver for botman.io framework
MIT License
12 stars 4 forks source link

Buttons не видит другие Type кроме text и callback #10

Closed ale24 closed 3 years ago

ale24 commented 3 years ago

Botman(не студия) | PHP 7.3.11 | "version" => "5.103"

В документации сказано:

Sending links | ✔ Supported See VK documentation page for available colours, types and other features. Just add new fields in array of additional parameters as it is shown in the example above.

Оба способа выставления Type не работают:

$buttons = array(
 Button::create('pay')->additionalParameters(["action" => ["type" => "open_link", "url" => "https://vk.com", "label" => "test"])
);
$question = Question::create('test')->addButtons($buttons);

$open_link = ["type" => "open_link", "url" => "https://vk.com", "label" => "test"];
$keyboard->addRows(
 new VKKeyboardRow([                
  ( new VKKeyboardButton() )->setRawAction($open_link)->setPayload(json_encode(["action"=>$open_link]))->setColor(null)
 ])
);

Судя по коду драйвер принудительно выставляет цвет, что возможно противоречит той же информации по ссылке от ВК:

//class VKKeyboardButton
const COLOR_PRIMARY = "primary";
protected $color = self::COLOR_PRIMARY;
public function toArray(){
 return [
  "color" => $this->color,
  "action" => $this->action
 ];
}

цвет кнопки. Параметр используется только для кнопок с type: text и callback.

Если убрать обязательное требование цвета, то некоторые Type вроде open_app удалось заставить работать, но open_link даже так почему-то не работает:

public function toArray(){
        if( !isset($this->color) ){
            return [                
                "action" => $this->action
            ];
        }
        return [
            "color" => $this->color,
            "action" => $this->action
        ];
}

// без обязательного цвета кнопки этот пример из ВК работает
$open_app = [
 "type" => "open_app",                  
 "app_id" => 6232540,
 "owner_id" => -157525928,
  "hash" => "123",
 "label" => "LiveWidget"                    
];

$keyboard->addRows(
  new VKKeyboardRow([               
  ( new VKKeyboardButton() )->setRawAction($open_link)->setPayload(json_encode(["action"=>$open_app]))->setColor(null)
 ])
);
yageorgiy commented 3 years ago

До версии 1.7.6 (включительно) драйвер не поддерживал другие типы кнопок, отличные от type="text". В версии 1.7.7 расширил поддержку других кнопок из документации:

$botman->hears('vk keyboard', function(BotMan $bot) {
    $keyboard = new VKKeyboard();
    $keyboard->setInline(false);
    $keyboard->setOneTime(false);

    $keyboard->addRows(
        new VKKeyboardRow([
            ( new VKKeyboardButton() )
                ->setColor(VKKeyboardButton::COLOR_PRIMARY)
                ->setText("Sample text")
                ->setValue("button1")
        ]),

        new VKKeyboardRow([
            ( new VKKeyboardButton() )
                ->setType(VKKeyboardButton::TYPE_OPEN_LINK)
                ->setLink("https://github.com/")
                ->setText("Open link")
                ->setValue("button_open_link")
        ]),
        new VKKeyboardRow([
            ( new VKKeyboardButton() )
                ->setType(VKKeyboardButton::TYPE_LOCATION)
                ->setValue("button_location")
        ]),
        new VKKeyboardRow([
            ( new VKKeyboardButton() )
                ->setType(VKKeyboardButton::TYPE_VK_PAY)
                ->setHash("action=transfer-to-group&group_id=1&aid=10")
                ->setValue("button_vk_pay")
        ]),
        new VKKeyboardRow([
            ( new VKKeyboardButton() )
                ->setType(VKKeyboardButton::TYPE_OPEN_APP)
                ->setAppID(123456789)
                ->setOwnerID(-123456789)
                ->setText("Open app")
                ->setValue("button_open_app")
        ]),

        new VKKeyboardRow([
            ( new VKKeyboardButton() )
                ->setType(VKKeyboardButton::TYPE_CALLBACK)
                ->setText("Callback button")
                ->setColor(VKKeyboardButton::COLOR_DEFAULT)
                ->setValue("button_callback")
        ])
    );

    $bot->reply("Native keyboard:", [
        "keyboard" => $keyboard->toJSON()
    ]);
});

image

ale24 commented 3 years ago

благодарю за оперативный апдейт

кому интересно, рабочий вариант для прошлой версии через sendRequest:

        $endpoint = "messages.send";

        $open_link = [                  
            'action' => [                   
                'type' => 'open_link',
                'link' => 'https://vk.com',
                'label' => 'test'
            ]       
        ];

        $open_link['action']['payload'] = json_encode($open_link);

        $keyboard = [
            'one_time' => false,
            'buttons' => [
                0 => [ 0 => $open_link ]
            ]         
        ];

        // Arguments ("v" and "access_token" are set by driver, no need to define)
        $arguments = [
             "peer_id" => $bot->getUser()->getId(), // User ID
             "message" => 'test', // Sticker ID
             "random_id" => rand(10000,100000), // Random ID (required by VK API, to prevent doubling messages)
             "keyboard" => json_encode($keyboard)            
        ];

        $bot->sendRequest($endpoint, $arguments);