sally / vk-keyboard
此软件包的最新版本(1.0)没有提供许可证信息。
将vk api键盘对象转换的软件包
1.0
2020-04-16 14:51 UTC
Requires
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^8.5
This package is auto-updated.
Last update: 2024-09-18 20:09:37 UTC
README
安装
在安装前,请查阅最新版本的vkontakte api更新,并阅读有关按钮、键盘、轮播图以及与vkontakte api交互的详细信息。 键盘文档
$ composer require sally/vk-keyboard
使用方法
截至创建库时,vkontakte api支持3种键盘类型,即内嵌键盘、普通键盘和轮播图。
普通键盘
内嵌键盘
轮播图
要创建这样的键盘对象,请使用这些对象的构造函数封装器。
创建普通键盘
<?php require '../vendor/autoload.php'; use Sally\VkKeyboard\Facade; use Sally\VkKeyboard\Contracts\Keyboard\Button\FactoryInterface; use Sally\VkKeyboard\Object\Keyboard\Button\Text; $keyboard = Facade::createKeyboardBasic(function (FactoryInterface $factory) { return [ [ $factory->location([]), ], [ $factory->text('Текстовая кнопка', ['button' => 1], Text::COLOR_RED), $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') ] ]; });
封装器将键盘转换为符合vkontakte api要求的json格式。
{ "one_time": true, "buttons": [ [ { "action": { "payload": "[]", "type": "location" } } ], [ { "color": "negative", "action": { "payload": "{\"button\":1}", "label": "Текстовая кнопка", "type": "text" } }, { "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 Sally\VkKeyboard\Facade; use Sally\VkKeyboard\Contracts\Keyboard\Button\FactoryInterface; use Sally\VkKeyboard\Object\Keyboard\Button\Text; $keyboard = Facade::createKeyboardInline(function (FactoryInterface $factory) { return [ [ $factory->location([]), ], [ $factory->text('Текстовая кнопка', ['button' => 1], Text::COLOR_RED), $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" } }, { "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 Sally\VkKeyboard\Facade; use Sally\VkKeyboard\Contracts\Keyboard\Button\FactoryInterface as ButtonFactoryInterface; use Sally\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" }
示例
更详细的示例请见 此处.