|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the TelegramBot package. |
| 4 | + * |
| 5 | + * (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Longman\TelegramBot\Commands\UserCommands; |
| 12 | + |
| 13 | +use Longman\TelegramBot\Commands\UserCommand; |
| 14 | +use Longman\TelegramBot\Request; |
| 15 | +use Longman\TelegramBot\Entities\InlineKeyboardMarkup; |
| 16 | + |
| 17 | +/** |
| 18 | + * User "/inlinekeyboard" command |
| 19 | + */ |
| 20 | +class InlinekeyboardCommand extends UserCommand |
| 21 | +{ |
| 22 | + /**#@+ |
| 23 | + * {@inheritdoc} |
| 24 | + */ |
| 25 | + protected $name = 'inlinekeyboard'; |
| 26 | + protected $description = 'Show a custom inline keybord with reply markup'; |
| 27 | + protected $usage = '/inlinekeyboard'; |
| 28 | + protected $version = '0.0.1'; |
| 29 | + /**#@-*/ |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + public function execute() |
| 35 | + { |
| 36 | + $message = $this->getMessage(); |
| 37 | + $chat_id = $message->getChat()->getId(); |
| 38 | + $text = $message->getText(true); |
| 39 | + |
| 40 | + $data = []; |
| 41 | + $data['chat_id'] = $chat_id; |
| 42 | + $data['text'] = 'Press a Button:'; |
| 43 | + |
| 44 | + //Keyboard examples |
| 45 | + $inline_keyboards = []; |
| 46 | + |
| 47 | + //0 |
| 48 | + $inline_keyboard[] = [ |
| 49 | + [ |
| 50 | + 'text' => '<', |
| 51 | + 'callback_data' => 'go_left' |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'text' => '^', |
| 55 | + 'callback_data' => 'go_up' |
| 56 | + ], |
| 57 | + [ |
| 58 | + 'text' => '>', |
| 59 | + 'callback_data' => 'go_right' |
| 60 | + ] |
| 61 | + ]; |
| 62 | + |
| 63 | + $inline_keyboards[] = $inline_keyboard; |
| 64 | + unset($inline_keyboard); |
| 65 | + |
| 66 | + //1 |
| 67 | + $inline_keyboard[] = [ |
| 68 | + [ |
| 69 | + 'text' => 'open google.com', |
| 70 | + 'url' => 'google.com' |
| 71 | + ], |
| 72 | + [ |
| 73 | + 'text' => 'open youtube.com', |
| 74 | + 'url' => 'youtube.com' |
| 75 | + ] |
| 76 | + ]; |
| 77 | + |
| 78 | + $inline_keyboards[] = $inline_keyboard; |
| 79 | + unset($inline_keyboard); |
| 80 | + |
| 81 | + //2 |
| 82 | + $inline_keyboard[] = [ |
| 83 | + [ |
| 84 | + 'text' => 'search \'test\' inline', |
| 85 | + 'switch_inline_query' => 'test' |
| 86 | + ], |
| 87 | + [ |
| 88 | + 'text' => 'search \'cats\' inline', |
| 89 | + 'switch_inline_query' => 'cats' |
| 90 | + ] |
| 91 | + ]; |
| 92 | + $inline_keyboard[] = [ |
| 93 | + [ |
| 94 | + 'text' => 'search \'earth\' inline', |
| 95 | + 'switch_inline_query' => 'earth' |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + $inline_keyboards[] = $inline_keyboard; |
| 100 | + unset($inline_keyboard); |
| 101 | + |
| 102 | + //3 |
| 103 | + $inline_keyboard[] = [ |
| 104 | + [ |
| 105 | + 'text' => 'open url', |
| 106 | + 'url' => 'https://github.com/akalongman/php-telegram-bot' |
| 107 | + ] |
| 108 | + ]; |
| 109 | + $inline_keyboard[] = [ |
| 110 | + [ |
| 111 | + 'text' => 'switch to inline', |
| 112 | + 'switch_inline_query' => 'thumb up' |
| 113 | + ] |
| 114 | + ]; |
| 115 | + $inline_keyboard[] = [ |
| 116 | + [ |
| 117 | + 'text' => 'send callback query', |
| 118 | + 'callback_data' => 'thumb up' |
| 119 | + ], |
| 120 | + [ |
| 121 | + 'text' => 'send callback query (no alert)', |
| 122 | + 'callback_data' => 'thumb down' |
| 123 | + ] |
| 124 | + ]; |
| 125 | + |
| 126 | + $inline_keyboards[] = $inline_keyboard; |
| 127 | + unset($inline_keyboard); |
| 128 | + |
| 129 | + $data['reply_markup'] = new InlineKeyboardMarkup( |
| 130 | + [ |
| 131 | + 'inline_keyboard' => $inline_keyboards[3], |
| 132 | + ] |
| 133 | + ); |
| 134 | + |
| 135 | + return Request::sendMessage($data); |
| 136 | + } |
| 137 | +} |
0 commit comments