-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathCancelCommand.php
103 lines (90 loc) · 2.61 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
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PhpTelegramBot\Core\Commands\UserCommands;
use PhpTelegramBot\Core\Commands\UserCommand;
use PhpTelegramBot\Core\Conversation;
use PhpTelegramBot\Core\Entities\Keyboard;
use PhpTelegramBot\Core\Request;
/**
* 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.
*/
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.2.1';
/**
* @var bool
*/
protected $need_mysql = true;
/**
* Command execute method
*
* @return \PhpTelegramBot\Core\Entities\ServerResponse
* @throws \PhpTelegramBot\Core\Exception\TelegramException
*/
public function execute()
{
$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 \PhpTelegramBot\Core\Entities\ServerResponse
* @throws \PhpTelegramBot\Core\Exception\TelegramException
*/
private function removeKeyboard($text)
{
return Request::sendMessage([
'reply_markup' => Keyboard::remove(['selective' => true]),
'chat_id' => $this->getMessage()->getChat()->getId(),
'text' => $text,
]);
}
/**
* Command execute method if MySQL is required but not available
*
* @return \PhpTelegramBot\Core\Entities\ServerResponse
* @throws \PhpTelegramBot\Core\Exception\TelegramException
*/
public function executeNoDb()
{
return $this->removeKeyboard('Nothing to cancel.');
}
}