You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.5 KiB

1 year ago
  1. <?php
  2. /**
  3. * This file is part of the PHP Telegram Bot example-bot package.
  4. * https://github.com/php-telegram-bot/example-bot/
  5. *
  6. * (c) PHP Telegram Bot Team
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Longman\TelegramBot\Commands\UserCommands;
  12. /**
  13. * User "/keyboard" command
  14. *
  15. * Display a keyboard with a few buttons.
  16. */
  17. use Longman\TelegramBot\Commands\UserCommand;
  18. use Longman\TelegramBot\Entities\Keyboard;
  19. use Longman\TelegramBot\Entities\ServerResponse;
  20. use Longman\TelegramBot\Exception\TelegramException;
  21. class KeyboardCommand extends UserCommand
  22. {
  23. /**
  24. * @var string
  25. */
  26. protected $name = 'keyboard';
  27. /**
  28. * @var string
  29. */
  30. protected $description = 'Show a custom keyboard with reply markup';
  31. /**
  32. * @var string
  33. */
  34. protected $usage = '/keyboard';
  35. /**
  36. * @var string
  37. */
  38. protected $version = '0.3.0';
  39. /**
  40. * Main command execution
  41. *
  42. * @return ServerResponse
  43. * @throws TelegramException
  44. */
  45. public function execute(): ServerResponse
  46. {
  47. /** @var Keyboard[] $keyboards */
  48. $keyboards = [];
  49. // Simple digits
  50. $keyboards[] = new Keyboard(
  51. ['7', '8', '9'],
  52. ['4', '5', '6'],
  53. ['1', '2', '3'],
  54. [' ', '0', ' ']
  55. );
  56. // Digits with operations
  57. $keyboards[] = new Keyboard(
  58. ['7', '8', '9', '+'],
  59. ['4', '5', '6', '-'],
  60. ['1', '2', '3', '*'],
  61. [' ', '0', ' ', '/']
  62. );
  63. // Short version with 1 button per row
  64. $keyboards[] = new Keyboard('A', 'B', 'C');
  65. // Some different ways of creating rows and buttons
  66. $keyboards[] = new Keyboard(
  67. ['text' => 'A'],
  68. 'B',
  69. ['C', 'D']
  70. );
  71. // Buttons to perform Contact or Location sharing
  72. $keyboards[] = new Keyboard([
  73. ['text' => 'Send my contact', 'request_contact' => true],
  74. ['text' => 'Send my location', 'request_location' => true],
  75. ]);
  76. // Shuffle our example keyboards and return a random one
  77. shuffle($keyboards);
  78. $keyboard = end($keyboards)
  79. ->setResizeKeyboard(true)
  80. ->setOneTimeKeyboard(true)
  81. ->setSelective(false);
  82. return $this->replyToChat('Press a Button!', [
  83. 'reply_markup' => $keyboard,
  84. ]);
  85. }
  86. }