Skip to content

Instantly share code, notes, and snippets.

@xISRAPILx
Created January 6, 2019 09:52
Show Gist options
  • Save xISRAPILx/5087fd787ec3499a964b1b4307e78401 to your computer and use it in GitHub Desktop.
Save xISRAPILx/5087fd787ec3499a964b1b4307e78401 to your computer and use it in GitHub Desktop.
Класс для создания кнопок ВКонтакте.
<?php
/**
* Created by PhpStorm.
* User: Исрапил
* Date: 09.12.2018
* Time: 17:57
*/
namespace bot\utils;
class Keyboard implements \JsonSerializable{
public const COLOR_PRIMARY = "primary"; //синяя кнопка, обозначает основное действие. #5181B8
public const COLOR_DEFAULT = "default"; //обычная белая кнопка. #FFFFFF
public const COLOR_NEGATIVE = "negative"; //опасное действие, или отрицательное действие (отклонить, удалить и тд). #E64646
public const COLOR_POSITIVE = "positive"; //согласиться, подтвердить. #4BB34B
/** @var array */
private $buttons = [];
/** @var bool */
private $one_time;
/** @var int */
private $currentLine = 0;
/**
* Конструктор класса
*
* @param bool $one_time true - кнопка пропадет после нажатия, false - нет.
*/
public function __construct(bool $one_time = true){
$this->one_time = $one_time;
}
/**
* @param string $command Команда для для идентификации действия
* @param string $label Надпись на кнопке
* @param string $color Цвет кнопки
* @param bool $newLine Перевод кнопок на новую строку.
*
* @return Keyboard
*/
public function addButton(string $command, string $label, string $color = self::COLOR_DEFAULT, bool $newLine = false) : Keyboard{
$this->currentLine += $newLine ? 1 : 0;
$this->buttons[$this->currentLine][] = [
"action" => [
"type" => "text",
"payload" => json_encode([
"command" => $command
]),
"label" => $label
],
"color" => $color
];
return $this;
}
/**
* @return array
*/
public function jsonSerialize() : array{
return [
"one_time" => $this->one_time,
"buttons" => $this->buttons
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment