-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramAPI.php
More file actions
253 lines (217 loc) · 6.39 KB
/
TelegramAPI.php
File metadata and controls
253 lines (217 loc) · 6.39 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php namespace ProcessWire;
/**
* Telegram Bot API Wrapper
*
* @author Maxim Alex
*/
class TelegramAPI extends WireData {
/**
* Bot token
*/
protected $token;
/**
* API base URL
*/
protected $apiUrl = 'https://api.telegram.org/bot';
/**
* Default options
*/
protected $options = [
'timeout' => 10,
'parseMode' => 'HTML'
];
/**
* Last error
*/
protected $lastError = '';
/**
* Last response
*/
protected $lastResponse = null;
/**
* Constructor
*
* @param string $token Bot token
* @param array $options Additional options
*/
public function __construct($token, array $options = []) {
$this->token = $token;
$this->options = array_merge($this->options, $options);
}
/**
* Send message
*
* @param string|int $chatId Chat ID
* @param string $text Message text
* @param array $options Additional options
* @return array|false Response or false on error
*/
public function sendMessage($chatId, $text, array $options = []) {
$params = array_merge([
'chat_id' => $chatId,
'text' => $text
], $options);
if(!isset($params['parse_mode']) && $this->options['parseMode']) {
$params['parse_mode'] = $this->options['parseMode'];
}
return $this->request('sendMessage', $params);
}
/**
* Send photo
*
* @param string|int $chatId Chat ID
* @param string $photo Photo URL or file path
* @param string $caption Optional caption
* @param array $options Additional options
* @return array|false
*/
public function sendPhoto($chatId, $photo, $caption = '', array $options = []) {
$params = array_merge([
'chat_id' => $chatId,
'photo' => $photo
], $options);
if($caption) {
$params['caption'] = $caption;
}
return $this->request('sendPhoto', $params);
}
/**
* Send document
*
* @param string|int $chatId Chat ID
* @param string $document Document URL or file path
* @param string $caption Optional caption
* @param array $options Additional options
* @return array|false
*/
public function sendDocument($chatId, $document, $caption = '', array $options = []) {
$params = array_merge([
'chat_id' => $chatId,
'document' => $document
], $options);
if($caption) {
$params['caption'] = $caption;
}
return $this->request('sendDocument', $params);
}
/**
* Get bot information
*
* @return array|false
*/
public function getMe() {
return $this->request('getMe');
}
/**
* Get updates
*
* @param int $offset Update offset
* @param int $limit Number of updates
* @param int $timeout Long polling timeout
* @return array|false
*/
public function getUpdates($offset = 0, $limit = 100, $timeout = 0) {
$params = [
'offset' => $offset,
'limit' => $limit,
'timeout' => $timeout
];
return $this->request('getUpdates', $params);
}
/**
* Set webhook
*
* @param string $url Webhook URL
* @param array $options Additional options
* @return array|false
*/
public function setWebhook($url, array $options = []) {
$params = array_merge(['url' => $url], $options);
return $this->request('setWebhook', $params);
}
/**
* Delete webhook
*
* @return array|false
*/
public function deleteWebhook() {
return $this->request('deleteWebhook');
}
/**
* Make API request
*
* @param string $method API method
* @param array $params Request parameters
* @return array|false Response data or false on error
*/
protected function request($method, array $params = []) {
$url = $this->apiUrl . $this->token . '/' . $method;
// Use JSON for better compatibility
$jsonData = json_encode($params);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $jsonData,
CURLOPT_TIMEOUT => $this->options['timeout'],
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Content-Length: ' . strlen($jsonData)
]
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if($error) {
$this->lastError = "CURL Error: {$error}";
return false;
}
if($httpCode !== 200) {
$this->lastError = "HTTP Error: {$httpCode}";
if($response) {
$data = json_decode($response, true);
if(isset($data['description'])) {
$this->lastError .= " - " . $data['description'];
}
}
return false;
}
$data = json_decode($response, true);
$this->lastResponse = $data;
if(!$data || !isset($data['ok'])) {
$this->lastError = "Invalid API response";
return false;
}
if(!$data['ok']) {
$errorMsg = $data['description'] ?? 'Unknown error';
$errorCode = $data['error_code'] ?? 0;
$this->lastError = "API Error ({$errorCode}): {$errorMsg}";
// Add helpful hints for common errors
if($errorCode == 403) {
$this->lastError .= " - Bot was blocked by the user or user hasn't started conversation with bot";
} elseif($errorCode == 400 && strpos($errorMsg, 'chat not found') !== false) {
$this->lastError .= " - Invalid chat ID or bot cannot access this chat";
}
return false;
}
return $data['result'] ?? true;
}
/**
* Get last error
*
* @return string
*/
public function getLastError() {
return $this->lastError;
}
/**
* Get last response
*
* @return array|null
*/
public function getLastResponse() {
return $this->lastResponse;
}
}