-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathCancelCommand.php
105 lines (90 loc) · 2.46 KB
/
CancelCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/cancel" command
*
* This command cancels the currently active conversation and
* returns a message to let the user know which conversation it was.
*
* If no conversation is active, the returned message says so.
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Conversation;
use Longman\TelegramBot\Entities\Keyboard;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
class CancelCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'cancel';
/**
* @var string
*/
protected $description = 'Cancel the currently active conversation';
/**
* @var string
*/
protected $usage = '/cancel';
/**
* @var string
*/
protected $version = '0.3.0';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* Main command execution if no DB connection is available
*
* @throws TelegramException
*/
public function executeNoDb(): ServerResponse
{
return $this->removeKeyboard('Nothing to cancel.');
}
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
$text = 'No active conversation!';
// Cancel current conversation if any
$conversation = new Conversation(
$this->getMessage()->getFrom()->getId(),
$this->getMessage()->getChat()->getId()
);
if ($conversation_command = $conversation->getCommand()) {
$conversation->cancel();
$text = 'Conversation "' . $conversation_command . '" cancelled!';
}
return $this->removeKeyboard($text);
}
/**
* Remove the keyboard and output a text.
*
* @param string $text
*
* @return ServerResponse
* @throws TelegramException
*/
private function removeKeyboard(string $text): ServerResponse
{
return $this->replyToChat($text, [
'reply_markup' => Keyboard::remove(['selective' => true]),
]);
}
}