1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telegram\Bot; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Telegram\Bot\Exceptions\TelegramSDKException; |
8
|
|
|
use Telegram\Bot\HttpClients\GuzzleHttpClient; |
9
|
|
|
use Telegram\Bot\HttpClients\HttpClientInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class TelegramClient. |
13
|
|
|
*/ |
14
|
|
|
class TelegramClient |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @const string Telegram Bot API URL. |
18
|
|
|
*/ |
19
|
|
|
const BASE_BOT_URL = 'https://api.telegram.org/bot'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @const int The timeout in seconds for a request that contains file uploads. |
23
|
|
|
*/ |
24
|
|
|
const DEFAULT_FILE_UPLOAD_REQUEST_TIMEOUT = 3600; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @const int The timeout in seconds for a request that contains video uploads. |
28
|
|
|
*/ |
29
|
|
|
const DEFAULT_VIDEO_UPLOAD_REQUEST_TIMEOUT = 7200; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var HttpClientInterface|null HTTP Client |
33
|
|
|
*/ |
34
|
|
|
protected $httpClientHandler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Instantiates a new TelegramClient object. |
38
|
|
|
* |
39
|
|
|
* @param HttpClientInterface|null $httpClientHandler |
40
|
|
|
*/ |
41
|
66 |
|
public function __construct(HttpClientInterface $httpClientHandler = null) |
42
|
|
|
{ |
43
|
66 |
|
$this->httpClientHandler = $httpClientHandler ?: new GuzzleHttpClient(); |
44
|
66 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sets the HTTP client handler. |
48
|
|
|
* |
49
|
|
|
* @param HttpClientInterface $httpClientHandler |
50
|
|
|
*/ |
51
|
|
|
public function setHttpClientHandler(HttpClientInterface $httpClientHandler) |
52
|
|
|
{ |
53
|
|
|
$this->httpClientHandler = $httpClientHandler; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the HTTP client handler. |
58
|
|
|
* |
59
|
|
|
* @return HttpClientInterface |
60
|
|
|
*/ |
61
|
4 |
|
public function getHttpClientHandler() |
62
|
|
|
{ |
63
|
4 |
|
return $this->httpClientHandler; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the base Bot URL. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
40 |
|
public function getBaseBotUrl() |
72
|
|
|
{ |
73
|
40 |
|
return static::BASE_BOT_URL; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Prepares the API request for sending to the client handler. |
78
|
|
|
* |
79
|
|
|
* @param TelegramRequest $request |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
40 |
|
public function prepareRequest(TelegramRequest $request) |
84
|
|
|
{ |
85
|
40 |
|
$url = $this->getBaseBotUrl().$request->getAccessToken().'/'.$request->getEndpoint(); |
86
|
|
|
|
87
|
|
|
return [ |
88
|
40 |
|
$url, |
89
|
40 |
|
$request->getMethod(), |
90
|
40 |
|
$request->getHeaders(), |
91
|
40 |
|
$request->isAsyncRequest(), |
92
|
40 |
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Send an API request and process the result. |
97
|
|
|
* |
98
|
|
|
* @param TelegramRequest $request |
99
|
|
|
* |
100
|
|
|
* @throws TelegramSDKException |
101
|
|
|
* |
102
|
|
|
* @return TelegramResponse |
103
|
|
|
*/ |
104
|
40 |
|
public function sendRequest(TelegramRequest $request) |
105
|
|
|
{ |
106
|
40 |
|
list($url, $method, $headers, $isAsyncRequest) = $this->prepareRequest($request); |
107
|
|
|
|
108
|
40 |
|
$timeOut = $request->getTimeOut(); |
109
|
40 |
|
$connectTimeOut = $request->getConnectTimeOut(); |
110
|
|
|
|
111
|
40 |
|
$options = $this->getOptions($request, $method); |
112
|
40 |
|
|
113
|
40 |
|
$rawResponse = $this->httpClientHandler->send($url, $method, $headers, $options, $timeOut, $isAsyncRequest, $connectTimeOut); |
114
|
|
|
|
115
|
|
|
$returnResponse = $this->getResponse($request, $rawResponse); |
116
|
|
|
|
117
|
40 |
|
if ($returnResponse->isError()) { |
118
|
|
|
throw $returnResponse->getThrownException(); |
119
|
40 |
|
} |
120
|
|
|
|
121
|
40 |
|
return $returnResponse; |
122
|
2 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
38 |
|
* Creates response object. |
126
|
|
|
* |
127
|
|
|
* @param TelegramRequest $request |
128
|
|
|
* @param ResponseInterface|PromiseInterface $response |
129
|
|
|
* |
130
|
|
|
* @return TelegramResponse |
131
|
|
|
*/ |
132
|
|
|
protected function getResponse(TelegramRequest $request, $response) |
133
|
|
|
{ |
134
|
|
|
return new TelegramResponse($request, $response); |
135
|
|
|
} |
136
|
40 |
|
|
137
|
|
|
/** |
138
|
40 |
|
* @param \Telegram\Bot\TelegramRequest $request |
139
|
|
|
* @param $method |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
private function getOptions(TelegramRequest $request, $method) |
143
|
|
|
{ |
144
|
|
|
if ($method === 'POST') { |
145
|
|
|
return $request->getPostParams(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return ['query' => $request->getParams()]; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|