1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TelegramBot; |
4
|
|
|
|
5
|
|
|
use TelegramBot\Entities\Update; |
6
|
|
|
use TelegramBot\Util\CrossData; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Plugin |
10
|
|
|
* |
11
|
|
|
* @link https://github.com/telegram-bot-php/core |
12
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
13
|
|
|
* @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License) |
14
|
|
|
*/ |
15
|
|
|
class Plugin |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The Update types |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected array $update_types = [ |
24
|
|
|
'Message', |
25
|
|
|
'EditedMessage', |
26
|
|
|
'ChannelPost', |
27
|
|
|
'EditedChannelPost', |
28
|
|
|
'InlineQuery', |
29
|
|
|
'ChosenInlineResult', |
30
|
|
|
'CallbackQuery', |
31
|
|
|
'ShippingQuery', |
32
|
|
|
'PreCheckoutQuery', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var WebhookHandler |
37
|
|
|
*/ |
38
|
|
|
protected WebhookHandler $hook; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var \Generator |
42
|
|
|
*/ |
43
|
|
|
protected \Generator $returns; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
protected bool $kill_on_yield = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check for the exit of the plugin. |
52
|
|
|
* |
53
|
|
|
* @param \Generator $return |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
|
|
public function __checkExit(\Generator $return): void |
57
|
|
|
{ |
58
|
|
|
if ($return->valid()) { |
59
|
|
|
if ($return->current() !== null && $this->kill_on_yield === true) { |
60
|
|
|
$this->kill(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($return->valid()) { |
65
|
|
|
$return->next(); |
66
|
|
|
$this->__checkExit($return); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Identify the update type. e.g. Message, EditedMessage, etc. |
72
|
|
|
* |
73
|
|
|
* @param Update $update |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
|
|
public function __identify(Update $update): ?string |
77
|
|
|
{ |
78
|
|
|
$type = $update->getUpdateType(); |
79
|
|
|
|
80
|
|
|
if ($type === null) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return str_replace('_', '', ucwords($type, '_')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Execute the plugin. |
89
|
|
|
* |
90
|
|
|
* @param WebhookHandler $receiver |
91
|
|
|
* @param Update $update |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function __execute(WebhookHandler $receiver, Update $update): void |
95
|
|
|
{ |
96
|
|
|
$this->hook = $receiver; |
97
|
|
|
|
98
|
|
|
if (method_exists($this, 'onReceivedUpdate')) { |
99
|
|
|
$return = $this->onReceivedUpdate($update); |
100
|
|
|
$this->__checkExit($return); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$type = $this->__identify($update); |
104
|
|
|
if (method_exists($this, ($method = 'on' . $type)) && $type !== null) { |
105
|
|
|
$return = $this->$method($update); |
106
|
|
|
$this->__checkExit($return); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Kill the plugin. |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function kill(): void |
116
|
|
|
{ |
117
|
|
|
$this->hook->kill(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* put CrossData into the plugins |
122
|
|
|
* |
123
|
|
|
* @param string $key |
124
|
|
|
* @param mixed $value |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function putCrossData(string $key, mixed $value): void |
128
|
|
|
{ |
129
|
|
|
CrossData::put($key, $value); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* get CrossData from the plugins |
134
|
|
|
* |
135
|
|
|
* @param string $key |
136
|
|
|
* @return string|array|bool|null |
137
|
|
|
*/ |
138
|
|
|
public function getCrossData(string $key): string|array|bool|null |
139
|
|
|
{ |
140
|
|
|
return CrossData::get($key); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |