|
| 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\Conversation; |
| 15 | +use Longman\TelegramBot\Entities\Keyboard; |
| 16 | +use Longman\TelegramBot\Request; |
| 17 | + |
| 18 | +/** |
| 19 | + * User "/cancel" command |
| 20 | + * |
| 21 | + * This command cancels the currently active conversation and |
| 22 | + * returns a message to let the user know which conversation it was. |
| 23 | + * If no conversation is active, the returned message says so. |
| 24 | + */ |
| 25 | +class CancelCommand extends UserCommand |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + protected $name = 'cancel'; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + protected $description = 'Cancel the currently active conversation'; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var string |
| 39 | + */ |
| 40 | + protected $usage = '/cancel'; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var string |
| 44 | + */ |
| 45 | + protected $version = '0.2.0'; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var bool |
| 49 | + */ |
| 50 | + protected $need_mysql = true; |
| 51 | + |
| 52 | + /** |
| 53 | + * Command execute method |
| 54 | + * |
| 55 | + * @return \Longman\TelegramBot\Entities\ServerResponse |
| 56 | + * @throws \Longman\TelegramBot\Exception\TelegramException |
| 57 | + */ |
| 58 | + public function execute() |
| 59 | + { |
| 60 | + $text = 'No active conversation!'; |
| 61 | + |
| 62 | + //Cancel current conversation if any |
| 63 | + $conversation = new Conversation( |
| 64 | + $this->getMessage()->getFrom()->getId(), |
| 65 | + $this->getMessage()->getChat()->getId() |
| 66 | + ); |
| 67 | + |
| 68 | + if ($conversation_command = $conversation->getCommand()) { |
| 69 | + $conversation->cancel(); |
| 70 | + $text = 'Conversation "' . $conversation_command . '" cancelled!'; |
| 71 | + } |
| 72 | + |
| 73 | + return $this->removeKeyboard($text); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Remove the keyboard and output a text |
| 78 | + * |
| 79 | + * @param string $text |
| 80 | + * |
| 81 | + * @return \Longman\TelegramBot\Entities\ServerResponse |
| 82 | + * @throws \Longman\TelegramBot\Exception\TelegramException |
| 83 | + */ |
| 84 | + private function removeKeyboard($text) |
| 85 | + { |
| 86 | + return Request::sendMessage( |
| 87 | + [ |
| 88 | + 'reply_markup' => Keyboard::remove(['selective' => true]), |
| 89 | + 'chat_id' => $this->getMessage()->getChat()->getId(), |
| 90 | + 'text' => $text, |
| 91 | + ] |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Command execute method if MySQL is required but not available |
| 97 | + * |
| 98 | + * @return \Longman\TelegramBot\Entities\ServerResponse |
| 99 | + * @throws \Longman\TelegramBot\Exception\TelegramException |
| 100 | + */ |
| 101 | + public function executeNoDb() |
| 102 | + { |
| 103 | + return $this->removeKeyboard('Nothing to cancel.'); |
| 104 | + } |
| 105 | +} |
0 commit comments