labile / vk-keyboard
用于转换vk API键盘对象的包
1.1
2021-08-18 18:50 UTC
Requires
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-09 14:55:34 UTC
README
安装前
建议在安装前查看vkontakte API的最新更新,并阅读有关按钮、键盘、轮播图和与vkontakte API交互的特点的文档。键盘文档
$ composer require labile/vk-keyboard
使用
在创建库时,vkontakte API支持3种键盘类型,即内联键盘、常规键盘和轮播图。
常规
内联
轮播图
要创建这样的键盘对象,请使用这些对象的门面构造器。
创建常规键盘
<?php require '../vendor/autoload.php'; use Astaroth\VkKeyboard\Facade; use Astaroth\VkKeyboard\Contracts\Keyboard\Button\FactoryInterface; use Astaroth\VkKeyboard\Object\Keyboard\Button\Text; $keyboard = Facade::createKeyboardBasic(function (FactoryInterface $factory) { return [ [ $factory->location([]), ], [ $factory->text('Текстовая кнопка', ['button' => 1], Text::COLOR_RED), $factory->callback('callback кнопка', ['button' => 2], Text::COLOR_BLUE), $factory->link('Кнопка с ссылкой', 'https://github.com/Sally-Framework/vk-keyboard', []), ], [ $factory->pay('action=transfer-to-group&group_id=1&aid=10') ], [ $factory->app('Кнопка приложения', 6979558, '-181108510', 'sendKeyboard') ] ]; });
门面将键盘转换为JSON,符合vkontakte API的要求
{ "one_time": true, "buttons": [ [ { "action": { "payload": "[]", "type": "location" } } ], [ { "color": "negative", "action": { "payload": "{\"button\":1}", "label": "Текстовая кнопка", "type": "text" } }, { "color": "primary", "action": { "payload": "{\"button\":2}", "label": "callback кнопка", "type": "callback" } }, { "action": { "link": "https:\/\/github.com\/Sally-Framework\/vk-keyboard", "label": "Кнопка с ссылкой", "payload": "[]", "type": "open_link" } } ], [ { "action": { "hash": "action=transfer-to-group&group_id=1&aid=10", "type": "vkpay" } } ], [ { "action": { "app_id": 6979558, "owner_id": "-181108510", "hash": "sendKeyboard", "label": "Кнопка приложения", "type": "open_app" } } ] ] }
创建内联键盘
<?php require '../vendor/autoload.php'; use Astaroth\VkKeyboard\Facade; use Astaroth\VkKeyboard\Contracts\Keyboard\Button\FactoryInterface; use Astaroth\VkKeyboard\Object\Keyboard\Button\Text; $keyboard = Facade::createKeyboardInline(function (FactoryInterface $factory) { return [ [ $factory->location([]), ], [ $factory->text('Текстовая кнопка', ['button' => 1], Text::COLOR_RED), $factory->callback('callback кнопка', ['button' => 2], Text::COLOR_BLUE), $factory->link('Кнопка с ссылкой', 'https://github.com/Sally-Framework/vk-keyboard', []), ], [ $factory->pay('action=transfer-to-group&group_id=1&aid=10') ], [ $factory->app('Кнопка приложения', 6979558, '-181108510', 'sendKeyboard') ] ]; });
{ "inline": true, "buttons": [ [ { "action": { "payload": "[]", "type": "location" } } ], [ { "color": "negative", "action": { "payload": "{\"button\":1}", "label": "Текстовая кнопка", "type": "text" } }, { "color": "primary", "action": { "payload": "{\"button\":2}", "label": "callback кнопка", "type": "callback" } }, { "action": { "link": "https:\/\/github.com\/Sally-Framework\/vk-keyboard", "label": "Кнопка с ссылкой", "payload": "[]", "type": "open_link" } } ], [ { "action": { "hash": "action=transfer-to-group&group_id=1&aid=10", "type": "vkpay" } } ], [ { "action": { "app_id": 6979558, "owner_id": "-181108510", "hash": "sendKeyboard", "label": "Кнопка приложения", "type": "open_app" } } ] ] }
创建轮播图
<?php require '../vendor/autoload.php'; use Astaroth\VkKeyboard\Facade; use Astaroth\VkKeyboard\Contracts\Keyboard\Button\FactoryInterface as ButtonFactoryInterface; use Astaroth\VkKeyboard\Contracts\Template\Carousel\Element\FactoryInterface as CarouselElementInterface; $templateWithOpenPhoto = Facade::createCarousel(function (CarouselElementInterface $elementFactory, ButtonFactoryInterface $buttonFactory) { return [ $elementFactory->openPhoto([ $buttonFactory->location(['button' => 'location']), $buttonFactory->pay('action=transfer-to-group&group_id=1&aid=10'), $buttonFactory->link('Кнопка с ссылкой', 'https://github.com/Sally-Framework/vk-keyboard', []), ], 'Карусель Open Photo', 'По нажатию у вас откроется картинка', '-109837093_457242809'), $elementFactory->openPhoto([ $buttonFactory->location(['button' => 'location']), $buttonFactory->pay('action=transfer-to-group&group_id=1&aid=10'), $buttonFactory->link('Кнопка с ссылкой', 'https://github.com/Sally-Framework/vk-keyboard', []), ], 'Карусель Open Photo', 'По нажатию у вас откроется картинка', '-109837093_457242809'), $elementFactory->openPhoto([ $buttonFactory->location(['button' => 'location']), $buttonFactory->pay('action=transfer-to-group&group_id=1&aid=10'), $buttonFactory->link('Кнопка с ссылкой', 'https://github.com/Sally-Framework/vk-keyboard', []), ], 'Карусель Open Photo', 'По нажатию у вас откроется картинка', '-109837093_457242809'), ]; });
{ "elements": [ { "buttons": [ { "action": { "payload": "{\"button\":\"location\"}", "type": "location" } }, { "action": { "hash": "action=transfer-to-group&group_id=1&aid=10", "type": "vkpay" } }, { "action": { "link": "https:\/\/github.com\/Sally-Framework\/vk-keyboard", "label": "Кнопка с ссылкой", "payload": "[]", "type": "open_link" } } ], "title": "Карусель Open Photo", "description": "По нажатию у вас откроется картинка", "photo_id": "-109837093_457242809", "action": { "type": "open_photo" } }, { "buttons": [ { "action": { "payload": "{\"button\":\"location\"}", "type": "location" } }, { "action": { "hash": "action=transfer-to-group&group_id=1&aid=10", "type": "vkpay" } }, { "action": { "link": "https:\/\/github.com\/Sally-Framework\/vk-keyboard", "label": "Кнопка с ссылкой", "payload": "[]", "type": "open_link" } } ], "title": "Карусель Open Photo", "description": "По нажатию у вас откроется картинка", "photo_id": "-109837093_457242809", "action": { "type": "open_photo" } }, { "buttons": [ { "action": { "payload": "{\"button\":\"location\"}", "type": "location" } }, { "action": { "hash": "action=transfer-to-group&group_id=1&aid=10", "type": "vkpay" } }, { "action": { "link": "https:\/\/github.com\/Sally-Framework\/vk-keyboard", "label": "Кнопка с ссылкой", "payload": "[]", "type": "open_link" } } ], "title": "Карусель Open Photo", "description": "По нажатию у вас откроется картинка", "photo_id": "-109837093_457242809", "action": { "type": "open_photo" } } ], "type": "carousel" }
示例
更详细的示例可以在这里找到。