Code Monkey home page Code Monkey logo

vk-keyboard's Introduction

VK Keyboard

Latest Version Licence Last Commit

Установка

Перед установкой рекомендую ознакомиться с последними обновлениями api вконтакте, а также прочитать про типы кнопок, клавиатур, каруселей и особенностей работы с api вконтакте. Документация клавиатур

$ composer require sally/vk-keyboard

Использование

На момент создания библиотеки api вконтакте поддерживает 3 типа клавиатур, а именно inline, обычная клавиатура и карусели

Обычные

Basic

Inline

Inline

Карусели

Open Link

Чтобы создать такие объекты кавиатур воспользуйтесь фасадом-конструктором этих объектов.

Для создания обычной клавиатуры

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

Фасад сконвертирует клавиатуру в json, согласно требованиям api вконтакте

{
  "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"
        }
      }
    ]
  ]
}

Если отправить такую клавиатуру, то она будет отображена в таком виде Keyboard

Для Inline клавиатуры

<?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"
        }
      }
    ]
  ]
}

Inline

Для каруселей

<?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"
}

Carousel

Примеры

Более детальные примеры представлены здесь.

vk-keyboard's People

Contributors

n0tm avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

labi-le

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.