1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpBotFramework\Utilities; |
4
|
|
|
|
5
|
|
|
trait BotState { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* \addtogroup State |
9
|
|
|
* \brief Create a state based bot using these methods. |
10
|
|
|
* \details Bot will answer in different way based on the state. |
11
|
|
|
* Here is an example where we use save user credential using bot states: |
12
|
|
|
* |
13
|
|
|
* <?php |
14
|
|
|
* |
15
|
|
|
* // Include the framework |
16
|
|
|
* require './vendor/autoload.php'; |
17
|
|
|
* |
18
|
|
|
* // Define bot state |
19
|
|
|
* define("SEND_USERNAME", 1); |
20
|
|
|
* define("SEND_PASSWORD", 2); |
21
|
|
|
* |
22
|
|
|
* // Create the class for the bot that will handle login |
23
|
|
|
* class LoginBot extends DanySpin97\PhpBotFramework\Bot { |
24
|
|
|
* |
25
|
|
|
* // Add the function for processing messages |
26
|
|
|
* protected function processMessage($message) { |
27
|
|
|
* |
28
|
|
|
* switch($this->getStatus()) { |
29
|
|
|
* |
30
|
|
|
* // If we are expecting a username from the user |
31
|
|
|
* case SEND_USERNAME: |
32
|
|
|
* |
33
|
|
|
* // Save the username |
34
|
|
|
* |
35
|
|
|
* // Say the user to insert the password |
36
|
|
|
* $this->sendMessage("Please, send your password."); |
37
|
|
|
* |
38
|
|
|
* // Update the bot state |
39
|
|
|
* $this->setStatus(SEND_PASSWORD); |
40
|
|
|
* |
41
|
|
|
* break; |
42
|
|
|
* |
43
|
|
|
* // Or if we are expecting a password from the user |
44
|
|
|
* case SEND_PASSWORD: |
45
|
|
|
* |
46
|
|
|
* // Save the password |
47
|
|
|
* |
48
|
|
|
* // Say the user he completed the process |
49
|
|
|
* $this->sendMessage("The registration is complete"); |
50
|
|
|
* |
51
|
|
|
* break; |
52
|
|
|
* } |
53
|
|
|
* |
54
|
|
|
* } |
55
|
|
|
* |
56
|
|
|
* } |
57
|
|
|
* |
58
|
|
|
* // Create the bot |
59
|
|
|
* $bot = new LoginBot("token"); |
60
|
|
|
* |
61
|
|
|
* // Create redis object |
62
|
|
|
* $bot->redis = new Redis(); |
63
|
|
|
* |
64
|
|
|
* // Connect to redis database |
65
|
|
|
* $bot->redis->connect('127.0.0.1'); |
66
|
|
|
* |
67
|
|
|
* // Create the awnser to the <code>/start</code> command |
68
|
|
|
* $start_closure = function($bot, $message) { |
69
|
|
|
* |
70
|
|
|
* // saying the user to enter a username |
71
|
|
|
* $bot->sendMessage("Please, send your username."); |
72
|
|
|
* |
73
|
|
|
* // and update the status |
74
|
|
|
* $bot->setStatus(SEND_USERNAME); |
75
|
|
|
* }; |
76
|
|
|
* |
77
|
|
|
* // Add the answer |
78
|
|
|
* $bot->addMessageCommand("start", $start_closure); |
79
|
|
|
* |
80
|
|
|
* $bot->getUpdatesLocal(); |
81
|
|
|
* @{ |
82
|
|
|
*/ |
83
|
|
|
|
84
|
|
|
/** \brief Status of the bot to handle data inserting and menu-like bot. */ |
85
|
|
|
public $status; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* \brief Get current user status from redis and set it in status variable. |
89
|
|
|
* \details Throw exception if redis connection is missing. |
90
|
|
|
* @param $default_status <i>Optional</i>. The default status to return in case there is no status for the current user. |
91
|
|
|
* @return The status for the current user, $default_status if missing. |
92
|
|
|
*/ |
93
|
|
|
public function getStatus(int $default_status = -1) : int { |
94
|
|
|
|
95
|
|
|
if (!isset($this->redis)) { |
96
|
|
|
|
97
|
|
|
throw new BotException('Redis connection not set'); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
View Code Duplication |
if ($this->redis->exists($this->_chat_id . ':status')) { |
|
|
|
|
102
|
|
|
|
103
|
|
|
$this->status = $this->redis->get($this->_chat_id . ':status'); |
|
|
|
|
104
|
|
|
|
105
|
|
|
return $this->status; |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->redis->set($this->_chat_id . ':status', $default_status); |
110
|
|
|
$this->status = $default_status; |
111
|
|
|
return $default_status; |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** \brief Set the status of the bot in both redis and $status. |
116
|
|
|
* \details Throw exception if redis connection is missing. |
117
|
|
|
* @param $status The new status of the bot. |
118
|
|
|
*/ |
119
|
|
|
public function setStatus(int $status) { |
120
|
|
|
|
121
|
|
|
$this->redis->set($this->_chat_id . ':status', $status); |
122
|
|
|
|
123
|
|
|
$this->status = $status; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** @} */ |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.