|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Client class for Telegram Bot-API. Can be used for running bots in two supported modes: |
|
5
|
|
|
* daemon listener mode and webhook mode. Can easily be instantiated in any place of your application. |
|
6
|
|
|
* |
|
7
|
|
|
* @package Teebot (Telegram bot framework) |
|
8
|
|
|
* |
|
9
|
|
|
* @author Stanislav Drozdov <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Teebot; |
|
15
|
|
|
|
|
16
|
|
|
use Teebot\Configuration\{ |
|
17
|
|
|
AbstractContainer, |
|
18
|
|
|
Config |
|
19
|
|
|
}; |
|
20
|
|
|
use Teebot\Api\{ |
|
21
|
|
|
Logger\Logger, |
|
22
|
|
|
Command\Processor, |
|
23
|
|
|
HttpClient, |
|
24
|
|
|
Method\GetUpdates, |
|
25
|
|
|
Response |
|
26
|
|
|
}; |
|
27
|
|
|
|
|
28
|
|
|
class Client implements ClientInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var AbstractContainer |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Processor $processor |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $processor; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var Logger |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $logger; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(AbstractContainer $config) |
|
46
|
|
|
{ |
|
47
|
|
|
define('TEEBOT_ROOT', realpath(__DIR__ . '/../')); |
|
48
|
|
|
|
|
49
|
|
|
$this->init($config); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Initializes the Client |
|
54
|
|
|
* |
|
55
|
|
|
* @param AbstractContainer $config |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function init(AbstractContainer $config) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->config = $config; |
|
60
|
|
|
$this->processor = new Processor($config, new HttpClient($config)); |
|
61
|
|
|
$this->logger = new Logger($config); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the latest updates from Telegram's API server |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $offset Offset for the updates list |
|
68
|
|
|
* @param int $limit Limit of the updates to get |
|
69
|
|
|
* @param bool $silentMode If set to true then the events, mapped (in config or by default) to |
|
70
|
|
|
* the entities in the result will not be triggered |
|
71
|
|
|
* @return Response |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getUpdates( |
|
74
|
|
|
$offset = Config::DEFAULT_OFFSET, |
|
75
|
|
|
$limit = Config::DEFAULT_LIMIT, |
|
76
|
|
|
$silentMode = false |
|
77
|
|
|
): Response { |
|
78
|
|
|
$method = (new GetUpdates()) |
|
79
|
|
|
->setOffset($offset) |
|
80
|
|
|
->setLimit($limit) |
|
81
|
|
|
->setTimeout($this->config->get('timeout')); |
|
82
|
|
|
|
|
83
|
|
|
return $this->processor->call($method, $silentMode); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Flushes the results and resets the offset pointer to the latest updates. Returns last offset. |
|
88
|
|
|
* Should be used to skip previous results from dialogs during first listener's start. Must not |
|
89
|
|
|
* be used for webhook. |
|
90
|
|
|
* |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
|
|
public function flush(): int |
|
94
|
|
|
{ |
|
95
|
|
|
$response = $this->getUpdates(Config::DEFAULT_OFFSET, Config::DEFAULT_LIMIT, true); |
|
96
|
|
|
|
|
97
|
|
|
return $response instanceof Response ? $response->getOffset() : -1; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Starts listener in daemon mode for receiving the updates from the chats. Should be used if webhook is not set. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function listen() |
|
104
|
|
|
{ |
|
105
|
|
|
$offset = $this->flush(); |
|
106
|
|
|
|
|
107
|
|
|
while (1) { |
|
108
|
|
|
try { |
|
109
|
|
|
$response = $this->getUpdates($offset); |
|
110
|
|
|
|
|
111
|
|
|
if ($response instanceof Response) { |
|
112
|
|
|
$offset = $response->getOffset(); |
|
113
|
|
|
} |
|
114
|
|
|
} catch (\Exception $e){ |
|
115
|
|
|
$this->logger->exception($e); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
sleep($this->config->get('timeout')); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns Response object built from received data. Method should be used if |
|
124
|
|
|
* bot is running in webhook mode. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $receivedData Received data from Telegram's webhook call. Not required, but could |
|
127
|
|
|
* be passed manually. If not passed - php input will be used to get the data. |
|
128
|
|
|
* @param bool $silentMode If set to true then the events, mapped to |
|
129
|
|
|
* the entities in the result will not be triggered |
|
130
|
|
|
* @return Response |
|
131
|
|
|
*/ |
|
132
|
|
|
public function webhook($receivedData = '', $silentMode = false): Response |
|
133
|
|
|
{ |
|
134
|
|
|
if (empty($receivedData)) { |
|
135
|
|
|
$receivedData = file_get_contents("php://input"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
if (empty($receivedData)) { |
|
139
|
|
|
return null; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $this->processor->getWebhookResponse($receivedData, $silentMode); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|